Pragmatism in the real world

Global installation of PHP tools with Composer

The Composer package manager along with the Packagist repository site is quickly becoming the defacto PHP package management system.

One feature I found out about recently is that you can install packages globally rather than locally into your project. I think that this is most useful for development tools, such as PHPUnit which are then available everywhere.

To install a composer package globally, you run the usual require command, but with the addition of the global modifier. So to install PHPUnit, you would run:

$ composer global require phpunit/phpunit
$ composer global require phpunit/dbunit
$ composer global require phing/phing
$ composer global require phpdocumentor/phpdocumentor
$ composer global require sebastian/phpcpd
$ composer global require phploc/phploc
$ composer global require phpmd/phpmd
$ composer global require squizlabs/php_codesniffer

This will install PHPUnit and all its dependencies into the ~/.composer/vendor/ directory and, most importantly, the phpunit CLI tools are installed into ~/.composer/vendor/bin/.

Simply add this directory to your PATH in your ~/.bash_profile (or ~/.bashrc) like this:

export PATH=~/.composer/vendor/bin:$PATH

and phpunit is now available on your command line.

To keep your tools up to date, you simply do this:

composer global update

To remove a package, you edit ~/.composer/composer.json and then run composer global update.

11 thoughts on “Global installation of PHP tools with Composer

  1. I also have "./vendor/bin" in my path, which is useful if projects depend on different versions of tools.

  2. Hey, thx for the hint!

    Unfortunately i can't use the "phpunit" command and i get this error:

    -bash: vendor/bin/phpunit: No such file or directory

    When i run phpunit with the full directory "~/.composer/vendor/bin/phpunit" it works? Am I missing something here?

    Thanks and cheers
    Christoph

    1. Hey,

      This is what worked for me. I added export PATH="~/.composer/vendor/bin:$PATH" to my ~/.bash_profile. Then I ran command source ~/.bash_profile.

  3. Global for the *user* you mean. What about global to the machine? If you have a build machine for instance that always needs phpunit, phpmd, etc..

    I guess that's why pear is still around, unfortunately it seems some authors are not keeping pear packages/release in step with composer.

  4. I was stuck after installing a composer package globally, but I was unable to run it. This was just the solution I was after. Thanks!

    The only step you forgot to mention was to run 'source ~/.bash_profile' to reload the profile config and be able to call the global package successfully.

  5. I installed the phpunit as mentioned above, but when i run the test, il returns always un error that PHPUnit_Framework_TestCase not found

    Anyone has an idea?

  6. Case dont exists the folder '~/.composer/vendor/bin' I do:
    if [ -d ~/.composer/vendor/bin ]; then
    export PATH=~/.composer/vendor/bin:$PATH
    fi

  7. Note:
    Sometimes path is .config/composer, instead of .composer/

    That was my case on Ubuntu 18.04.

  8. I usually install a global version of phpunit as well as a local one. The reason for this is that I am often using the local version like `composer tests` since I have a mapping in my composer.json file for `php -n ./vendor/bin/phpunit tests'`, but I also have setup a pre-commit hook and in this one I need to use the global package since I am only commiting code that has no development packages installed.

Comments are closed.