How to install and use PHP Composer on Ubuntu 18.04

s2 0

is a dependency manager for PHP (similar to npm for Node.js or pip for Python). Composer pulls in all the required PHP packages your project depends on and manages them for you.

In this tutorial, we are going to show you how to install and use Composer on an Ubuntu 18.04 computer. The same steps can be used for Ubuntu 16.04.


Before continuing with this tutorial, make sure that you are logged in as a user with sudo rights and that PHP is installed on your Ubuntu 18.04 system.

Install PHP Composer

If you’re in a hurry and don’t want to check file integrity, scroll down to the Install Composer – the quick way


To install Composer on your Ubuntu system, do the following:

  1. Before downloading and installing Composer, first update the package index and install the required requirements:

    sudo apt updatesudo apt install wget php-cli php-zip unzip
  2. Now that we have installed php cli on our computer, we can download the Composer Installer with:

    php -r "copy('', 'composer-setup.php');"

    The above command loads the composer-setup.php File in the current working directory.

  3. Next we need to check the data integrity of the script by comparing the script SHA-384 Hash with the latest installer hash found on the Public keys / signatures of the composer

    We use the following wget command to download the expected signature of the latest composer installer from the composer’s github page and store it in a variable named. save HASH:

    HASH="$(wget -q -O -"

    Now run the following command to check that the installation script is not damaged:

    php -r "if (hash_file('SHA384', 'composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"

    If the hashes match, you will see the following output:

    Installer verified

    If the hashes don’t match, see Installer corrupt. In this case you will have to download the Composer installation script again and change the value of the $HASH variable with echo $HASH
    . Once the installer has verified, you can move on to the next step.

  4. The following command installs Composer in the /usr/local/bin Directory:

    sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
    All settings correct for using Composer
    Composer (version 1.8.5) successfully installed to: /usr/local/bin/composer
    Use it: php /usr/local/bin/composer

    the composer is installed as a system-wide command and is available to all users.

  5. The final step is to verify the installation:


    The above command prints the version, commands, and arguments of the composer.

    / ____/___  ____ ___  ____  ____  ________  _____
    / /   / __ / __ `__ / __ / __ / ___/ _ / ___/
    / /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
    ____/____/_/ /_/ /_/ .___/____/____/___/_/
    Composer version 1.8.5 2019-04-09 17:46:47
        command [options] [arguments]
If you want a project-related Composer installation, use the following command php composer-setup.php which creates a file called. is created composer.phar in your current working directory. You can use the composer by walking ./composer.phar <command>.

Install Composer [Quick Way]

Use the following commands to quickly install Composer on your Ubuntu system:

  1. Install PHP-CLI and Zip:

    sudo apt update && sudo apt install wget php-cli php-zip unzip curl
  2. Download composer with curl:

    curl -sS |php
  3. Move the composer file to /usr/local/bin Directory:

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

Getting started with Composer

Now that Composer is installed on your Ubuntu system, we’ll show you how to use Composer in a PHP project.

The first step is to create a directory that will be the root of the project and keep the composer.json File. This file describes your PHP project including the PHP dependencies and other metadata.

Create the project directory and switch to it with:

mkdir ~/my-first-composer-projectcd ~/my-first-composer-project

The next step is to initialize a new one composer.json Use of composer require <package name> Command and specify the package we want to download.

In this example, we are creating a sample application that displays the current time with a package named. prints carbon

Run the following command to initialize a new one composer.json and install the carbon package:

composer require nesbot/carbon
Using version ^1.33 for nesbot/carbon
./composer.json has been created
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 3 installs, 0 updates, 0 removals
  - Installing symfony/polyfill-mbstring (v1.9.0): Downloading (100%)
  - Installing symfony/translation (v4.1.4): Downloading (100%)
  - Installing nesbot/carbon (1.33.0): Downloading (100%)
symfony/translation suggests installing symfony/config
symfony/translation suggests installing symfony/yaml
symfony/translation suggests installing psr/log-implementation (To use logging capability in translator)
Writing lock file
Generating autoload files

As you can see in the output above, Composer creates the composer.json File and it will download and install Carbon and all of its dependencies.

If you list your project’s directory, you will see that it contains two files composer.json and composer.lock, and a vendor Directory.

ls -l
-rw-r--r-- 1 linuxize users   60 Aug 29 00:50 composer.json
-rw-r--r-- 1 linuxize users 6.8K Aug 29 00:50 composer.lock
drwxr-xr-x 5 linuxize users 4.0K Aug 29 00:50 vendor
  • the vendor Directory is the directory in which the project dependencies are saved.
  • the composer.lock contains a list of all installed packages including the version of the packages.
  • the composer.json describes the PHP project and all PHP dependencies.
You can search the Composer repository Package is
for PHP packages.

Composer has autoload functions that allow us to use PHP classes without the need to do so require or include the files.

Create a file named testing.php and add the following code:


require __DIR__ . '/vendor/autoload.php';

use CarbonCarbon;

printf("Now: %s", Carbon::now());

Let’s analyze the code line by line.

In the first line after the opening php tag we add the vendor/autoload.php File that was automatically generated by Composer and that loads all libraries automatically.

Next we’ll use aliasing CarbonCarbon how Carbon and in the last line we print the current time with the carbon now Method.

Run the script by typing:

php testing.php

The output should look something like this:

Now: 2018-08-28 23:47:19

Later on, if you want to update your PHP packages, you can just do the following:

composer update

The above command looks for newer versions of the installed packages and whether a newer version is found and the Version restriction
match with the one specified composer.json, Composer updates the package.


You learned how to install Composer on your Ubuntu 18.04 computer. We also showed you how to use Composer to create a simple PHP project.

For more information on Composer, see the official Composer documentation

If you have any questions please leave a comment below.