How to Install and Use PHP Composer on CentOS 7

By 

Updated on

5 min read

How To Install and Use Composer on CentOS 7

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

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

In this tutorial, we will go through the steps of installing and using Composer on a CentOS 7 machine.

Prerequisites

Ensure that you have met the following prerequisites before continuing with this tutorial:

Installing Composer on CentOS

The following steps describe how to install Composer on a CentOS 7 system.

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

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

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

    The command above will download the composer-setup.php file in the current working directory .

  3. To verify the data integrity of the script compare the script SHA-384 hash with the latest installer hash found on the Composer Public Keys / Signatures page.

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

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

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

    Terminal
    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:

    output
    Installer verified

    If the hashes don’t match you will see Installer corrupt. In this case, you need to redownload the Composer installation script and double check the value of the $HASH variable with echo $HASH . Once the installer is verified, you can continue with the next step.

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

    Terminal
    sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
    output
    All settings correct for using Composer
    Downloading...
    
    Composer (version 1.8.5) successfully installed to: /usr/local/bin/composer
    Use it: php /usr/local/bin/composer

    The composer is installed as a system-wide command and it will be available for all users.

  5. The last step is to verify the installation:

    Terminal
    composer

    The command above will print the Composer’s version, commands, and arguments.

    output
    ______
    / ____/___  ____ ___  ____  ____  ________  _____
    / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
    / /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
    \____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                        /_/
    Composer version 1.8.5 2019-04-09 17:46:47
    
    Usage:
        command [options] [arguments]

    At this point, you have successfully installed Composer on your CentOS system.

Tip
If you want to have a per-project Composer installation use the following command php composer-setup.php which will create a file named composer.phar in your current working directory. You can use the composer by running ./composer.phar <command>.

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 a directory that will be the project root directory and hold the composer.json file.

composer.json file describes your PHP project including the PHP dependencies and other metadata.

Create the project directory and switch to it with:

Terminal
mkdir ~/my-first-composer-project
cd ~/my-first-composer-project

In this example, we will create a sample application that will print the current time using a package called carbon .

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

Terminal
composer require nesbot/carbon
output
Using version ^2.2 for nesbot/carbon
./composer.json has been created
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 3 installs, 0 updates, 0 removals
  - Installing symfony/polyfill-mbstring (v1.9.0): Downloading (100%)
  - Installing symfony/translation (v4.1.4): Downloading (100%)
  - Installing nesbot/carbon (2.2.0): Downloading (100%)
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

As you can see from the output above Composer will create the composer.json file and it will download and install 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.

Terminal
ls -l
output
-rw-r--r-- 1 linuxize users   60 Aug 29 00:50 composer.json
-rw-r--r-- 1 linuxize users 6.8K Aug 29 00:50 composer.lock
drwxr-xr-x 5 linuxize users 4.0K Aug 29 00:50 vendor
  • The vendor directory is the directory where the project dependencies are stored.
  • The composer.lock contains a list of all installed packages including their versions.
  • The composer.json describes the PHP project and all PHP dependencies.
Tip
You can search the Composer repository Packagist for PHP packages.

Composer provides autoload capabilities which allow us to use PHP classes without the need to use require or include the files.

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

php
<?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 are including the vendor/autoload.php file that was automatically generated by Composer which will autoload all of the libraries.

Next, we are aliasing Carbon\Carbon as Carbon and in the last line we are printing the current time using the Carbon now method.

Run the script by typing:

Terminal
php testing.php

The output should look something like this:

output
Now: 2018-09-26 20:08:18

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

Terminal
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 7 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.

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page