Official Power Up Hosting Blog

Everything about Linux, Windows, and hosting ;)


I am an Online Marketer and technology lover. I like to learn new things and share that with people.


Our Newsletter

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.



Official Power Up Hosting Blog

How To Install Composer on Ubuntu 16.04 (The Easy Way)


The composer is the package management system for PHP.

It will install and the dependencies required by the applications.

It installs appropriate versions of PHP for the projects based on the project requirement.

Here, in this tutorial, you will learn How to Install and Use Composer on Ubuntu 16.04.


  • Ubuntu 16.04 server configured according to the Initial server setup guide.

Installing the Dependencies

Before installing the composer, we have to install their dependencies also.

First, update the package index.

$ sudo apt-get update

After that, we have to install the dependencies.

Here is the list of dependencies which we are going to install.

curl: It is used to download the composer.

php-cli: It is used to install and run the composer.

php-bstring: It is used for providing the functions to the library which we are using

git: Composer uses this to download the required dependencies.

unzip: It is used to unzip the downloaded packages.

We are going to install all of them in a single command line.

$ sudo apt-get install curl php-cli php-mbstring git unzip

In the next step, let us see How to download and install the composer.

Downloading and Installing Composer

You have to install the composer using an installer.

Change to the home directory.

$ cd ~

And then get the installer using curl.

$ curl -sS -o composer-setup.php

After that, we have to verify the installer whether it is matching the SHA-384 hash.

Use the below command to verify whether the installer matches the SHA-384 hash value as for the latest version of the installer.

$ 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 the hash is matching, you will get the following output.

Installer verified

If you want to install the composer globally, use the below command.

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

The above command will download and install the composer in the system as system-wide command composer at /usr/local/bin.

The Output will look like the following.

All settings correct for using Composer
Downloading 1.1.1...

Composer successfully installed to: /usr/local/bin/composer
Use it: php /usr/local/bin/composer

To test the installation, execute the following command.

$ composer

You will get the output like this.


/ / ____ ___ ____ ____ ________ _____
/ / / __ / __ `
_ / __ / __ / ___/ _ / /
/ // // / / / / / / // / // (
) / /
/_// // // ./_//_//

Composer version 1.1.1 2016-05-17 12:25:44

command [options] [arguments]

-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.
. . .

If you get this output, then it means the composer is installed on your system.

You can have individual composer executables for individual projects hosted on your server. For that, you have to install the composer locally.

After the above process, execute the below command.

$ php composer-setup.php

It will create a file called ./composer.phar. You can execute this file using the below command.

$ ./composer.phar command

Composer.json file generation

You need to have composer.json file in order to use the composer in your project.

The composer.json file is the one which tells composer to download dependencies which are required by the project.

Also it specifies the version to be installed for the project. So, that you can avoid the backward compatibility while doing the project due to the unstable version of packages.

There is no need for you to create this project manually. Also, there may be the chance of getting syntax errors.

The composer will create a composer.json file automatically while installing a dependency for the project using the require command.

You can add more dependencies using this command without having to edit the file.

Here is the process flow of adding a package as the dependency in the project. It follows as

  • Finding the library requirement of applications
  • Find a matching open source library on It is the official repository for the composer.
  • Select the package which you require.
  • Run the composer require the command to include the dependency in the composer.json file and then install the package.

Let us see how it works in real-time using demo applications.

Here, we will give a sentence and that has to be converted into the slug (URL friendly string).

This technique is used for converting the Page title into URL.

First, change to home directory.

$ cd ~
$ mkdir slugify
$ cd slugify

Find the Package from the Package List

Go to the Packagist and search for the package which helps us in generating the slug.

Search for "slug" in the search bar.

All the relevant packages will be listed here. In each description after the name, you can see the numbers.

The down arrow indicates the number of downloads and the star indicates that how many times that the package got featured on the composer.

You have to choose the package which has been downloaded more time. Since the package is downloaded by many people, it will be the stable version.

Also, you have to find the package which is matching our expectation.

Here the slugify matches our requirement. If you read the package description, you will get to know what the packages are about.

The packages will be listed with package name and vendor name. Also, they will be listed with the package name.

Here, we are going to use cocur/slugify.

Adding the Package

To add the package, use the composer require command.
Then generate the composer.json file.

$ composer require cocur/slugify

You will get the following output.

Using version ^2.1 for cocur/slugify
./composer.json has been created
Loading composer repositories with package information
Updating dependencies (including require-dev)
- Installing cocur/slugify (v2.1.1)
Downloading: 100%         

Writing lock file
Generating autoload files

You can see that the composer automatically chooses the version of packages to be added to the output. Also two files composer.json and composer.lock in the file.

Also, you can see a vendor directory.

$ ls -l

You will get the following output.

total 12
-rw-rw-r-- 1 sammy sammy   59 May 17 10:24 composer.json
-rw-rw-r-- 1 sammy sammy 2894 May 17 10:24 composer.lock
drwxrwxr-x 4 sammy sammy 4096 May 17 10:24 vendor

We have already seen the use of composer.json. Let me tell you the use of the composer.lock.

The file contains the version information about the each package.

It will help people, in case if they copy your project, the file will help them to install the appropriate version of Composer.

In that vendor directory, all the project dependencies are located.

You should not commit the vendor folder into version control. All you have to do is just include composer.json and composer.lock in vendor folder.

If you tend to install a project and it contains the composer.json, you have to use the composer install to download the project dependencies.

View on Version Constraints

Take a look at the composer.json file. You will get something like this.

$ cat composer.json

You will get the following output for that.

   1 {
   2 "require": {

5 }

You can see the ^ symbol before the version number.

There are a lot of constraints and formats available to define the required package version.

This will help you to provide the flexibility for the requirement of installing various versions of packages.

This ^ is the recommended one for the interoperability and it is generated by the composer.json file.

You can set the 2.1 as the minimum version and allow the update up to the version 3.0.

Even though you don't need to edit the constraints manually most of the time, still you need to edit the constraints manually.

here is the example for how the composer version constraints work.

Constraint	Meaning	Example Versions Allowed
^1.0	>= 1.0 < 2.0	1.0, 1.2.3, 1.9.9
^1.1.0	>= 1.1.0 < 2.0	1.1.0, 1.5.6, 1.9.9
~1.0	>= 1.0 < 2.0.0	1.0, 1.4.1, 1.9.9
~1.0.0	>= 1.0.0 < 1.1	1.0.0, 1.0.4, 1.0.9
1.2.1	1.2.1	1.2.1
1.*	>= 1.0 < 2.0	1.0.0, 1.4.5, 1.9.9
1.2.*	>= 1.2 < 1.3	1.2.0, 1.2.3, 1.2.9

If you want to know more information about the constraints, refer this

Adding Auto Load Script

PHP can't handle the class loading. The Composer gives the script for autoloading to include in your project.

It will help you to work with the dependencies easily and defining the own name spaces.

Before any class initiation, just add the vendor/autoload.php file in the PHP scripts.


We are going to create nano test.php and then we will use the cocur/slugify library.

$ nano test.php

Then add the following line.


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

use Cocur\Slugify\Slugify;

$slugify = new Slugify();

echo $slugify->slugify('Hello World, this is a long sentence and I need to make a slug from it!');

Then run the above script using the below command.

$ php test.php

After that, you will get the following output.


Update the project dependencies

Updating the project dependencies is very easy. To update the dependencies, just use update command.

$ composer update. 

The command will look for the recent version of the dependencies. If there is any recent version available and it is compatible with the composer.json file, then the old dependency will be replaced with the new one.

Also, the composer.lock file will be updated to make use of the changes.

Here is the command to update any specific dependency.

$ composer update vendor/package vendor2/package2


The composer is a necessary tool for the PHP developer as it makes ease of their job.

If you have any queries, please leave that in the comment. We will answer you.

Also, make sure to signup for our upcoming tutorials.



I am an Online Marketer and technology lover. I like to learn new things and share that with people.