How to install and use PHP Composer on Ubuntu 20.04

s2 0

is a dependency manager for PHP (similar to npm for Node.js or pip for Python).

Composer allows you to specify the PHP libraries your project depends on and all libraries and dependencies are retrieved and managed for you. Composer is used in all modern PHP frameworks and platforms such as Laravel, Symfony, Drupal and Magento.

This tutorial explains how to install and use Composer on Ubuntu 20.04.

Install PHP Composer on Ubuntu

Before installing Composer, make sure that you have all of the required requirements installed on your system:

sudo apt updatesudo apt install wget php-cli php-zip unzip

Composer provides an installer written in PHP that we use to install Composer. Use wget
to download the installer:

wget -O composer-setup.php

The above command saves the file as a composer-setup.php in the current working directory.

Composer is a single file CLI application and can be installed either globally or as part of the project. Global installation requires sudo permissions.

  • To globally install Composer as a system-wide command that is available to all users, simply place the file in a directory on the system PATH
    . Run the following command to install 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.10.7) successfully installed to: /usr/local/bin/composer
    Use it: php /usr/local/bin/composer

    You can now use Composer by running it composer in your terminal.

  • To install Composer locally, enter:

    sudo php composer-setup.php --install-dir=/path/to/project

    This creates a file called. downloaded composer.phar in your project root directory. To use Composer, navigate to the project directory and run it php composer.phar

When a new version of Composer is available, you can update your installation with the following command:

sudo composer self-update  

Getting started with Composer

Now that Composer is installed on your Ubuntu system, let’s see how to use Composer to create a PHP project.

The first step is to create the project root directory and navigate to it:

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

In this example we are using a PHP package called carbon
to create a sample application that prints the current time.

Run the following command to initialize a new Composer project and install the Carbon package:

composer require nesbot/carbon
Using version ^2.35 for nesbot/carbon
./composer.json has been created
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 5 installs, 0 updates, 0 removals
  - Installing symfony/translation-contracts (v2.1.2): Downloading (100%)         
  - Installing symfony/polyfill-php80 (v1.17.0): Downloading (100%)         
  - Installing symfony/polyfill-mbstring (v1.17.0): Downloading (100%)         
  - Installing symfony/translation (v5.1.2): Downloading (100%)         
  - Installing nesbot/carbon (2.35.0): Downloading (100%)         
Writing lock file
Generating autoload files
5 packages you are using are looking for funding.
Use the `composer fund` command to find out more!

As shown in the output, Composer creates the composer.json File downloads and downloads and installs 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-rw-r--. 1 linuxize linuxize    60 Mar 27 18:05 composer.json
-rw-rw-r--. 1 linuxize linuxize 11135 Mar 27 18:06 composer.lock
drwxrwxr-x. 6 linuxize linuxize    82 Mar 27 18:06 vendor
  • vendor is the directory in which the project dependencies are saved.
  • composer.lock is a file that contains information about all installed packages and their versions and binds the project to the specific versions.
  • composer.json is the file that describes your PHP project, including PHP dependencies and other metadata.
All PHP packages that can be installed with Composer are listed under Package is

Composer has autoload capabilities 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.

the vendor/autoload.php File is generated automatically by Composer and loads all libraries automatically.

Next line creates alias Carbon and the last line prints the current time with the carbon now Method.

Run the script by typing:

php testing.php

The output should look something like this:

Now: 2020-06-17 20:41:04

If you need to update the project packages later, enter:

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.


We showed you how to install Composer on Ubuntu 20.04 and use it to create a basic PHP project.

For more information on Composer, see the official documentation

If you have any questions please leave a comment below.