Skip to content

How to Install and Use Composer on CentOS/RHEL 9/8

This detailed guide offers an in-depth look at installing and utilizing PHP Composer on CentOS/RHEL 9/8 systems. Composer Setup on CentOS/RHEL

Steps
8
Category
PHP
Covers
CentOS, Composer, RHEL
Versions & support
About PHP →, About CentOS →
On this page 8 steps

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.

Prerequisites

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

Step 1 of 8Installing 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 of 8Downloading 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 of 8Installing 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 of 8Configuring 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 of 8Using 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 of 8Installing 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 of 8Updating Packages

To update your PHP packages to their latest versions:

composer update 

Step 8 of 8Autoloading

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.

Rahul

Rahul Kumar is the founder and chief editor of TecAdmin.net. A Red Hat Certified Engineer (RHCE), he has worked in IT since 2009, with hands-on experience in Linux system administration, DevOps, web hosting, cloud platforms and automation. He started TecAdmin in 2013 to share practical, step-by-step guides, and leads its editorial work with one aim: making technical learning clear and useful for sysadmins, developers and beginners alike.

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *


Back to top