How to Install and Use PHP Composer on Ubuntu 20.04

Posted 

4 min read

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

With Composer, you can specify the PHP libraries your project depends on, and it 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.

Installing PHP Composer on Ubuntu

Before installing Composer, ensure that you have all the necessary requirements installed on your system:

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

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

wget -O composer-setup.php https://getcomposer.org/installer

The command above will save the file as 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. The global installation requires sudo privileges.

  • To install Composer globally as a system-wide command that will be available for all users, simply place the file in a directory that is in 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
    Downloading...
    
    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 in your terminal

  • To install composer locally enter:

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

    This will download a file named composer.phar in your project 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  

Getting 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 root directory and navigate to it:

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

In this example, we’ll use 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 and downloads and installs carbon and all 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 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
  • vendor is the directory where the project dependencies are stored.
  • composer.lock is a file that keeps information about all installed packages and their versions, locking the project to the specific versions.
  • composer.json is the file that describes your PHP project, including the PHP dependencies and other metadata.
All PHP packages installable with Composer are listed at Packagist.

Composer has autoload capabilities which allow us to use PHP classes without the need to require or include the files.

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

<?php

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 autoload all of the libraries.

Next line creates alias Carbon and the last line prints the current time using 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

Later, if you need to update the project packages, enter:

composer update

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

Conclusion

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 any questions, please leave a comment below.