Setup Composer For PHP Dependencies On CentOS 6

Published on: Mon, Dec 7, 2015 at 4:10 am EST
CentOS Linux Guides PHP

Introduction

Composer is a popular dependency manager for PHP which can simplify the installation and update of libraries necessary for your project. Nowadays, it's becoming the de-facto standard in this area because of its unparalleled convenience and ease of use.

Unlike Yum or Apt, Composer never installs or updates any code or packages at the system level. Instead, code packages and libraries are managed within the project's own directory on a per-project basis.

In this tutorial, I will show you how to install and use Composer to manage the dependencies of a PHP project. These steps were written for a CentOS 6 based Vultr LEMP server.

Prerequisites

  • Deploy a CentOS 6 server instance with PHP (no lower than 5.3.2, 5.3.4 or higher would be better) installed.
  • Log in as a common user with sudo permissions. You can find how to create such a user in this article.

Step 1: Install dependencies for the Composer program

sudo yum update
sudo yum install curl git php55u-cli.x86_64

Step 2: Install the Composer program

Composer can be installed either locally or globally. Local installation can bring you more flexibility for each single project and avoid permission issues while global installation is more convenient with a system-wide executable.

To install locally

To install Composer locally in your project directory ~/myproject:

cd ~
mkdir myproject
cd myproject
curl -sS https://getcomposer.org/installer | php

The Composer program, a PHP archive file called composer.phar, will be downloaded into your project directory.

You can test the installation with the command php composer.phar.

To install globally

curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer

Once installed, you can use the command composer anywhere instead of using php composer.phar in a specific project directory. As a matter of convenience, I will use the global command composer in the following sections.

Step 3: Use Composer to install dependencies

To install dependencies with Composer, first, you need to create a file called composer.json within your project directory. This file defines all of the required dependencies. For example, the content of a composer.json file requiring the "psr/log" library could be like:

{
    "require": {
        "psr/log": "1.0.0"
    }
}

Here, "psr/log" represents "vendor/package", "1.0.0" represents "version number". Both of the two items can be found on Composer's default repository site packagist.org.

If you need other libraries, just add a comma and a new line for each of them:

{
    "require": {
        "psr/log": "1.0.0",
        "phpunit/php-timer": "1.0.7",
        "symfony/event-dispatcher": "3.0.0"
    }
}

Then run the following command to install the libraries you have specified. Any libraries that your picks depend on will also be installed automatically.

composer install

All of the installed libraries will be saved in the ./vendor directory.

Anytime you modify the content of composer.json, or when you want to check if there are new versions, you should run the update command:

composer update

Also, some of the libraries provide autoload information. You can autoload them by simply adding the following sentence into your project code:

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

That's it. You are now ready to manage your project's dependencies with Composer. For more details, visit the Composer official website.

Enjoy!

Optional: XDebug warning

You may see the warning "You are running composer with xdebug enabled..." when using Composer. If so, you can resolve the warning by following these steps.

Modify the configuration of PHP in the file /etc/php.d/15-xdebug.ini:

sudo vi /etc/php.d/15-xdebug.ini

Comment the line zend_extension=xdebug.so with a ;:

;zend_extension=xdebug.so

Save and quit:

:wq

Then reboot the system:

sudo reboot

Want to contribute ?

You could earn up to $300 by adding new articles