How to Install and Use PHP Composer on CentOS 8

Published on

5 min read

How To Install and Use Composer on CentOS 8

Composer is a dependency manager for PHP (as npm is to Node.js or pip is to Python).

Composer will pull in all the required PHP packages your project depends on and manages them for you. It is used in all modern PHP frameworks and platforms such as Laravel, Symfony, Drupal, and Magento 2.

This tutorial goes through the steps of installing Composer on CentOS 8. If you are in a hurry and you don’t want to verify the file integrity, scroll down to the Installing Composer - Quick Way section.

Prerequisites

Ensure that you have met the following requirements before continuing:

Installing Composer on CentOS

Perform the following steps to install Composer on CentOS 8.

  1. Install the PHP CLI (command-line interface) package and all other dependencies with:

    sudo dnf install php-cli php-json php-zip wget unzip
  2. Once PHP CLI is installed, download the Composer installer script:

    php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"

    The command above downloads a file named composer-setup.php in the current working directory .

  3. Verify the data integrity by comparing the file’s SHA-384 hash with the hash found on the Composer Public Keys / Signatures page.

    The following wget command downloads the signature of the latest Composer installer from the Composer’s Github page, and store it in a variable named HASH:

    HASH="$(wget -q -O - https://composer.github.io/installer.sig)"

    To verify that the installation script is not corrupted run the following command:

    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 hashes match, the following message will be shown:

    Installer verified

    Otherwise, if the hashes don’t match, you will see Installer corrupt. Once the integrity is verified, continue with the next step.

  4. Run the following command to install Composer in the /usr/local/bin directory:

    sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer

    The command above installs composer as a system-wide command and available for all users. The output will look something like this

    All settings correct for using Composer
    Downloading...
    
    Composer (version 1.10.1) successfully installed to: /usr/local/bin/composer
    Use it: php /usr/local/bin/composer

    The command above installs composer as a system-wide command and available for all users.

  5. Verify the installation by printing the Composer’s version:

    composer -V
    Composer version 1.10.1 2020-03-13 20:34:27

At this point, you have successfully installed Composer on your CentOS system, and you can start using it.

Installing Composer [Quick Way]

Perform the following steps to quickly install Composer on your CentOS 8 system:

  1. Install PHP CLI and Zip:

    sudo dnf install php-cli php-json php-zip curl unzip
  2. Download Composer with curl:

    curl -sS https://getcomposer.org/installer |php
  3. Move the Composer file to /usr/local/bin directory:

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

Getting Started with Composer

Now that Composer is installed on your CentOS system, we will show you how to use Composer in a PHP project.

Start by creating the project root directory and navigate to it :

mkdir ~/my-first-composer-projectcd ~/my-first-composer-project

In this example, we’ll use a PHP package called carbon to create a sample application that prints the current time.

Run the following command to initialize a new Composer project and install the carbon package:

composer require nesbot/carbon
sing version ^2.32 for nesbot/carbon
./composer.json has been created
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 4 installs, 0 updates, 0 removals
  - Installing symfony/translation-contracts (v2.0.1): Downloading (connecting..Downloading (100%)         
  - Installing symfony/polyfill-mbstring (v1.15.0): Downloading (100%)         
  - Installing symfony/translation (v5.0.6): Downloading (100%)         
  - Installing nesbot/carbon (2.32.1): Downloading (100%)         
symfony/polyfill-mbstring suggests installing ext-mbstring (For best performance)
symfony/translation suggests installing symfony/config
symfony/translation suggests installing symfony/yaml
symfony/translation suggests installing psr/log-implementation (To use logging capability in translator)
Writing lock file
Generating autoload files
3 packages you are using are looking for funding.
Use the `composer fund` command to find out more!

As you can see from the output above, Composer creates a file named composer.json and downloads carbon and all its dependencies.

If you list your project’s directory , you will see that it contains two files composer.json and composer.lock, and a vendor directory.

ls -l
-rw-rw-r--. 1 vagrant vagrant    60 Mar 27 18:05 composer.json
-rw-rw-r--. 1 vagrant vagrant 11135 Mar 27 18:06 composer.lock
drwxrwxr-x. 6 vagrant vagrant    82 Mar 27 18:06 vendor
  • vendor is the directory where the project dependencies are stored.
  • The composer.lock file contains a list of all installed packages, including their versions.
  • composer.json is a file that describes your PHP project, including the PHP dependencies and other metadata.
You can search the Composer repository Packagist for PHP packages.

Composer has autoload capabilities, that allow you to use PHP classes without the need of the require or include statements.

Create a file named testing.php and add the following code:

<?php

require __DIR__ . '/vendor/autoload.php';

use Carbon\Carbon;

printf("Now: %s", Carbon::now());

Let’s analyze the code line by line.

In the first line after the opening php tag we include the vendor/autoload.php file that autoloads all of the libraries.

Next, we are aliasing Carbon\Carbon as Carbon. The last line prints the current time using the Carbon now method.

Run the script by typing:

php testing.php

The output should look something like this:

Now: 2020-03-27 22:12:26

Later on, if you want to update your PHP packages you can simply run:

composer update

The command above will check for newer versions of the installed packages, and if a newer version is found and the version constraint match with the one specified in the composer.json, Composer will update the package.

Conclusion

You have learned how to install Composer on your CentOS 8 machine. We have also shown you how to use Composer to create a basic PHP project.

To find more information about Composer, visit the official Composer documentation page.

If you have any questions, please leave a comment below.