Install and use PHP Composer on Ubuntu 16.04

PHP Composer  is a package management system for PHP which prevents users from having to "reinvent the wheel" when it comes to commonly-used website components like user authentication or database management. Composer is modeled on other popular package management systems like Ruby's Bundler.

Note

For any Cloud Server with Plesk, applications like PHP Composer should always be installed and managed through the Plesk interface.

Composer vs PEAR

PEAR was the first substantial package management system for PHP. However, PEAR has fallen out of favor with developers in recent years.

Due to the difficult process of getting packages approved for inclusion with PEAR, many of the packages available through PEAR are out of date. PEAR also requires users to install packages system-wide, whereas Composer allows you to install packages either system-wide or on a per-project basis.

Composer also tends to be better at handling dependencies, has a wider and more up-to-date codebase, and is more actively maintained.

Requirements

  • A Cloud Server running Linux (Ubuntu 16.04)
  • PHP installed and configured, version 5.3.2 or higher

Use the command php -v to check your PHP version:

vServer (VPS) from IONOS

Low-cost, powerful VPS hosting for running your custom applications, with a personal assistant and 24/7 support.

100 % SSD storage
Ready in 55 sec.
SSL certificate
ser@localhost:~# php -v
PHP 7.0.13-0ubuntu0.16.04.1 (cli) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
    with Zend OPcache v7.0.13-0ubuntu0.16.04.1, Copyright (c) 1999-2016, by Zend Technologies

In this example, the server is running PHP version 7.0.13.

Install PHP Composer

Update your packages:

sudo apt-get update

Install the curl utility:

sudo apt-get install curl

Download the installer:

sudo curl -s https://getcomposer.org/installer | php

Move the composer.phar file:

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

Use the composer command to test the installation. If Composer is installed correctly, the server will respond with a long list of help information and commands:

user@localhost:~# composer
   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                /_/
Composer version 1.3.2 2017-01-27 18:23:41

Usage:
  command [options] [arguments]

Options:
  -h, --help                     Display this help message
  -q, --quiet                    Do not output any message

Using PHP Composer

To use Composer, you will create a composer.json file in your project directory, then use the php composer.phar install command to install the required dependencies.

The composer.json file specifies which packages you want Composer to install and manage. To find available packages, visit the main PHP Composer repository which aggregates all of the public PHP packages that can be installed using Composer.

For this tutorial, we will install the PHP framework Symfony, which is used by thousands of projects, including Spotify, Drupal, and Magento.

Here is the Symfony page on Packagist.org. We need two things from this page:

  1. The install command (composer require symfony/symfony)
  2. The current version (3.2.4)

Create a directory on your server for this project:

sudo mkdir /var/www/html/symfony-test

Move to that directory:

cd /var/www/html/symfony-test

Then create the composer.json file and open it for editing:

sudo nano composer.json

Put the following content into this file:

{
  "require": {
    "symfony/symfony": "3.2.4"
  }
}

Save and exit the file. Then use the following command to install Symfony:

composer install
We use cookies on our website to provide you with the best possible user experience. By continuing to use our website or services, you agree to their use. More Information.