Install Composer on Ubuntu 16.04 [5 Minutes Beginner Tutorial]

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.

Install the Required Dependencies

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

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.

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.

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.

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

You will get the below output.

Then install the composer globally using the below command.

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

You can see the following output.

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

And you will get the following output.

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.

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.

Then, you will get the following output.

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.

You will get the following output.

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.

Then add the lines in the file.

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

You will get the following output.

Then, update the project dependencies using the below command.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *