How to Install and Use PHP Composer on Ubuntu 20.04

How to Install and Use PHP Composer on Ubuntu 20.04

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

With Composer, you can specify PHP libraries that depend on your project, and that will pull and manage all libraries and dependencies 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 you have installed all the necessary requirements on your system:
sudo apt update
sudo apt install wget php-cli php-zip unzip

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

wget -O composer-setup.php

The above command will save the file as composer-setup.php in the current working directory.

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

To install Composer globally as a system-wide command that will be available to all users, simply place the file in a directory on the PATH system. 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

Output :

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 composer on your terminal

To install the composer locally, enter:

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

This will download the file named composer.phar in your project’s root directory. To use Composer, navigate to the project directory and run php composer.phar

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

sudo composer self-update  

Get started with Composer

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

The first step is to create the project’s root directory and navigate to it:
mkdir ~/my-first-composer-project
cd ~/my-first-composer-project

In this example, we will use a PHP package called carbon to create an example 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

Output :

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 and downloads and installs carbon and all its dependencies.

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

ls -l

Output :

-rw-rw-r--. 1 vagrant vagrant    60 Mar 27 18:05 composer.json
-rw-rw-r--. 1 vagrant vagrant 11135 Mar 27 18:06 composer.lock
drwxrwxr-x. 6 vagrant vagrant    82 Mar 27 18:06 vendor
  • The vendor is the directory where project dependencies are stored.
  • composer.lock is a file that stores information about all installed packages and their versions, locking the project to a specific version.
  • composer.json is a file that describes your PHP project, including PHP dependencies and other metadata.

Composer has automatic loading capabilities that allow us to use PHP classes without the need to require or include files.

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


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

use Carbon\Carbon;

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

Let’s analyze the code line by line.

The vendor / autoload.php file is automatically generated by Composer and automatically loads all libraries.

The next line creates an alias for Carbon and the last line prints the current time using the current Carbon method.

Run the script by typing:

php testing.php

The output will look like this:

Output :

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

Then, if you need to update the project package, enter:

composer update

The above command will check for newer versions of installed packages, and if a newer version is found and the version restrictions match those specified in composer.json, Composer will update the package.


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

For more information about Composer, visit the official documentation page.

If you have questions, please leave a comment below.

Related posts

How to Monitor CPU Usage in Linux Over Time


How to integrate the ONLYOFFICE editor with the Nuxeo Platform


How to Get Windows Terminal Fonts (Cascadia Code) on an Ubuntu Terminal


How to Install the GitScrum Agile Project Management Tool on Ubuntu 20.04 LTS


How to Install Yarn on Ubuntu 20.04


How to Install Drupal 9 with Nginx and Let’s Encrypt SSL on Debian 10


How to Activate Dark Mode on Ubuntu 20.04 LTS


How to Install and Use FFmpeg on Ubuntu 20.04


How to Make a Bootable Linux USB Drive Using Linux