Composer is quite a powerful and popular tool frequently used by webmasters in daily work. It is designed to help installation and project updates, watching project dependancies. It is checking all packages that given project depends upon and selects appropriate versions to make the process go smooth and prevent conflicts.
This article gives instructions on how to install Composer using Ubuntu 16.04. For different system please check our articles.
To start you need a server running Ubuntu and user access with sudo permissions.
First, you need to install the dependencies. This should be done before Composer installation. You need to update cache of package manager with the help of the following command:
$ sudo apt-get update
After that we are going to install dependencies. Use the following command to install dependancies:
$ sudo apt-get install curl php-cli php-mbstring git unzip
Curl is needed to download Composer; php-cli is to install and run it; php-mbstring is providing functionality for the library that we are going to use; git will be needed for Composer to download dependencies and unzip will be used to unzip packages.
The next stage is to download and install composer. Open home directory and get the installer with the help of curl:
$ cd ~
$ curl -sS https://getcomposer.org/installer -o composer-setup.php
After that you need to run a PHP script to make sure that installer matches SHA-384 hash for the latest version of installer stored at Composer Public Keys page. Use the example below, replacing sample hash with the latest hash:
$ php -r “if (hash_file(‘SHA384’, ‘composer-setup.php’) === ‘669656bab3166a7aff8a7506b8cb2d1c292f042046c5a994c43155c0be6190fa0355160742ab2e1c88d40d5be660b410‘) { echo ‘Installer verified’; } else { echo ‘Installer corrupt’; unlink(‘composer-setup.php’); } echo PHP_EOL;”
If verification has been successful you will see confirmation message.
The following command will help you install composer system-wide; it will be installed under /usr/local/bin
sudo php composer-setup.php –install-dir=/usr/local/bin –filename=composer
Output will be confirmation of successful installation.
If you want to execute composer separately for each project stored on your server you can install it individually for each project. The same approach will be useful in case you don’t have permissions for global installation. In this case installation is done with the help of $ php composer-setup.php. This command will generate composer.phar file in your current directory.
The next stage is to generate composer.json file, which will be needed to use composer in your projects. This file informs composer which dependencies are needed to download and which versions of packages can be installed. This is an important measure to keep the project integrity.
You do not need to perform this operation manually, composer generates this file when you are adding a dependency using a require command.
Using composer to install a package as dependency will require following actions:
- check what library application needs;
- find the library on packagist.org;
- select the package;
- run composer require command to add dependency in composer.json file.
- this will install the package.
Important step of the process is including the autoload script.
PHP does not automatically load classes, so composer provides a script that can be used to autoload your project. This facilitates working with dependencies. What you need to do is to add vendor/autoload.php file into PHP scripts before any case of class instantiation.
To update the project dependancies you need to run update command:
$ composer update
This command will check for latest versions of libraries used in your project. If it finds a newer version your previous version will be updated. It is also possible to update one specific library with the help of
$ composer update vendor/package