Composer is an essential tool for PHP developers, serving as a robust dependency manager. It facilitates the downloading and installation of all necessary PHP packages required for your projects from the packagist.org repository. Utilized by modern PHP frameworks like Laravel, Symfony, Drupal, and Magento 2, Composer streamlines the management of project dependencies.
This guide will walk you through the process of installing and using Composer on Alma Linux 8.
Prerequisites
- A server running Alma Linux 8.
- Root access to the server.
Install Required Packages
Before proceeding with the Composer installation, ensure that PHP dependencies are installed. Execute the following command:
dnf install php-cli php-json php-zip wget unzip -y
After successful installation of these packages, proceed to the next steps.
Download Composer Installation Script
The simplest method to install Composer is through its installer script. Download it with the command below:
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
This command will download the composer-setup.php file to your current working directory.
Verify the Installation Script
To ensure that the installer script is not corrupted, verify its integrity 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 the script is valid, you will see:
Installer verified
Install Composer on Alma Linux 8
To install Composer into the /usr/local/bin
directory, execute the following command:
php composer-setup.php --install-dir=/usr/local/bin --filename=composer
Expected 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
Verify the installation with:
composer -V
It should yield a version output similar to:
Composer version 2.2.4 2022-01-08 12:30:42
Working with Composer
Let’s explore using Composer in a PHP project. Begin by creating a new project:
mkdir project
Navigate into your project directory and install the Carbon package:
cd project composer require nesbot/carbon
During installation, you should see output similar to:
- 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!
A composer.json file and all necessary dependencies will be populated within your project directory. List the files with:
ls -l
The output will include:
-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, craft a simple PHP application. Create a file called myapp.php:
nano myapp.php
Insert the following code into the file:
<?php require __DIR__ . '/vendor/autoload.php'; use Carbon\Carbon; printf("Now: %s", Carbon::now());
Save and close the file. Execute your application:
php myapp.php
The output should display the current timestamp:
Now: 2022-01-09 06:02:17
Conclusion
This guide covered the installation and usage of Composer on Alma Linux 8. By leveraging Composer, you can efficiently manage dependencies, enhancing your PHP development workflow.
Frequently Asked Questions (FAQ)
What is Composer?
Composer is a dependency management tool for PHP. It allows developers to manage the libraries their projects depend on with ease.
Which PHP frameworks use Composer?
Composer is widely used in popular PHP frameworks such as Laravel, Symfony, Drupal, and Magento 2.
Can Composer be used to manage project versions?
Yes, Composer also allows you to easily manage and maintain version control for your project’s dependencies.
How does Composer manage dependencies?
Composer uses a file named composer.json to manage dependencies, specifying which libraries and versions are required for a project.