CentOS

How to Install PHP Composer on CentOS 8

PHP Composer is a dependency manager used for managing all PHP project’s dependencies. It pulls all the needed PHP packages on which a project depends and manages them in a good way.  The composer is used in all modern platforms and PHP frameworks such as Magento 2, Laravel, Drupal, and Symfony. We will go through all steps for composer installation on CentOS 8 system in this tutorial. We elaborated two methods for PHP composer installation in this tutorial with proper implementation. Let us discuss both methods.

Prerequisites

The PHP should install on your CentOS 8 distribution. You can also install PHP on CentOS 8 from this URL.

Root or sudo privileges are needed for running the installation commands.

Install PHP composer on CentOS 8

By implementing the following steps, we can install PHP composer on CentOS 8 system:

Step 1: Install Required Dependencies

First, update all system packages and then, install all necessary dependencies including the PHP command-line interface package on CentOS 8 system by running the below-mentioned command:

$ sudo dnf install php-cli php-json php-zip wget unzip

Step 2: Download Composer Installer

Once the all above packages are downloaded, download the PHP composer installer script by using the following command:

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

After executing the above-mentioned command ‘composer-setup.php’ file is to be downloaded into the current working directory.

Step 3: Verify data integrity

Download the latest composer installer using the following wget command from the Github Composer’s page and store it into a ‘Hash’ variable:

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

Now, verify that the installation script is either running or not by typing the below-mentioned command on the terminal:

$ 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 after comparison, the hashes match it means the installer is verified and the following message will show on the terminal:

In another case, it shows a message ‘Installer corrupted’. Once your installer script identity is verified, move to the next step.

Step 4: Install Composer

In this step, you will install php composer on your system  by running the below-mentioned command:

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

The above command installs the PHP composer as system-wide and every user can use this command for composer installation. The following output receives on the terminal after executing the above command:

Now, verify the composer installation by displaying the installed version on the terminal window as follows:

$ composer -V

You will see that installed version displayed on the screen as follows:

The installation of PHP composer is completed at this point on your CentOS 8 system. Now, start the composer and use it.

Alternative method: Install PHP composer using a quick way

This method provides the quick method for Composer installation on CentOS 8 system. Perform the following few steps to install PHP composer:

Step 1. Install required PHP, zip, unzip and curl packages by using the following command:

$ sudo dnf install php-cli php-json php-zip curl unzip

Step 2. Using the curl command, download the composer installer.

$ curl -sS https://getcomposer.org/installer |php

Step 3. Now, Move all files of php Composer into the ‘/usr/local/bin’ directory by running the below-mentioned command:

$ sudo mv composer.phar /usr/local/bin/composer

Conclusion

We installed the PHP composer in this article on CentOS 8 system. We discuss two different methods for composer installation on CentOS 8. You can choose a method from both that more suits your needs.

 

Similar Posts