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 RHEL based distribution like Alma/Rocky Linux.
Also check:
- How to Setup Opencart with LAMP (PHP, Apache, Mariadb) on Ubuntu 20.04
- How to install and set up PHP and Apache(LAMP stack) on Ubuntu 20.04
Installing
To install Composer in Rocky/Alma Linux, follow these steps:
- Ensure your packages are up to date
- Ensure php cli is installed
- Download the installer script
- Verif the installation script
- 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.