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.
- Log into your server via SSH.
- 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.
- 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
-
If that does not work, try this instead
Installing Globally
Installing globally is recommended if you will be using Composer with multiple projects.
- Log into your server via SSH.
- Navigate to your user's home directory.
[server]$ cd ~
- 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
- Change into the installation directory.
[server]$ cd ~/.php/composer
- 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
-
- Add the following line to the .bash_profile file located in your user's home directory. Replace username with your shell username.
-
export PATH=/home/username/.php/composer:$PATH
- Save the file and close it.
- Update your .bash_profile:
[server]$ . ~/.bash_profile
- 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.