Composer is a popular dependency manager for PHP, allowing developers to manage libraries and packages in their projects efficiently. This guide will walk you through the steps to install and use Composer on CentOS 8.
Step 1: Update the System
Before installing Composer, ensure that your system packages are up to date. Open your terminal and run the following commands:
sudo dnf update -y
Step 2: Install Required Dependencies
Composer requires several packages to run. Use the following command to install the necessary tools:
sudo dnf install curl php-cli php-mbstring unzip -y
- curl: Used to download Composer’s installer script.
- php-cli: The command-line interface for PHP.
- php-mbstring: A PHP extension needed for handling multi-byte strings.
- unzip: Required for extracting Composer’s installation package.
Step 3: Download and Install Composer
Next, download the Composer installer script using curl:
curl -sS https://getcomposer.org/installer -o composer-setup.php
To verify the integrity of the downloaded script, run the following command to fetch the hash:
HASH=$(curl -sS https://composer.github.io/installer.sig)
Now, verify the downloaded installer against the hash:
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 installer is verified, you will see the message “Installer verified.” If not, you should remove the corrupt installer.
Now, install Composer globally so it can be accessed from anywhere on your system:
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
You can verify the installation by checking the Composer version:
composer --version
Step 4: Using Composer
With Composer installed, you can now use it to manage PHP dependencies.
Creating a New Project
- Create a Directory: Navigate to your project’s directory or create a new one:
mkdir my_project && cd my_project
- Initialize Composer: To create a
composer.json
file, run:
composer init
Follow the prompts to set up your project information and dependencies.
Installing Dependencies
To install a package from Packagist (the default package repository for Composer), use the require
command. For example, to install the monolog/monolog
logging library, run:
composer require monolog/monolog
This will download the package and its dependencies into the vendor/
directory and update the composer.json
file.
Updating Dependencies
To update your project’s dependencies, simply run:
composer update
This command will update all dependencies to the latest versions according to the version constraints defined in your composer.json
.
Removing Dependencies
To remove a package from your project, use the remove
command:
composer remove vendor/package_name
This command will uninstall the specified package and update the necessary files.
Step 5: Autoloading with Composer
One of Composer’s features is its ability to autoload classes from installed packages. To set this up, include the following line in your PHP scripts:
require 'vendor/autoload.php';
This statement will automatically load the installed packages without the need to manually include them.
Conclusion
You have successfully installed and configured Composer on your CentOS 8 server. With Composer, managing PHP dependencies becomes much easier, allowing for efficient development and project management. Be sure to regularly update your Composer and packages to maintain security and performance. For more advanced usage, refer to the Composer documentation.