How To Install and Use PHP Composer on Ubuntu 18.04

August 29, 2019


PHP Composer is an application that tracks dependencies for a project. It does not replace system package managers like apt or yum.

Composer allows you to specify a set of libraries for a specific project then identifies the versions and dependencies and installs them.

This guide will show you how to install and get started with PHP Composer in Ubuntu 18.04.

tutorial on installing and using php composer on ubuntu


Steps For Installing PHP Composer on Ubuntu

Step 1: Update Local Repository

Start by updating the local repository lists by enter the following in a command line:

sudo apt-get update

Step 2: Download the Composer Installer

To download the Composer installer, use the command:

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

Step 3: Verify Integrity of the Download

1. Visit the Composer Public Keys page. Copy the Installer Signature (SHA-384).

2. Set the code shell variable:


3. Run the script below to compare the official hash against the one you downloaded:

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

The script will either tell you the download is verified, or that it has been corrupted. If it’s corrupted, re-download the file.

Step 4: Install PHP Composer

1. Installing PHP Composer requires curl, unzip, and a few other utilities. Install them by entering the following:

sudo apt-get install curl php-cli php-mbstring git unzip
install supporting software for composer

Install Composer as a command accessible from the whole system.

2. To install to /usr/local/bin. enter:

sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer

The installer should output:

All settings correct for using Composer

Composer (version 1.6.5) successfully installed to: /usr/local/bin/composer
Use it: php /usr/local/bin/composer

3. Once the installer finishes, verify the installation:

composer ––version

The system should output the version number installed, for example:

Composer version 1.8.6 2019-6-11 15:03:05

4. To remove the installer:

php –r “unlink(‘composer-setup.php’);”

Basic Composer Usage

Composer is designed to track dependencies on a per-project basis. This makes it easier for other users to create the same environment. Composer uses a composer.json file to keep track of required software and allowed versions.

It also uses a composer.lock file to maintain consistency if someone copies the directory. These files are automatically generated using the require command.

1. Open a terminal, and enter the following:

mkdir c_sample
cd c_sample

2. Next, you’ll need to choose a package to load. The website has a broad range of different PHP packages for download. In this example, let’s use the monolog/monolog package. You can follow the instructions, or search the website for monolog.

3. In the terminal window, enter:

composer require monolog/monolog

The system will download the software and create the composer.json and composer.lock files.

Note: Monolog is a package for managing logfiles. The name before the slash is the vendor, and the name after the slash is the package name.

4. Once the process completes, list the contents of the directory:

ls -l

You should see the composer.json and composer.lock files, along with a vendor directory.

5. To view the contents of the composer.json file:

cat composer.json

The system will show you that it has added the monolog software. The carat ^ sign beside the version number indicates the minimum version of the software.

Setting Up Autoloading

PHP doesn’t automatically load classes. However, you can configure Composer to autoload classes for you. This makes working with dependencies much easier.

1. Create a new file using your favorite text editor:

sudo nano composer_sample.php

2. Enter the following into the file:


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

use Monolog\Logger;

use Monolog\Handler\StreamHandler;


// create a log channel

$log = new Logger('name');

$log->pushHandler(new StreamHandler('/~/c_sample/text.log', Logger::WARNING));


// add records to the log


example of autoloading classes

3. Write the file (Ctrl+O), and exit (Ctrl+X).

4. Now you can run the command to autoload monolog:

php composer_sample.php

Updating Dependencies

To update all the dependencies in your composer.json file enter the command:

composer update

This will update all dependencies according to the version specified in the file.

To update one (or more) dependencies individually:

composer update vendor/package vendor_b/package_b


Now you have a good understanding of how to install and start using PHP Composer. We also covered setting up autoloading and updating dependencies.

Was this article helpful?
Sofija Simic
Sofija Simic is an aspiring Technical Writer at phoenixNAP. Alongside her educational background in teaching and writing, she has had a lifelong passion for information technology. She is committed to unscrambling confusing IT concepts and streamlining intricate software installations.
Next you should read
How to Install and Use PHP Composer on CentOS 7
September 10, 2019

Composer is an application that tracks dependencies for a project, allowing you to specify a set of libraries...
Read more
PHP Error Reporting: How to Enable & Display All Errors / Warnings
August 6, 2019

If a script is not written correctly, or if something unusual happens, PHP can generate an error message...
Read more
How to Install RPM Packages on Ubuntu
July 9, 2019

RPM is a package format used by Red Hat based derivatives like CentOS, RHEL or Fedora. It gets its name from...
Read more
How to List Installed Packages on Ubuntu
June 12, 2019

Having a precise list of installed packages helps system admins maintain, replicate, and reinstall systems...
Read more
  • © 2021 Copyright phoenixNAP | Global IT Services. All Rights Reserved.