Installing Composer overview

Overview

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
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:

Installing Composer official site

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

    If successful, you'll see the following message:

    #!/usr/bin/env php All settings correct for using Composer
    Downloading... 
    
    Composer successfully installed to: /home/username/example.com/project/composer.phar 
    Use it: php composer.phar
    

Install Globally

  1. Log into your server via SSH.
  2. Navigate to your user's 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 located in your user's home directory. Add the following path to the .bash_profile 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.

Troubleshooting

You may notice the following error when running composer:

PHP Fatal error: Uncaught Error: Class 'Phar' not found

Phar is already enabled by default, so this error usually means there are lines in your phprc file that reference Phar which should be removed. First, check which version of PHP your Shell is using.

[server]$ php -v

Next, check the phprc file for that version. For example, if your Shell is using PHP 8.3, run the following:

[server]$ cat ~/.php/8.3/phprc

If you see any lines mentioning Phar, remove them, then kill off any processes to update the file.

See also

Did this article answer your questions?

Article last updated PST.

Still not finding what you're looking for?