Linux

How to Install and Use php Composer in Rocky Linux/Centos 8

Pinterest LinkedIn Tumblr

Composer is an application-level package manager for the PHP programming language that provides a standard format for managing dependencies of PHP software and required libraries. It is similar to pip for python.

This guide shows how to install and use composer in a centos 8 server. It also work on Similar OS like Rhel 8, Alma Linux and Rocky Linux.

Also check:

Installing

To install Composer in Rocky Linux/Centos 8, follow these steps:

  1. Ensure your packages are up to date
  2. Ensure php cli is installed
  3. Download the installer script
  4. Verif the installation script
  5. Install Composer

1. Ensure your packages are up to date

Before proceeding, let us ensure that our OS packages are up to date. Use this command:

sudo dnf update -y

2. Ensure php cli is installed

The package php-cli gives you access to the command php in the command line. This command will allow us to execute php scripts.

Run this command to install php cli with other depencies packages

sudo dnf install -y \
    php-cli \
    php-json \
    php-zip \
    wget \
    unzip

3. Download the installer script

Lets use the php cli to download and run the composer set up script:

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

The downloaded script will be saved as composer-setup.php in the current dir

4. Verify the installation script

To verify that the installation script is not corrupted, use this command:

HASH="$(wget -q -O - https://composer.github.io/installer.sig)"
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

5. Installing Composer

Install Composer in the /usr/local/bin directory using this command:

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

6. Verify that the installation was successful

Verify the installation by printing the Composer’s version using this command in the terminal:

composer -V

Output:

➜ composer -V
Composer version 2.1.8 2021-09-15 13:55:14

7. Quick Way of installing php composer

You can also use these commands to download and install composer from its site.

First download dependencies:

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

Then download the composer.phar file:

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

Then move the file into executable scripts directory:

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

Finally confirm that composer is working as expected:

➜ composer -V
Composer version 2.1.8 2021-09-15 13:55:14

Conclusion

In this guide, we managed to install composer in our system. We can now use it to manage php dependencies.

I am a Devops Engineer, but I would describe myself as a Tech Enthusiast who is a fan of Open Source, Linux, Automations, Cloud and Virtualization. I love learning and exploring new things so I blog in my free time about Devops related stuff, Linux, Automations and Open Source software. I can also code in Python and Golang.