In Ubuntu 22.04, you can install the PHP package manager Composer in just a few steps. With the command-line tool curl, in­stall­a­tion is quick and simple.

Ubuntu 22.04: install Composer step by step

To install Composer, you first need to ensure that PHP is already installed on your system and you have access to PHP via the terminal. You can check both by entering the following command in the terminal:

php
bash

If there’s an error message, you can install the command line tool for PHP 8 with the following command:

sudo apt install php8.1.cli
bash
Image: Terminal after running the command ‘php’
If you haven’t installed the PHP command line tool, Ubuntu will suggest the ap­pro­pri­ate commands in the terminal.

Once you’ve verified that you can use PHP on your system, you can begin in­stalling PHP Composer.

Tip

You can use Composer in IONOS web hosting packages. In addition to PHP support, web hosting packages from IONOS offer many other benefits such as built-in DDoS pro­tec­tion.

Step 1: update the system

First, you’ll want to make sure your Linux system is in good shape by running available updates. You may need to confirm execution of the upgrades by choosing y (yes). You can use the following terminal commands for this:

sudo apt update
sudo apt upgrade
bash

Updating your system may take a few moments. How long it takes will depend on how many updates you need to install.

Step 2: install required packages

For the Ubuntu in­stall­a­tion of Composer to work, you need to install a few packages that Composer requires for in­stall­a­tion. This includes, for example, the command line tool curl or the version control Git.

The packages can also be down­loaded directly in the terminal using the following commands:

sudo apt install curl php-mbstring git unzip
bash

Step 3: install Composer

Now you can get started with the actual in­stall­a­tion of PHP Composer on Ubuntu 22.04. You do this by using the down­loaded curl tool and typing the following command into terminal:

curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer
bash
Image: Terminal after running the installation command for Composer
Terminal verifies the success of the Ubuntu in­stall­a­tion of Composer.

Step 4: check if in­stall­a­tion was suc­cess­ful

As a final step, manually check if the Composer in­stall­a­tion was suc­cess­ful by accessing the package manager. If this doesn’t work, your $PATH en­vir­on­ment variable may not be con­figured correctly. The command below invokes the current version of Composer:

Composer
bash

If the in­stall­a­tion of Composer on Ubuntu 22.04 went smoothly, terminal will show the following:

Image: Terminal after running the command ‘composer’
The Composer font and current Composer version are displayed upon launching Composer.
Note

If you’re using an operating system other than Ubuntu 22.04, you can still use Composer. Just take a look at our other in­stall­a­tion guides:

First steps when using Composer

The package manager is used in PHP projects to manage and update de­pend­en­cies. To help you get started with Composer and avoid common errors, we’ve compiled the most important Composer commands.

Step 1: create composer.json file

The main task of Composer is to manage the de­pend­en­cies of your PHP project. The central place to specify these de­pend­en­cies is the composer.json file. This can be created manually. Al­tern­at­ively, you can specify to have the JSON file be set up auto­mat­ic­ally when you create your first de­pend­ency. The Composer command to manually create a composer.json file is as follows:

composer init
bash

Step 2: add de­pend­en­cies to your project

You can also use a Composer command to insert a de­pend­ency into your project. This ensures that the composer.json file is updated to the ap­pro­pri­ate state. In the example code, the popular logging library Monolog is inserted as a de­pend­ency in a PHP project.

composer require monolog/monolog
bash

Step 3: update de­pend­en­cies

Every now and then it’s necessary to update the de­pend­en­cies of a PHP project. A Composer command can be used for this. It updates all de­pend­en­cies you’ve added to your project in a single step:

composer update
bash
Go to Main Menu