A composer is a tool for dependency management in PHP. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you.
Install and use PHP composer on centOS 8; Through this tutorial, we will learn how to install and use PHP composer on CentOS 8.
Installing PHP Composer on CentOS 8
Use the following steps to install PHP composer on centOS 8:
- Step 1 – Install the PHP CLI
- Step 2 – Download Composer with curl
- Step 3 – Move Composer file
- Step 4 – Getting Started with Composer
Step 1 – Install the PHP CLI
First of all, open the command line or terminal and execute the following command into it to install PHP CLI on centOS 8:
sudo dnf install php-cli php-json php-zip curl unzip
Step 2 – Download Composer with curl
Once the PHP CLI installation is completed, then execute the following command on the command line or terminal to download composer with curl:
curl -sS https://getcomposer.org/installer |php
Step 3 – Move Composer file
Then move the composer file using the following command on centOS 8:
sudo mv composer.phar /usr/local/bin/composer
Step 4 – Getting Started with Composer
At this point, WE have successfully installed Composer on CentOS system. And now, we will learn how to use Composer in a PHP project.
Now, execute the following command on the command line or terminal to create PHP project using the composer command:
mkdir ~/my-first-composer-project cd ~/my-first-composer-project
Once the PHP project creation is done, then execute the following command to initialize a new Composer project and install the carbon package:
composer require nesbot/carbon
Then create a file named testing.php
and add the following code:
<?php
require __DIR__ . '/vendor/autoload.php';
use Carbon\Carbon;
printf("Now: %s", Carbon::now());
Finally, execute the following command n command line or terminal to test above created PHP project with the help of PHP composer:
php testing.php
The output should look something like this:
Now: 2022-08-10 22:12:26
Conclusion
Through this tutorial, we have learned how to install and use Composer on CentOS 8 machine. And also how to use Composer to create a basic PHP project.