How to Install PHP Composer on Ubuntu 22.04 LTS

Composer is an application-level package manager for the PHP programming language similar to NPM for Node.Js or PIP for Python. Composer provides a standard format for managing all dependencies of PHP software and the required libraries by downloading all the required PHP packages for your project and managing them for you. It is used by most modern PHP frameworks such as Laravel, Drupal, Magento, and Symfony.

The following tutorial will teach you how to install Composer on Ubuntu 22.04 LTS Jammy Jellyfish using the command line terminal, along with some basic usage examples working with composer.

Update Ubuntu

First, ensure your system is up-to-date using the following command.

sudo apt update && sudo apt upgrade

Install PHP

First, if you have not installed PHP on your system, use the following command that will also install some other packages that will be required.

sudo apt install curl wget php-common php-cli php-gd php-mysql php-curl php-intl php-mbstring php-bcmath php-imap php-xml php-zip git unzip

Install PHP Composer

The Composer team has made an official PHP script to install and configure PHP Composer on your system. You can download this by visiting the download page or opening your terminal and executing the following.

PHP Method:

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

WGET Method:

wget -O composer-setup.php

Now with the setup.php downloaded, it is time to install composer. This can be done in two ways: one in which you can install composer globally, or the second alternative, installing per PHP application basis.

Install globally:

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

Install per application:

cd /example-project/php-application && mkdir -p bin 
sudo php composer-setup.php --install-dir=bin --filename=composer
sudo chmod +x bin/composer

As per above, you will need to navigate to the PHP project directory and create a bin directory for the PHP composer to be installed.

Once done, confirm composer is installed and which build and version it is.

composer --version

Now test your composer installation with the following command:


Example output:

test composer command ubuntu 22.04 lts
test composer command ubuntu 22.04 lts

Working with PHP Composer

The tutorial will show you some essential functions when the PHP composer is installed to test its operation.

First, create your project directory:

mkdir ~/composer-test && cd ~/composer-test

Next, find a package from Packagist, or alternative, use the tutorial’s example and run the following command to initialize a new composer.json file and install the carbon package:

composer require psr/log

Example output:

example project php composer download psr ubuntu 22.04 lts
example project PHP composer download psr ubuntu 22.04 lts

As above, the composer will automatically create and update the composer.json file along with the dependencies required. Alternatively, you can install or downgrade existing packages to a specific version number.


composer require psr/log=1.0

Optionally, in the project directory, you can list the files and directories within it using the ls command.

ls -l

A breakdown of the terms and the files listed.

  • composer.json – File created for the project and all PHP dependencies.
  • composer.lock – File containing the list of all packages and versions.
  • vendor – Directory where the dependencies are downloaded and installed.

With your project, you may want or require updating the dependencies, which can be done by running the following command.

composer update

Example output:

php composer update command example ubuntu 22.04 lts
PHP composer update command example ubuntu 22.04 lts

As you can see above, no dependencies require updating. If there were, the process would begin.

If you no longer need the module, you can remove it with the composer remove command:

composer remove psr/log

Example output:

remove example with composer ubuntu 22.04 lts
remove example with composer ubuntu 22.04 lts

When removing packages, as with installing, removing will automatically update the composer.json file, which can verify this using the cat command.

cat composer.json

Example before removal of psr/log package:

    "require": {
        "psr/log": "1.0"

Example after removal of psr/log package:


How to Upgrade PHP Composer

A handy feature of PHP composer is that it can self-upgrade to the latest stable version. Run the following command to upgrade.

composer self-update

As mentioned above, the PHP composer is already at the latest version. However, you will be prompted to upgrade if an upgrade is available.

Comments and Conclusion

The composer is a pretty popular tool, and no doubt you will encounter this as a developer. One of the better advantages of having a composer is that it deals with PHP dependencies on a per-project basis. You can have multiple projects on the same machine that depend on having various versions of PHP installed.


Like to get automatic updates? Follow us on one of our social media accounts!