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.
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:
- Unlimited traffic
- Fast SSD NVMe storage
- Free Plesk Web Host Edition
In this example, the server is running PHP version 7.0.13.
Install PHP Composer
Update your packages:
Install the curl utility:
Download the installer:
Move the composer.phar file:
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:
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:
- The install command (composer require symfony/symfony)
- The current version (3.2.4)
Create a directory on your server for this project:
Move to that directory:
Then create the composer.json file and open it for editing:
Put the following content into this file:
Save and exit the file. Then use the following command to install Symfony: