Learn Downloading, Installing and Using Composer on Ubuntu 14.04

January 14, 2019

Table of Contents

Composer is an extremely popular PHP management tool for dependencies, used to make installation and updates easier for projects. It also checks what other packages a project needs, and automatically obtains them for you, with the correct version.

In this doc, we will install and start using Composer on An ITWeb.Services Ubuntu 14.04 VPS.

Requirements

  • An ITWeb.Services Ubuntu 14.04 VPS.
  • A regular user with Sudo access to that VPS.

Installation

Dependency installation

First of all, we must ensure our VPS has all of Composer’s requirements successfully installed and working.

Update the package list.

sudo apt-get update

Next, actually install Composer’s requirements. You’ll need curl for the download, and php5-cli for the installation and usage of it. git is also used by Composer for project requirement downloads.

Install the requirements.

sudo apt-get install curl php5-cli git

Download and install

Installing Composer is very simple.

curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer

That downloads and installs Composer as a global command, called composer, located in /usr/local/bin. You will get this output.

#!/usr/bin/env php
All settings correct for using Composer
Downloading...
Composer successfully installed to: /usr/local/bin/composer
Use it: php /usr/local/bin/composer

Run the following to test the installation.

composer

The output will be as follows.

   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ / __ `__ / __ / __ / ___/ _ / ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
____/____/_/ /_/ /_/ .___/____/____/___/_/
                    /_/
Composer version 1.0-dev (9859859f1082d94e546aa75746867df127aa0d9e) 2015-08-17 14:57:00
Usage:
 command [options] [arguments]

Usage

JSON file

To use Composer, you need a file called composer.json to tell Composer what requirements your project has and what version of those requirements to install. Don’t create this manually to avoid doing something incorrectly – Composer makes the file for you when you add dependencies. Additional dependencies are also added automatically.

Example Composer Usage

To use Composer for dependency installations:

  • Identify the library needed for the app.
  • Find a good, open-source library on <Packagist.org>, Composer’s repository.
  • Choose the package you want.
  • Use composer require to include and install the dependency.

Initialising Slugit

We will now go through this process with a simple example app, which will take a sentence and make it a friendly string, called a slug. This is used frequently to convert page names to URLs, to make it easier to generate URLs and paths.
We will start by making a folder for the app, called slugit.

mkdir ~/slugit
cd ~/slugit

Searching on Packagist

We will now go on packagist.org and find a package to help generate slugs. Searching for slug on Packagist should show some of these packages.

easy-slug/easy-slug, muffin/slug, ddd/slug, zelenin/slug, webcastle/slug, anomaly/slug-field_type

We need to find a string to slug converter, so cocur/slugify looks good, with many installations and stars.

Requiring a package

After choosing the package, we run composer require to include it as a dependency, generate composer.json, and install it.

composer require cocur/slugify

As seen in the output generated, Composer selected the most recent package version and used it. Checking ~/slugit, you should see 2 files, composer.lock and composer.json, plus a folder named vendor.

composer.lock is used to store info about package versions, and keep them the same.

The vendor folder is used to install the dependencies. Do not commit this folder into a Git repository or GitHub.

If a project you’ve download already contains composer.json, use composer install to download its dependencies.

Understanding version constraints

If you check what composer.json includes, you should see something similar to this block.

{
    "require": {
        "cocur/slugify": "^1.2"
    }
}

Composer has many different formats, and constraints, to define a package’s version, to allow flexibility coupled with stability. ^ before a version number makes that version the minimum, and allows all versions below 2.0.

You shouldn’t normally need to change version constraints, but if you do, check Composer’s official documentation for more information and guidelines on how it all works.

The autoload script

Composer provides an autoload script, which makes it much easier to work with your dependencies and namespaces.

Just include vendor/autoload.php in your PHP before any class instantiation.

Back to our slugit example. Let’s create a test script, called example.php, using cocur/slugify.

vim example.php

Put the following into example.php.

<?php
require __DIR__ . '/vendor/autoload.php';
use CocurSlugifySlugify;
$slugify = new Slugify();
echo $slugify->slugify('Hello World, this is a long sentence and I need to make a slug from it!');

Run the script.

php example.php

It will output the following text:

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

Updating project dependencies

To update project dependencies.

composer update

If updates are found, and compatible with the constraint given in composer.json, it’ll replace the previous version and update composer.lock.

To update one or more specific libraries.

composer update vendor1/package1 vendor2/package2

Conclusion

In this tutorial, we went through installing, configuring, and an example of using Composer for PHP application dependency management.

Need help?

Do you need help setting up this on your own service?
Please contact us and we’ll provide you the best possible quote!