We hope you find this tutorial helpful. In addition to guides like this one, we provide simple cloud infrastructure for developers. Learn more →

How To Install and Use Composer on Your VPS Running Ubuntu

Posted Aug 15, 2013 116.8k views PHP Frameworks PHP Ubuntu


Composer is a great dependency management tool for PHP. What it does is it keeps track of the libraries you declare your project needs and fetches and installs them for you. It is not a package manager per se because it’s not used globally by default, but rather in each individual project you may have.

What is it used for? Say you have an application project that depends on a couple of libraries that in turn can even depend on others. Once you declare these dependencies in a small .json file, Composer will track down those libraries and install them for you.

In this tutorial, we will install Composer on a VPS and see how it works and what we can do with it. To this end, I will use a server running Ubuntu and the LAMP stack already installed on it. As for the specific requirements of Composer, it runs on Linux, OS X, and Windows alike but it requires you have PHP 5.3.2+ installed.

Be sure you have all of the requirements installed:

sudo apt-get install php5 git php5-curl


First, you need to download Composer into your project folder. Let’s create an empty folder for this purpose in our /var/www Apache web root folder called my_project:

cd /var/www
mkdir my_project
cd my_project

Download Composer into this folder with the following command:

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

You should see a success message that looks something like this:

All settings correct for using Composer

Composer successfully installed to: /var/www/my_project/composer.phar
Use it: php composer.phar

Now if you check in the project folder, you’ll see that only one file has been downloaded: composer.phar - a PHP archive file that can be run from the command line.

If you are in a different folder than your project but don’t want to navigate back to your project folder to download Composer, you can specify the --install-dir option and it will download it wherever you specify:

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

This command you can run from anywhere and it will install Composer in another_folder located in /var/www. To check if Composer is working properly, you can use the following command that will bring up a list of available commands:

php composer.phar

Using Composer

To use Composer in your project, you’ll need one file: composer.json. In this file you will describe the dependencies of your project. Let’s say our project depends on the DigitalOcean library that helps interact with the DigitalOcean API. So let’s create the file:

nano composer.json

And in this file you can paste in the following:

    "require": {
        "toin0u/digitalocean": "1.2.1"

Save the file and exit. With this simple block of code, you specify that your project requires that library (called digitalocean) created by the vendor toin0u. After the colon you write the version you need - in this case being 1.2.1. Here you can specify any version you want and even use wildcards. But where do you get this information about the packages?

There is a great website where you can find a lot of libraries with all the information necessary to be specified in the composer.json file: Packagist. The DigitalOcean API library can also be found on Packagist and you can check there exactly what you have to specify in the require block.

To use wildcards for the version, you can specify something like this:

"toin0u/digitalocean": "1.2.*"

This will match any version that starts with 1.2 (1.2.0, 1.2.1, etc). Additionally, you can specify multiple dependent libraries, not only one. You can do so by adding multiple entries in the require block:

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

OK, so now that your project has specified its dependencies, you need to run a command for Composer to fetch and install them to your project:

php composer.phar install

This will find the latest version of the package that matches the number we specified and download it into a folder called /vendor right there in your project. In our example, we will now find the library in the vendor/toin0u/digitalocean folder. Additionally, Composer creates a composer.lock file in which it specifies the exact versions of the packages it installed and locks the project to those versions.

The purpose of the .lock file is to restrict Composer from installing other versions to your project. So when you run the install command, Composer will first check for this file and will install what’s specified in it rather than what the .json file tells it to. This is great because if your dependencies are rather strict and other people need to deploy your projects, you can rest assured that the exact same version of the library is being used.

And as we’ve seen, if there is no .lock file, Composer will create one based on the dependencies it installed from the .json file. But if you do indeed want to overwrite the .lock file and upgrade the dependent libraries to newer versions, you can run the following command:

php composer.phar update

This will get the latest stable versions of the dependent libraries that match the criteria in the composer.json file and overwrite the composer.lock file with the new dependencies. If you only want a particular library to get updated (which very well is probably true in most cases) you can run the update command and pass the packages you need updated as options in the following format:

php composer.phar update vendor/package vendor/package2

Or you can use wildcards to update all the packages from a specific vendor:

php composer.phar update vendor/*


Another cool thing about Composer is its autoloading feature. For those libraries that provide autoloading information, Composer automatically generates an autoload.php file directly in the /vendor folder that you can include in your project. Then you can directly start using the classes from those libraries. In your PHP project, you can just specify this:

require 'vendor/autoload.php';

And you are good to go.


Composer is a widely used tool in the world of PHP. There are many projects and frameworks out there that use Composer to manage and install their packages and dependencies. So getting familiar with how it works is quite important.

Article Submitted by: Danny


Creative Commons License