Tips & Tricks with Composer

I'll be updating this post with new tips and tricks about composer as my personal list of useful commands and configurations.


Install composer package globally

composer global require "codeception/codeception:*"  

Remove dev dependencies

composer update --no-dev  

We could use the tag --no-dev when installing our dependencies and avoid the dev ones

Avoid API rate limit and OAuth tokens

composer config -g github-oauth.github.com <oauthtoken>  

Composer troubleshooting guide

Install a single library with a specific version and stability flag

composer require doctrine/doctrine-fixtures-bundle:2.1.*@dev  

Update a single library with composer

composer update doctrine/doctrine-fixtures-bundle  

How to install a composer new dependency without updating old ones

 Composer create-project command

Create a project from branch

dev-v/1.7 will be the branch we want to install.

composer create-project block8/phpci /opt/phpci --keep-vcs --no-dev "dev-v/1.7"  
Create a project from private repo

The option --repository-url= will indicate where is our private repo form where create the new project

composer create-project block8/phpci /opt/phpci --keep-vcs --no-dev --repository-url=http://repo.yourcomposerrepo.com  

--repository-url= will be looking for a package.json file if there is non on that address it will throw an error

Composer create project from private repo

Global configuration

You can add to your $COMPOSER_HOME a file called config.json with your default composer configuration to be used across all your projects.

config.json

{
    "config": {
        "preferred-install": "dist",
        "github-oauth": {
            "github.com": "jisaf7y783oh32ho3h891h1e2h8iijfsi8"
        }
    }
}

In the example below preferred-install dist option will be use and a specific GitHub token will be use for authentication with the domain github.com

Edit your vendor dependencies directly

For practical reasons, you may prefer cloning sources instead of downloading packages. For instance, this can be useful to edit a library directly in the vendor directory to test the behaviour of your application with that change. The --prefer-source option will force cloning sources instead of downloading an archive:

$ composer update me/repo --prefer-source

Then to see modified files in your vendor:

$ composer status -v
You have changes in the following dependencies:  
/path/to/app/vendor/me/repo/Laravel/Request:
    M Request.php

Composer will also tells you when you try to update a vendor that has been modified, and asks if you want to discard the changes:

$ composer update
Loading composer repositories with package information  
Updating dependencies  
  - Updating me/repo v1.2.0 (v1.2.0- => v1.2.0)
    The package has modified files:
    M Request.php
    Discard changes [y,n,v,s,?]?

Prefer source to edit your vendors

Installing package from local git repository

"repositories": [
    {
        "type":"vcs",
        "url":"/home/my-repository"
    }
],
"require":{
     "me/my-repository":"dev-master"
}

If your package is published on packagist, you need to add --prefer-source option to force installation from VCS.

Installing package from local git repository

Install package from a development branch (forked project)

"repositories": [
    {
        "type":"vcs",
        "url":"http:github.com/me/one-repository"
    }
],
"require":{
     "other/one-repository":"dev-master"
}

Notice: the differences between me/one-repository and other/one-repository

There is another way to achieve this using inline-aliases in our composer.json file like:

composer.json

"require" : {
    "other/repository": "dev-my-branch as the-other-expected-branch"
}

Have composer use development branches


Resources