Tutorial

How To Install and Use Composer on Your VPS Running Ubuntu

Published on August 15, 2013
How To Install and Use Composer on Your VPS Running Ubuntu

Status: Deprecated

This article covers a version of Ubuntu that is no longer supported. If you are currently operate a server running Ubuntu 12.04, we highly recommend upgrading or migrating to a supported version of Ubuntu:

Reason: Ubuntu 12.04 reached end of life (EOL) on April 28, 2017 and no longer receives security patches or updates. This guide is no longer maintained.

See Instead:
This guide might still be useful as a reference, but may not work on other Ubuntu releases. If available, we strongly recommend using a guide written for the version of Ubuntu you are using. You can use the search functionality at the top of the page to find a more recent version.

Introduction

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

Installation

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

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/*

Autoloading

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.

Conclusion

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

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
10 Comments


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

even better add this to make composer global

sudo mv composer.phar /usr/local/bin/composer

and then just type composer anywhere for any new project.

Hi all, just seen this and know it’s a little old but thought I would pipe in, basically you should never run composer update on your server, composer update is for finding newer versions of your dependencies, you run composer update locally in your development environment, ensure that your composer.lock file is not in your .gitignore file, then on the server you run composer install to ensure it matches your development environment

The swap worked fine for me :)

NVM a reboot fixed it but would still like to know the cause

when i run ‘composer update’ i get

Fatal error: Uncaught exception ‘ErrorException’ with message ‘proc_open(): fork failed - Cannot allocate memory’ in phar:///usr/local/bin/composer/vendor/symfony/console/Symfony/Component/Console/Application.php:983 Stack trace: #0 [internal function]: Composer\Util\ErrorHandler::handle(2, ‘proc_open(): fo…’, ‘phar:///usr/loc…’, 983, Array) #1 phar:///usr/local/bin/composer/vendor/symfony/console/Symfony/Component/Console/Application.php(983): proc_open(‘stty -a | grep …’, Array, NULL, NULL, NULL, Array) #2 phar:///usr/local/bin/composer/vendor/symfony/console/Symfony/Component/Console/Application.php(799): Symfony\Component\Console\Application->getSttyColumns() #3 phar:///usr/local/bin/composer/vendor/symfony/console/Symfony/Component/Console/Application.php(760): Symfony\Component\Console\Application->getTerminalDimensions() #4 phar:///usr/local/bin/composer/vendor/symfony/console/Symfony/Component/Console/Application.php(690): Symfony\Component\Console\Application->getTerminalWidth() #5 phar:///usr/local/bin/com in phar:///usr/local/bin/composer/vendor/symfony/console/Symfony/Component/Console/Application.php on line 983

any ideas?

This wont work on a 512 droplet because its causing an error. -> https://github.com/composer/composer/issues/945

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
February 8, 2014

@alban: Try upgrading your droplet instead of just adding swap, does that fix it?

hello, when i run composer.phar update, it write killed. I make a swap file of 2G but same effect… I’m on ubuntu 12.04. have you an idea?

I just figured it out: I ran sudo apt-get clean followed by sudo apt-get update (not sure if that’s what fixed it or the clean command) and then ran through the steps here: http://thereluctantdeveloper.com/2013/11/upgrading-php-5-4-ubuntu-12-04-lts/

In case anyone else needs help with this.

I just tried this and running "sudo apt-get install php5 git php5-curl"results in “php5 is already the newest version” followed by it trying to install git-man libcurl3-gnutls liberror-perl patch and it doesn’t work due to some 404 errors…

Anyway, my PHP is currently at 5.3.10 so how do I get my PHP to the necessary 5.3.2+ level? This article doesn’t seem to cover that bit. I’m running the LAMP image using Ubuntu 12.04 and haven’t upgraded anything yet.

This is very confusing as to why it wouldn’t upgrade my PHP.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more