Install Composer on Ubuntu 16.04 [5 Minutes Beginner Tutorial]

What is Composer? 

Well, It is definitely an musical instrument. Composer is a dependency manager for PHP. It combines and keep libraries in server environment for development.

A required additional Package of PHP can be installed without any trouble, if you have dependency manager.

Also, A perfect PHP version can be used for projects based on the requirement.

Here in this tutorial, let us see how to install composer on Ubuntu 16.04 server.

Requirements

  • As usual a server with Ubuntu 16.04 and sudo user enabled. If you dont know how to create one, check this article.
  • Sudo user a new user account to your server which will have same privileges as root user.
  • Get 50$ Credit on Vulture Clouds Here and Test the Instruction for free.

Install the Required Dependencies

When you about to install Composer, you have to install few more components to support the operations of Composer.

$ sudo apt-get update

Here are the few things which we will install.

php-cli – Installation and running the composer

php-mbstring – Provide the necessary functions to the library

Git – Downloads the required project dependencies.

unzip – Unpack the downloaded packages.

Use the below command to install all of them at the same time.

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

Now, you have installed all the required packages. It’s time to install the composer.

Download and Install Composer

First, switch to home directory, using the below command.

$ cd ~

Then download the composer using curl. If you have not installed curl, then you will get error. So, make sure to follow the above step.

$ curl -sS https://getcomposer.org/installer -o composer-setup.php

Next, execute the below command to verify the SHA-384 hash key.

$ php -r "if (hash_file('SHA384', 'composer-setup.php') === '669656bab3166a7aff8a7506b8cb2d1c292f042046c5a994c43155c0be6190fa0355160742ab2e1c88d40d5be660b410') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"

You will get the below output.

Installer verified

Then install the composer globally using the below command.

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

The composer will be installed in the following location. /usr/local/bin

You can see the following output.

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

If you want to test the installation, just use the below command.

$ composer

And you will get the following output.

Output
   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 1.1.1 2016-05-17 12:25:44

Usage:
  command [options] [arguments]

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

The Composer.json file creation

Inorder for the composer to function and carry out the necessary task, the json file is required.

Here the composer.json works by intimating the composer which version of the packages required and dependencies are required for project to run smoothly.

Here, you the usage of unstable version is avoided. If you use unstable version of packages, then you will struggle to progress the work.

Because, unstable version does not provide better support.

Even in Ubuntu, we only talk about 14.04, 16.04 and 18.04 as they are the stable version of ubuntu operating system.

Note: You dont need to create the composer.json file. It will be created by composer itself.

The main thing here is you dont need to edit the composer file everytime when you add the dependency to your project.

You can do this by simply using the command called require

You might have got little confused with the statements. Let me explain you clearly.

First, you have to identify which dependencies are required for your project.

Then, go and find the perfect package in the packagist.org

After that, use the composer require library to install the package in the composer.json file.

I will give you the widely used example of the process.

When you develop a website, the slug is very necessary for the site.

The slug is also called as URI. We are going to use a package which will convert given sentence into a slug.

Switch to the home directory. Then create a directory called slugify.

$ cd ~
$ mkdir slugify
$ cd slugify

Finding Packages in the Slugify Website.

The slugify has the search box for you to search the required packages. Here you have to put the related search term to find the right packages.

The slugify will return a lot of packages with similar functionality. Ofcourse, there will be a lot of packages developed by people for the same function will be submitted to slugify.

You can choose a package by identifying the number of installs and number of stars it has received.

The more number of stars and installs indicates that the packages is more stable one.

You should also read the package details to see whether it is matching your requirements.

If it is ok, then chose the package.

Here, i will search for slug in packagis.org website.

install composer ubuntu

I will click on the slugify and choose the cocur/slugify package to be installed.

To install that particular package, i will use the below command.

$ composer require cocur/slugify

Then, 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

Here, the important thing is that composer automatically decides the required version of packages.

If you check your directory, you can see few new files such as composer.json, composer.lock and directory of vendor.

$ 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

Here, the composer.lock will contain the package version information. It ensures that same packages are installed when someone clone this project.

The vendor directory contains the project dependencies.

You should not put this vendor directory under version control. You can set composer.json and composer.lock under version control.

The last task is to include autoload script in the PHP script. PHP does not initiate any functions automatically and this auto load helps in this situation.

Here is the command to install Autoload.

$ nano test.php

Then add the lines in the file.

<?php
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!');

Save and close the file and then test it using the below command.

$ php test.php

You will get the following output.

hello-world-this-is-a-long-sentence-and-i-need-to-make-a-slug-from-it

Then, update the project dependencies using the below command.

$ composer update

This command will look for new packages to update. If it matches your project version constraints mentioned in the composer.json file, then it will replace old version with new version.

The composer.lock will also be updated.

Conclusion

Now, you have learned how to install composer on Ubuntu 16.04 system. If you have queries, just leave them in comments. Make sure to subscribe for our tutorial to receive notifications. Also, enable push notification get notified when an article goes live.

LIKE THIS ARTICLE?

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn

Leave a Comment