Installing Composer overview

You must use a VPS in order to run Composer due to the amount of resources it requires.

Composer is a dependency management tool for PHP projects. It allows you to declare the libraries your project requires and installs/updates them for you.

It is deployed as a PHAR (PHP Archive) file. Phar is already enabled by default on your DreamHost server. You can confirm by running the following command via SSH:

[server]$ php -m | grep Phar

Some PHP projects are now suggesting that Composer be used to manage external libraries during installation. MediaWiki is one such example, stating in their installation instructions:

Change the default PHP CLI your shell uses

If you wish to have your commands default to a specific version of PHP, view the following article:

This example uses PHP 5.6

Installing Composer

View the following link for further details:

Install Locally

Installing Composer locally is good if you only have one project to maintain.

  1. Log into your server via SSH.
  2. Navigate to your project directory.
    [server]$ cd ~/<path-to-your-project>
    Replace <path-to-your-project> with the path to your project. If the project directory is named "project" and it's on the domain "example.com", the path would be "example.com/project".
  3. Run the Composer installer.
    [server]$ curl -sS https://getcomposer.org/installer | php
    If that does not work, try this instead
    [server]$ php -r "readfile('https://getcomposer.org/installer');" | php
  4. If successful, you'll see the following message:
    #!/usr/bin/env php All settings correct for using Composer Downloading... Composer successfully installed to: /home/example_username/example.com/project/composer.phar 
    
    Use it: php composer.phar
    

    This downloads the 'composer.phar' file.

Install Globally

  1. Log into your server via SSH.
  2. Navigate to your home directory.
    [server]$ cd ~
  3. Make a directory for installation. You can choose any valid directory name, but these instructions will assume the directory "~/.php/composer".
    [server]$ mkdir -p ~/.php/composer
  4. Navigate to the installation directory.
    [server]$ cd ~/.php/composer
  5. Run the Composer installer.
    [server]$ curl -sS https://getcomposer.org/installer | php

    If that does not work, try this instead:

    [server]$ php -r "readfile('https://getcomposer.org/installer');" | php
  6. View the creating and editing a file via SSH article for instructions on how to edit your existing .bash_profile. Then add the following path for the composer.phar file.
    export PATH=/home/<username>/.php/composer:$PATH
    Replace <username> with your shell username
  7. Save the file and close it.
  8. Run the following to update your .bash_profile:
    [server]$ . ~/.bash_profile
  9. Rename the composer file for easier handling.
    [server]$ mv ~/.php/composer/composer.phar ~/.php/composer/composer

Using Composer

If you installed Composer locally, then you would invoke Composer like this while in the project directory:

[server]$ php composer.phar <composer-command>

If you installed Composer globally, you should be able to invoke Composer this way:

[server]$ composer <composer-command>

In either case, replace <composer-command> with the composer command you want to run. Composer commands can be found listed in the Composer documentation.

See also

Did this article answer your questions?

Article last updated PST.