PHP Composer is a dependency manager for PHP, facilitating the installation and management of PHP packages. It’s essential for modern PHP development. This article guides you through installing and using PHP Composer on CentOS/RHEL 9/8.

Advertisement

Prerequisites

  • A CentOS/RHEL 9/8 system
  • Command-line access with sudo privileges
  • Basic knowledge of PHP and Linux commands

Step 1: Installing PHP

Before installing Composer, ensure PHP is installed. Use the following command to install PHP:

sudo dnf install php php-cli php-json 

Step 2: Downloading Composer

You can download Composer using curl. First, ensure curl is installed:

sudo dnf install curl 

Then, download the Composer installer script:

curl -sS https://getcomposer.org/installer | php 

Step 3: Installing Composer Globally

To use Composer globally, move the composer.phar file to a global path:

sudo mv composer.phar /usr/local/bin/composer 

Make the file executable:

chmod +x /usr/local/bin/composer 

Verify the installation:

composer --version 

Step 4: Configuring PHP for Composer

Adjust your PHP configuration if necessary. Composer requires a certain amount of memory and suitable settings. Edit the php.ini file:

sudo nano /etc/php.ini 

Ensure these settings are adequately configured:

  • memory_limit (Composer recommends 2GB)
  • allow_url_fopen = On

Step 5: Using Composer

To start using Composer, navigate to your PHP project directory and run:

composer init 

This command will guide you through creating a composer.json file for your project.

Step 6: Installing PHP Packages

To install PHP packages, use:

composer require [package-name] 

This command adds the package to your composer.json file and installs it. All the packages files are stored under ./vendor directory in project root directory.

Step 7: Updating Packages

To update your PHP packages to their latest versions:

composer update 

Step 8: Autoloading

Composer provides an autoloader for your project’s dependencies. Include the autoloader in your PHP scripts:


<?php
require 'vendor/autoload.php';

//Your application code here

Conclusion

Composer is a powerful tool for managing PHP dependencies. By following these steps, you can successfully install and use Composer on CentOS/RHEL 9/8. It simplifies the process of managing PHP packages, ensuring your project is up-to-date and efficient.

Share.

1 Comment

Exit mobile version