Run Composer installed binaries globally

cd-ing into a directory then running ./command, or worse yet, php command.php, sucks!
As an example, I wanted to run the doctrine command from anywhere.
I already have composer installed globally, and no longer have to type php composer.phar, but I haven’t blogged it since it was pretty easy, but anyway here it is:

curl -sS https://getcomposer.org/installer | php
 sudo mv composer.phar /usr/local/bin/composer

Then you can just type composer from anywhere. If you already have composer you should run sudo composer self-update. Anyway on to our php binaries (Doctrine, PHPUnit, Behat, you name it). If it’s a fresh install of composer you should see when you type composer global install:

Composer could not find a composer.json file in /home/username/.composer

Now we know where we can create our composer.json. Go to the sites for the packages you would like and copy paste the require field info; Here is Doctrine’s:

{
    "require":
    {
       "doctrine/dbal": "2.3.4",
       "doctrine/orm": "2.3.5"
    }
}

Now run composer global install and it will install in /home/username/.composer/vendor. The bin folder is inside that. Lastly we need to have our bin path set in our PATH environment variable, so the system knows to check that folder for a binary matching the command’s name. edit ~/.bashrc (it could be .bash_profile or something similar). At the end, paste this in:

export PATH=$PATH:/home/username/.composer/vendor/bin

now exit the shell and open a new terminal up and log back in. you can now type doctrine into the command line and lo and behold, your composer executables are rocking!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s