How To Install PHP Composer on a CentOS 7 VPS or Dedicated Server


Introduction

Composer is a robust tool designed to streamline dependency management in PHP. The tool pulls and manages all the dependencies and libraries in one place. It installs and updates the libraries that your projects depend on, and enables you to declare these libraries on a per-project basis. Composer is utilized in virtually all latest PHP platforms and frameworks including Drupal, Magento 2, Symfony, and Laravel.

This tutorial will show you how to install PHP Composer on your CentOS 7 VPS or dedicated server.

Before You Start

For this tutorial to flow seamlessly, you require the following:

  • A fully configured CentOS 7 server

Step1 - Putting All The Dependencies In Place

Before downloading and installing The Composer ensure that your CentOS 7 server has the required dependencies to aid this process.

First, execute the command below to update the cache for your package manager:

$ sudo yum update

Next, install the following dependencies:

  • Curl: Which will help you download Composer.
  • Php-cli:A dependency used to run and install Composer.
  • php-mbstring: The package that will provide the functions for our library.
  • Git: Which the Composer will use to download project dependencies.
  • Unzip: Which will be used to extract the zipped packages.

To install all these dependencies run the command below:

$ sudo yum install curl php-cli php-mbstring git unzip

That's all! All the dependencies are successfully installed, the stage is set and it should be easy to install Composer.

Step 2 - Installing The Composer

Composer comes with an installer which is developed in PHP. Download this installer and authenticate it, before using it to install the Composer itself.

First, execute the command below to change to the home directory:

$cd ~

Then, run the command below to get the Installer.:

$ curl -sS https://getcomposer.org/installer -o composer-setup.php

To authenticate this installer, download a SHA-384 hash on this page, and then copy and store this hash as a CentOS shell variable:

$ HASH=93b54496392c062774670ac18b134c3b3a95e5a5e5c8f1a9f115f203b75bf9a129d5daa8ba6a13e2cc8a1da0806388a8

If have the most recent hash, run the command below to match the installer to this 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 this installer is authentic, you will get an output similar to the one below:

Installer verified

However, if the installer is not authentic, you will get the following output:

Installer corrupt

In such a case, download the installation command again, then scrutinize the hash to ensure its the latest. Once you are sure the script and the hash are correct, run the verification command again.

When the Installer is validated, run the command below to install PHP Composer globally:

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

The command above will install the PHP Composer in the directory, /usr/local/bin, as a system-wide command called composer. Once the process is completed, you will get the following output:

All settings correct for using Composer
Downloading...

Composer (version 1.7.2) successfully installed to: /usr/local/bin/composer
Use it: php /usr/local/bin/composer

Now, run the command below to test the installation.

$ composer

This will give you an output displaying the version of the Composer, together with command options and their descriptions:

   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ / __ `__ / __ / __ / ___/ _ / ___/
/ /___/ /_/ // // // /_/ //_/ (__  )  __/ /
____/____/_/ /_/ /_/ .___/____/____/___/_/
                    /_/
Composer version 1.7.22018-08-1616:57:12

Usage:
  command [options] [arguments]

Options:
  -h, --help                     Display this help message
  -q, --quiet                    Do not output any message
  -V, --version                  Display this application version
      --ansi                     Force ANSI output
      --no-ansi                  Disable ANSI output
  -n, --no-interaction           Do not ask any interactive question
      --profile                  Display timing and memory usage information
      --no-plugins               Whether to disable plugins.
  -d, --working-dir=WORKING-DIR  If specified, use the given directory as working directory.
  -v|vv|vvv, --verbose           Increase the verbosity of messages: 1for normal output, 2for more verbose output and 3for debug
. . .

This output indicates that the Composer was flawlessly deployed on the CentOS 7 server.

Conclusion

Congratulation! PHP Composer was successfully installed on a CentOS 7 System. You can now explore further options to learn how to utilize the Composer and leverage the power and reliability of this tool.

Check out these top 3 VPS services:

Was this article helpful?