How to require a fork with composer

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


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?


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"
},