In this tutorial, we will learn how to install and Use PHP Composer on CentOS 7 and 8.
A composer is a tool that manages the dependency in PHP. Composer allows the user to declare the libraries inside the project. Composer automatically installs and updates these dependencies for you.
The composer will pull all the required PHP packages, depending on the project dependencies. Composer supports all the latest PHP platforms and frameworks like Magento, Laravel, etc.
Prerequisites
- Root user Or Other user with sudo privileges.
- Server with CentOS 7/8 operating system.
Installing Composer on CentOS
To install Composer on the server, run the following command.
- Install the DNF package
yum install dnf -y
- Install PHP command -line interface and other dependencies
sudo dnf install php-cli php-json php-zip wget unzip -y
- After installing the PHP command -line interface, download the Composer installer script.
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
Verify the Composer installer script by comparing the file’s SHA-384 hash with the original hash, which is found on the Composer Public Keys / Signatures page.
- Run the following command to download the latest Composer installer’s signature from the Composer’s GitHub page, and store the signature in the variable named as HASH.
HASH="$(wget -q -O - https://composer.github.io/installer.sig)"
- To verify the script is fine, run the following command to verify.
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 installation script is not corrupted, then it will show the following output.
Installer verified
If the installation script is corrupted, then it will show the following output.
Installer corrupt
- 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
The above command installed composer globally on the server. Every user on the server can access the composer command.
The output will look like this.
All settings correct for using Composer Downloading... Composer (version 2.1.9) successfully installed to: /usr/local/bin/composer Use it: php /usr/local/bin/composer
- To verify the Composer installation, run the following command.
composer -V
The output will look like this.
Composer version 2.1.9 2021-10-05 09:47:38
- If you face the following issue, then you use need to create a symbolic link from /usr/local/bin/composer to /usr/bin/composer.
To create the symbolic link, run the following command.
ln -s /usr/local/bin/composer /usr/bin/composer
Installing Composer on CentOS (Method 2)
If the above installation command does not work, you can use these commands to install the composer on the server.
- Install PHP command -line interface and other dependencies.
sudo dnf install php-cli php-json php-zip curl unzip -y
- Download the composer with the curl command.
curl -sS https://getcomposer.org/installer |php
- Move the downloaded Composer file in the
/usr/local/bin
directory.
sudo mv composer.phar /usr/local/bin/composer
- To verify the Composer installation, run the following command.
composer -V
The output will look like this.
Composer version 2.1.9 2021-10-05 09:47:38
Getting Started with Composer
After installing the Composer on the server, we will learn how to use the Composer command inside a project.
- Create and open the project
mkdir ~/demo-project cd ~/demo-project
In this project, we will use a PHP package laravel
to create a simple application.
composer require laravel/installer
- The above command will create some files.
ls -l
-rw-r--r--. 1 root root 63 Oct 27 08:31 composer.json -rw-r--r--. 1 root root 34770 Oct 27 08:30 composer.lock drwxr-xr-x. 7 root root 94 Oct 27 08:30 vendor
composer.json – This file describes your project. It includes the PHP dependencies and other metadata.
composer.lock – It is the file that contains the list of all the installed packages with their version.
Vendor – This directory contains all the dependencies of the project.
- To update the PHP packages of your project, run the following command.
composer update
The above command will check the latest version of the PHP packages and install it if the version constraint match with the one specified in the composer.json file.
Conclusion
In this Install PHP Composer on CentOS tutorial, we have learned how to install and use the Composer command in CentOS 7/8. We have also seen how to use Composer to create a PHP project. You can get a brief idea about the Composer from their official page. To visit Composer’s official page.
If you guys have any queries related to this tutorial, then let me know in the comment section.
Leave a Reply