How to Install and Use Composer on Your VPS Running Ubuntu
Edit Article

  • 1 Editor

We might have forgotten several concepts, as new things come up every day. If you are in need of help, don't worry we will help you to refresh your knowledge.


Now let us start with some basics..

What are composer?

It is dependency management tool used mostly with PHP. It allows you to declare the libraries necessary for your project. It usually keeps tracks of the libraries that you have declared in your project needs. It also fetches and installs them for you. It is not package manager like Yum or apt. It deals with packages or libraries and manages them on a per project basis. Installing the same in the directory called vendor which resides in your project. It is called as dependency manager tool. It is because by default it never installs anything globally.


Here is brief explanation on how it works:

  1. You have a project that depends on a number of libraries
  2. Some of them depend on other
  3. If you have composer then
  4. It will enable you to declare the libraries which are necessary for you
  5. It also finds out which versions of packages can and need to be installed. Install them and it will download the same in to your project

Use of Composer:

For instance we have an application project depending on a couple of libraries. It in turn also depends on others too. Once you define all this dependencies in .json file format the composer will track down those libraries and install if for you. Isn’t it cool?


Today, I will show you how to install the Composer on a VPS and will check how it works and what we can do with the same. For these articles I will be using the Ubuntu and LAMP stack already installed on the same. I assume that all the requirements for composer are installed like runs on Linux, OS X, and Windows alike. It also requires the PHP 5.3.2 +


Please make sure all requirements are fulfilled


sudo apt-get install php5 git php5-curl


Steps to install and configure the Composer on Ubuntu Step – 1: Installing composer on Ubuntu


For that create an empty folder for instance /var/www Apache web root folder called Project1 and then download the composer in that folder

cd /var/www
mkdir my_project
cd my_project

After that download Composer into the folder with following command

curl -sS https://getcomposer.org/installer | php

You will able to see message as below All settings correct for using Composer


Downloading...


Composer successfully installed to:

/var/www/my_project/composer.phar

Use it: php composer.phar


Verify your project folder you will find only one folder downloaded i.e. composer.phar. The php archive can be run through the command line


In case you are working with different projects and do not want to navigate then,


specify –install-dir option and it will download it


curl -sS https://getcomposer.org/installer | php -- --install-dir=/var/www/another_folder


The above command you can run from anywhere and it will install composer in another_folder located in/var/www. In order to verify that composer is working properly execute below steps:

php composer.phar

Step – 2: How to use Composer

Nano composer.json
And once create paste following in the same
{
    "require": {
        "toin0u/digitalocean": "1.2.1"
    }
}

Save the file and exit out Now in order to use wildcard version you can specify below "toin0u/digitalocean": "1.2.*"


The above will match any version that starts with 1.2 and you can also specify multiple dependent libraries which can be done by adding as follows:

"require": {
        "toin0u/digitalocean": "1.2.1",
        "acme/foo": "1.0"
}

Once your project specifies the dependencies, it is time to run command for composer in order to fetch and install them to your project.


php composer.phar install


You will find the latest version of the package that matches the number specified and downloaded. Additionally it also create composer lock file. It has the exact version of package to be installed and locks the project to those versions.


The purpose of lock file is to restrict composer to install other versions to your project. When you run this command composer will check this file and install what is specified for that file and not what in that .json file


In case there are no lock files then composer will create by their own as per the dependencies which it installed from the .json file. However, in case you need to overwrite the same and upgrade it newer versions then you can run following command:

php composer.phar update

The above command will get the latest stable versions of the dependent libraries. It will match the criteria provided in the .json file. In case you are interested in updating only some of the library which I think will be true in many cases. Then you can run the update command in those cases and pass the packages as shown below

php composer.phar update vendor/package vendor/package2

Or you can also take help of wildcards as below php composer.phar update vendor/*


Step – 3: Autoloading:


Another good feature of composer is the autoloading. It is here where those libraries provides the autoloading information for them composer. It will automatically generate an autoload.php file directly in folder configured. Here you can include in your project. This will allow you to start using class libraries for php projects you can execute.

require 'vendor/autoload.php';

Due to its feature of installing the package, it also enables us to install in a way that is compatible with the latest version of the package. It makes a better candidate when our project contains higher amount of the dependencies. Imagine you have project having large amount of dependencies. All needs to be installed manually it will take much time in finding and installing them. A small problem in installing the same could result in failure of including those dependencies. Thus, the composer could be used in those cases. It will club all those dependencies and make it ready for your project.

Conclusion:

Hope you have refreshed your mind with these concepts. We have updated more concepts so that you get the best out of our blog. If you feel we have missed out anything, feel free to let us know. Stay connected for more updates.

Article Tools

Did this article help you?

YesNo

Become
an Author!

Write an Article