How to Install and Use Composer on Ubuntu

Updated on July 21, 2021
How to Install and Use Composer on Ubuntu header image

Introduction

Composer is a tool for dependency management in PHP. It allows you to declare the libraries your project depends on and manages (install/update) them for you.

Prerequisites

1. Install Support Libraries

Composer uses PHP, and it is best to ensure the latest PHP binaries get installed by adding the repository from one of the Ubuntu developers:

# sudo add-apt-repository -y ppa:ondrej/php

2. Install PHP and Unzip

After adding the repositories, update apt, install PHP, and unzip:

# sudo apt update
# sudo apt install -y -q php8.0-{cli,mysql,gd,common,curl}
# sudo apt install -y -q unzip

The commands above install the CLI version of PHP, the MySQL drivers, the GD library, the common components, and the curl library for PHP. It also installs the unzip binary which composer uses to unzipping packages when downloaded.

3. Create a Non-Root User

Composer should never run as the root user. If the server is a web server, there is probably a lemp user or www-data user, which is OK to run the composer binary. This documentation assumes composer runs as a command line utility and without a web server. To create the user run the following:

# sudo useradd -m composer

This adds a new user called composer and creates a home directory in /home/composer.

4. Install Composer

Install composer using the root user or the sudo command so the permissions allow the binary to install in the /usr/local/bin directory. To install composer, run the following one line command:

# sudo curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

5. Using Composer

After installing composer, switch to the composer user:

# su - composer

To check to ensure you switched to the composer user, run whoami at the prompt to ensure it returns composer. You can also use pwd to ensure you are in the /home/composer directory.

As the composer user (notated by the changed prompt) run:

$ composer

Composer responds with information about the binary, and the usage, options, and available commands. This ensures a successful installation.

   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 2.1.3 2021-06-09 16:31:20
...
	

6. Composer Basics

Adding a Package

To add a managed package using composer, use the composer require command followed by the package name. You can find a searchable list of composer packages at Packagist. One of the most common logging packages is monolog/monolog. To include it, run:

$ composer require monolog/monolog

This downloads, unzips, and retrieves the files required to use the monolog/monolog package. Then, it places them in the vendor/ subdirectory structure for use.

Removing a Package

After you've added a library, it's just as easy to remove a package. To remove the monolog/monolog package added above, run:

$ composer remove monolog/monolog

Including Composers Libraries

To use the libraries composer downloads, in any PHP script, require the autoloader:

#!/usr/bin/php
<?php

require_once 'vendor/autoload.php';
...

This imports any composer downloaded and defined packages.

7. Composer Support Files

Composer uses two main files to track the dependencies and packages. It uses composer.json to track the required packages defined by the user, and it uses composer.lock to track every package downloaded.

composer.json

Inside composer.json the package and version requirements get defined. The package name is straightforward, but the version number can contain various constraints.

8. Updating Composer

Main Binary

Composer has an auto-update feature to allow you to keep it up-to-date. At any time, to update the main binary run:

# sudo composer self-update

Dependencies

If you need to update the packages at any time, as the composer user in directory that composer.json is in run:

$ composer update

Conclusion

Composer is a powerful PHP dependency manager. Combining it with the searchable Packagist repository gives you many resources at your fingertips to integrate with your PHP program with ease.

Further Reading