How to Install and use PHP Composer on AlmaLinux 8
Composer is a dependency manager for PHP that allows you to download and install all the required PHP packages needed for your project. It is a command-line tool that installs all libraries and dependencies for your project from the packagist.org repository. It is used in modern PHP frameworks such as Laravel, Symfony, Drupal, and Magento 2.
In this post, we will show you how to install and use Composer on Alma Linux 8.
Prerequisites
- A server running Alma Linux 8.
- A root password is configured on your server.
Install Required Packages
Before installing Composer, you will need to install PHP dependencies required to install Composer. You can install all of them by running the following command:
dnf install php-cli php-json php-zip wget unzip -y
Once all the packages are installed, you can proceed to the next step.
Download Composer Installation Script
The simple and easiest way to install the Composer is to install it from the installer script. You can download it using the following command:
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
This will download the composer-setup.php file to your current working directory.
Verify the Installation Script
After downloading the installer script, you will need to verify it whether it is corrupted or not. You can verify it using the following 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 everything is fine, you will get the following output:
Installer verified
Install Composer on Alma Linux 8
Finally, install the Composer to the /usr/loca/bin directory using the following command:
php composer-setup.php --install-dir=/usr/local/bin --filename=composer
Output:
All settings correct for using Composer Downloading... Composer (version 2.2.4) successfully installed to: /usr/local/bin/composer Use it: php /usr/local/bin/composer
You can now verify the installation using the following command:
composer -V
You should see the following output:
Composer version 2.2.4 2022-01-08 12:30:42
Working with Composer
In this section, we will show you how to use Composer in a PHP project.
First, create a project using the following command:
mkdir project
Next, navigate to the project directory and install the carbon package with the following command:
cd project
composer require nesbot/carbon
Output:
- Downloading symfony/translation-contracts (v2.5.0) - Downloading symfony/polyfill-php80 (v1.24.0) - Downloading symfony/polyfill-mbstring (v1.24.0) - Downloading symfony/deprecation-contracts (v2.5.0) - Downloading symfony/translation (v5.4.2) - Downloading nesbot/carbon (2.55.2) - Installing symfony/translation-contracts (v2.5.0): Extracting archive - Installing symfony/polyfill-php80 (v1.24.0): Extracting archive - Installing symfony/polyfill-mbstring (v1.24.0): Extracting archive - Installing symfony/deprecation-contracts (v2.5.0): Extracting archive - Installing symfony/translation (v5.4.2): Extracting archive - Installing nesbot/carbon (2.55.2): Extracting archive 3 package suggestions were added by new dependencies, use `composer suggest` to see details. Generating autoload files 6 packages you are using are looking for funding. Use the `composer fund` command to find out more!
This will create a composer.json file and install carbon with all dependencies to the project directory.
You can list all files with the following command:
ls -l
You will get the following output:
-rw-r--r-- 1 root root 60 Jan 9 06:01 composer.json -rw-r--r-- 1 root root 18538 Jan 9 06:01 composer.lock drwxr-xr-x 6 root root 82 Jan 9 06:01 vendor
Next, create a myapp.php file and add the following code:
nano myapp.php
Add the following code:
<?php require __DIR__ . '/vendor/autoload.php'; use Carbon\Carbon; printf("Now: %s", Carbon::now());
Save and close the file then run your application using the following command:
php myapp.php
You will get the following output:
Now: 2022-01-09 06:02:17
Conclusion
In this guide, we explained how to install Composer on Alma Linux 8. We also explained how to interact with Composer in your PHP project. I hope this will help you to speed up your PHP applications