Setting up PHP composer

This article walks through the simple steps for setting up composer. Initially at project level, then, optionally, at the global level.

Note: This article is for Mac(Unix)/Linux users, please refer the official docs for Windows.

What is Composer?

Composer is the dependency manager for PHP packets. In my opinion it should be in every web developer’s tootlkit, as it is somewhat likely that PHP will be used at some point and extremely likely that dependencies will be required within the projects.

Full documentation can be found on the official composer website, which is where the following steps have been taken from, for manually installing composer. Please refer to their documentation for programatic installation of composer.

Setting up Composer at project level

  1. Download Composer
    $ php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
  2. Verify the Download
    $ php -r "if (hash_file('SHA384', 'composer-setup.php') === '544e09ee996cdf60ece3804abc52599c22b1f40f4323403c44d44fdfdd586475ca9813a858088ffbc1f233e9b180f061') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
  3. Install Composer
    $ php composer-setup.php
  4. Delete the download file
    $ php -r "unlink('composer-setup.php');"

Making Composer globally accessible

To use composer in a global scope, simply move it to the usr/local/bin directory.

Note: If the following command fails, try it again with super user access; sudo before mv

  1. Move the Composer PHAR
    $ mv composer.phar /usr/local/bin/composer
  2. Verify installation & version (optional)
    $ composer --version
    // will display your composer version

You are now set to use composer. Simply type composer [commands + tags] in the terminal and enjoy.

Updating Composer

To update composer at any time, simply enter the following command:

$ composer self-update

To update composer dependencies, simply enter the following command (from the directory that contains the composer.json file):

$ composer update