Installing Composer overview

 

Overview

This article walks you through installing Composer on a DreamHost web server.

Background

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, which 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

Change the default PHP CLI your shell uses

See this article if you wish to have your commands default to a specific version of PHP.

How to install Composer

The following sections explain how to install locally or globally on your web server.

Installing Locally

Installing Composer locally is recommended 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. For example, 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 this message:

    Composer successfully installed to: /home/username/example.com/project/composer.phar 
    Use it: php composer.phar
    

Installing Globally

Installing globally is recommended if you will be using Composer with multiple projects.

  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 use the directory ~/.php/composer.
    [server]$ mkdir -p ~/.php/composer
  4. Change into 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. Add the following line to the .bash_profile file located in your user's home directory. Replace username with your shell username.
  7. export PATH=/home/username/.php/composer:$PATH
  8. Save the file and close it.
  9. Update your .bash_profile:
    [server]$ . ~/.bash_profile
  10. Rename the composer file for easier handling.
    [server]$ mv ~/.php/composer/composer.phar ~/.php/composer/composer

Using Composer

You would invoke Composer differently depending if you installed it locally or globally.

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

Locally

If you installed Composer locally, invoke it while in the project directory.

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

Globally

If you installed Composer globally, invoke it using this syntax from any directory.

[server]$ composer <composer-command>

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?