Use a GitHub Branch as a Composer Dependency

My current project sees Celery (a python distributed task queue) added to my PHP application. There’s a handy PHP interface to the RabbitMQ that Celery uses as a backend, which makes it easy for me to create jobs, called celery-php. This requires either the PECL AMQP extension or alternatively it has experimental support for the PHP library for AMQP – I would normally prefer the PECL version but ran into version compatibility problems, missing manual pages, and decided that a pure PHP solution might be more portable and perhaps I would just add the experimental branch to my composer.json file for this project.

The branch is called php-amqplib, and I need to do two things:

  • Add the Github repository for celery-php to the composer.json, so that it will be picked up rather than Composer looking on packagist for it.
  • Specify the version as dev-[branchname]

The result looks like this:

{
    "repositories": [
        {
            "type": "git",
            "url": "https://github.com/gjedeer/celery-php"
        }
    ],
    "require": {
        "videlalvaro/php-amqplib": "2.2.*",
        "massivescale/celery-php": "dev-php-amqplib"
    }
}

14 thoughts on “Use a GitHub Branch as a Composer Dependency

  1. Would this also work if you forked a project and only wanted to pull updates from a specific github repo? (Instead of the default) I recently made a custom change to a Laravel package and would love to only get updates from my repo through composer.

  2. I’ve just tried this to allow branch specific versioning of a package that I make regular small changes to (meaning updating a tag seemed excessive). I tried creating a branch named 1.1 (based on the most recent tag being 1.1.12) and then just commit small changes to this. Unfortunately Composer must use a test for if the branch name is numeric and try resolving it to a tag – I got the usual (and totally useless) “Requested package could not be found” message using “dev-1.1” but after renaming the branch to “one-point-one” and the requirement to “dev-one-point-one” it works. A way round this would be useful if one existed, but it works for now.

    • @M1ke for a less verbose branch workaround than ‘number-point-number’ you can use something like: v.2.1.3

  3. If other packages have requirements on the package you are changing, your development branch may fail to meet those requirements (which will result in “Your requirements could not be resolved to an installable set of packages.”). To fix this, you can do an inline alias to that all other packages will see it as a specific version.

    If you started with:
    [code]
    “massivescale/celery-php”: “1.2.3”
    [/code]
    change it to:
    [code]
    “massivescale/celery-php”: “dev-php-amqplib as 1.2.3”
    [/code]

    (Thanks to Rafael Dohms for the tip.)

  4. Pingback: PHP7: Easiest Upgrade Yet | LornaJane

  5. Note: if composer fails to find the branch (“The requested package user/repo branch could not be found.”) try clearing the cache with composer clear-cache. That fixed the issue for me.

    • The devil is in the detail with this – so glad it helped you too (I refer to this post fairly often!)

  6. I’m struggling. I have this that works:

    “`
    “repositories”: [
    {
    “type”: “composer”,
    “url”: “https://nova.laravel.com”
    }
    ],
    “`

    Now I have to add this:

    “`
    “repositories”: {
    “nova-sluggable”: {
    “type”: “vcs”,
    “url”: “https://github.com/TheNerka/nova-sluggable”
    }
    }
    “`

    I don’t understand how to combine the bottom one with the top one. The top one’s syntax doesn’t have the “:” name part. But I need bot.

    • I don’t think that the repositories element can take named values. Try adding them in a collection with square brackets, and no named key. Composer wll check all the enries in this section and figure out which repo is where.

  7. Two things to beware of:

    1. Composer might require a GitHub OAuth token to access repositories sometimes; composer will prompt you to set one up, but only if you try to install the package using a command instead of editing composer.json (you should add the repository to the composer.json first):

    [code]composer require –dev “GitHubUser/OriginalPackageName:dev-BranchName”[/code]

    2. When requiring a package from a forked git repository, [b]you need to use the original package name defined in package.json, [i]not[/i] the name of the fork![/b]

Leave a Reply

Please use [code] and [/code] around any source code you wish to share.

This site uses Akismet to reduce spam. Learn how your comment data is processed.