ChatGPT解决这个技术问题 Extra ChatGPT

How to require a fork with composer?

This is my composer.json, I want to use Nodge's fork of lessphp project on Github

 "repositories": [{
    "type": "package",
    "package": {
        "version": "dev-master",
        "name": "nodge/lessphp",
        "source": {
            "url": "https://github.com/Nodge/lessphp.git",
            "type": "git",
            "reference": "master"
        },
        "autoload": {
            "classmap": ["lessc.inc.php"]
        }
    }
}],
"require": {
    "php": ">=5.3.3",
    "nodge/lessphp": "dev-master"
},

But I get this error when I run composer update:

nodge/lessphp dev-master -> no matching package found.

I don't know how to require correctly this fork.


m
miken32

The most common (and easiest) way of doing it is using a VCS repository.

All you have to do is add your fork as a repository and update the version constraint to point to your custom branch. Your custom branch name must be prefixed with dev-.

Assuming you forked monolog/monolog and created a branch called bugfix, you would update your composer.json like this:

{
    "repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/igorw/monolog"
        }
    ],
    "require": {
        "monolog/monolog": "dev-bugfix"
    }
}

Note that you don't change the require statement except to specify your bugfix branch. You still reference the upstream package (monolog/monolog), not your personal fork (igorw/monolog), and the branch name is prefixed with dev-. You can read details in the docs


This doesn't seem to work for github.com/Polycademy/purl which is a fork of github.com/jwage/purl I've been trying to: "repositories": [ { "type": "vcs", "url": "git@github.com:Polycademy/purl.git" } ], and "require": { "jwade/purl": "dev-master", }, But I keep getting: Problem 1 - The requested package jwade/purl could not be found in any version, there may be a typo in the package name.
@CMCDragonkai try running composer show jwade/purl -v it should tell you why it ignored the dev-master version if it does.
"Read the docs" is a bad answer, a better answer would be to give a concrete example
Nice to warn about not referencing to personal fork. This saved my day, +1
Your branch name does not need to add dev- you will end up duplicated dev-dev- when you require in composer.json file. This is why our master branch when you required in composer.json, you have added in as dev-master
N
Neilime

Using VCS works:

"name": "test/test",
"repositories": [{
    "type": "vcs",
    "url": "http://github.com/Nodge/lessphp"
}],
"require": {
    "leafo/lessphp": "dev-master"
},

But if I require a module that has this composer.json, it doesn't work. It installs the original project, not the fork.

Example

"name": "example/example",
"require": {
    "test/test": "dev-master"
},

I should mention again the repository. Is that normal?


same here same problem
Manually delete the module using "rm -rf" and then "composer update". That worked for me.
I've used a dozen of forks, and this never worked. Here's a working solution: stackoverflow.com/a/27970559/183904
@Julien, I've just used this method and it worked like a charm... The docs are quite clear, you must only remember to put the dev- prefix in your custom branch name when requiring the package in question.
Worked for me... but I want to mention that I needed to use https:// for the URL.
S
Shiro

If you can't get @Neilime answer to work for you, make sure your fork uses a different branch.

For example push your changes to a branch on your fork called my-bugfix, do not added dev- prefix in your branch name but in your composer.json you have to add it. Your composer file will look like:

"repositories":
[
    {
        "type": "vcs",
        "url": "http://github.com/yourname/packageName"
    }
],
"require": {
    "owner/packageName": "dev-my-bugfix"
},

This is ingenious and clear explanation for me ! Thank You !
but what if it's someone else's fork? See example here
Also, name field in your forked repository's composer.json file should stay the same with the original owner.
M
Mohamed Saleh

I have tried many options but After I got this post I saw the light and it just worked perfect.

This is what you have to do:

1- Fork The repository

2- Create a branch and make the required modifications.

3- Add the repository label to your composer.json

"repositories": [
     
        {
            "type": "vcs",
            "url": "https://github.com/user/yourforkname"
        }
    ]

4- In the command line inside your project require your fork like this:

composer require vendor/packagename:dev-branchname

And Voilá!!

You have your fork version working


E
E Ciotti

According to the Composer documentation http://getcomposer.org/doc/05-repositories.md#vcs, it's enough to specify the original repository (not the fork) in the require ("nodge/lessphp" in your case). Composer will then install YOUR fork (look at the code in the vendors)


S
Sam

So, this is 2019, and most of the answers here are already correct.

If you find yourself however, in a situation where you need to require a particular branch of your fork (that you created), have composer list the available versions/tags first. This saved me a lot of time.

A full example with spatie/laravel-backup package.

First, add repositories key to composer.json. With the url of your fork

"repositories": [{
   "type": "vcs",
   "url": "https://github.com/holymp2006/laravel-backup"
 }]

Get available versions/tags

composer show "spatie/laravel-backup" --all

Choose the version you want from versions in the terminal output, then require that version

composer require spatie/laravel-backup:v5.x-dev

u
user3497737

I usually add a "dist" node to the package definition. I never had a problem using it this way.

I can't remember where I got this trick from, though, for any further explanations.

{
    "repositories": [
        {
            "type": "package",
            "package": {
                "version": "dev-master",
                "name": "nodge/lessphp",
                "source": {
                    "url": "https://github.com/Nodge/lessphp.git",
                    "type": "git",
                    "reference": "master"
                },
                "autoload": {
                    "classmap": ["lessc.inc.php"]
                },
                "dist": {
                    "url": "https://github.com/Nodge/lessphp/archive/master.zip",
                    "type": "zip"
                }
            }
        }
    ],
    "require": {
        "nodge/lessphp": "*"
    }
}

k
kennethormandy

The accepted answer and clarifying answers all worked well for me when I had ex. an application, which needed a dependency I had forked and modified. I’d still use the accepted answer in this case.

However, when I had a package I wanted to distribute myself on Packagist, which also contained a forked and modified dependency, this approach no longer worked.

If someone were to try and install with this config, they’ll still get that same -> no matching package found. error message.

This answer and the linked Composer docs suggest that the repo settings have to be at the top-level composer.json. That means, someone installing this package would have to add that config to their composer.json file too—which adds a lot of unnecessary confusion to the install process.

Instead, I published my fork to Packagist. My understanding is that while forks are frowned upon, this would be considered a maintained fork, since I am using it for this new package.

Hopefully that’s helpful for anyone who has this problem with a package or library they’d like to distribute.