Composer is a dependency manager for PHP (similar to npm for Node.js or pip for Python).

In this tutorial, we will go through the steps of installing and using Composer on a CentOS 7 machine.

Ensure that you have met the following prerequisites before continuing with this tutorial:

  • Logged in user with sudo privilege
  • Have PHP 7 installed on your CentOS 7 system.

Installing Composer on CentOS

The following steps describe how to install Composer on a CentOS 7 system.

01. First install the PHP CLI (command line interface) package and all other dependencies with:

sudo yum install php-cli php-zip wget unzip

02. Once PHP CLI is installed, download the Composer installer script with:

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"

The command above will download the composer-setup.php file in the current working directory.

03. To verify the data integrity of the script compare the script SHA-384 hash with the latest installer hash found on the Composer Public Keys / Signatures page.

The following wget command will download the expected signature of the latest Composer installer from the Composer’s Github page and store it in a variable named HASH:

HASH="$(wget -q -O - https://composer.github.io/installer.sig)"

To verify that the installation script is not corrupted run the following command:

php -r "if (hash_file('SHA384', 'composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"

If the hashes match, the following message will be shown:

Installer verified

 

04. Run the following command to install Composer in the /usr/local/bin directory:

sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer

Output:
All settings correct for using Composer
Downloading...

Composer (version 1.8.6) successfully installed to: /usr/local/bin/composer
Use it: php /usr/local/bin/composer

The composer is installed as a system-wide command and it will be available for all users.

05. The last step is to verify the installation:

composer

If  cannot verify composer

Output:

bash: composer: command not found

Move composer to /usr/bin/composer

mv /usr/local/bin/composer /usr/bin/composer

So, use can verify the installation