Global installation of Composer on windows

03 Sep 2012

I've been tinkering with composer recently (on windows) and I noticed there was no simple way to install it system wide. Typically the composer.phar would live within the root of your project directory and so you'd manage your dependencies from there.

But as I would like to manage several projects with it I thought it would be cleaner to have the composer.phar in one place. That way any updates to the composer.phar will only need to be done once. A system wide installation is outlined in the documentation for Linux, but there aren't any pointers for windows.

So to get around problem this I created a small .bat file and copied it to the same directory that my php binary exists, accompanied with the composer.phar file this now runs from anywhere.

To repeat the steps..

  1. Open a terminal and navigate to the directory your php binary is
    (or any other directory that exists under your system path)

  2. Run the following:
    php -r "eval('?>'.file_get_contents(''));"

  3. Create a composer.bat by running the following command:
    echo "@ECHO OFF" "php '%~dp0composer.phar' %*" > composer.bat


And your done (you may have to restart your terminal). Just type in "composer" and you should see the usage options.

Tip: If you're using windows powershell you might get the following warning;

'■@' is not recognized as an internal or external command,

If that's the case simply delete the composer.bat file, create a new file, copy in the following contents and save as composer.bat:

php "%~dp0composer.phar" %*
For some reason powershell will prefix the outfile with a special character. No idea how to get around this :/


comments powered by Disqus