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.
- 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. 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 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
- 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 will assume the directory ~/.php/composer.
[server]$ mkdir -p ~/.php/composer
- Navigate to 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
- 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
- Save the file and close it.
- Run the following to 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
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.