You can install the Composer package manager, which was specially designed for PHP, in just a few steps in Ubuntu 22.04. Installation is done in the terminal using the command line tool curl.
Install Composer on Ubuntu 22.04: Step-by-Step Guide¶
To install Composer, you must first ensure that PHP is already installed on your system and that you have access to PHP through the terminal. You can verify these two points by entering the following command in the terminal:
If an error message is displayed, you can install the command line tool for PHP 8 with the following command:
sudo apt install php8.1-cli
bash
After verifying that you can use PHP on your system, the installation of PHP Composer can begin.
With web hosting from IONOS, enjoy many other benefits, such as built-in protection against DDoS attacks and support for new PHP versions.
Step 1: Update the system¶
First, you must to update your Linux system by running all existing updates. You may need to confirm the upgrade process by clicking « Y » (Yes). You can use the following terminal commands for this purpose:
sudo apt update
sudo apt upgrade
bash
Updating your system may take a few moments. It entirely depends on the number of updates to be installed.
Step 2: Install required packages¶
For the Composer installation on Ubuntu to work, you must first install the additional packages including Composer needs for installation. This includes, for example, the curl command-line tool or Git version control.
Packages can also be downloaded directly from the terminal by typing the following commands into your command line:
sudo apt install curl php-mbstring git unzip
bash
Step 3: Install PHP Composer¶
Now you can start the full-fledged installation of PHP Composer on Ubuntu 22.04. To do this, use the curl tool you just downloaded by entering the following command in your terminal:
curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer
bash
Step 4: Verify successful installation¶
In the last step, you need to manually verify success PHP Composer installation by trying to access the package manager. If that doesn’t work, your $PATH environment variable may not be set correctly. The command below calls the current version of PHP Composer:
If your terminal now shows the following output, it means that the installation of PHP Composer was successful on Ubuntu 22.04:
Get started with Composer¶
The package manager is used in PHP projects to manage independences and update them. To make getting started with PHP Composer easy, we’ve put together the essential PHP Composer commands for your first steps.
Step 1: Create the composer.json file¶
The main task of PHP Composer is to manage the dependencies of your PHP project. The central location where you specify these dependencies is the composer.json file. It can be created manually. You can also configure the JSON file automatically when you create your first dependency. The Composer command you need to manually create a composer.json file is:
Step 2: Add dependencies to your project¶
If you now want to add a dependency to your project, you can also use a Composer command to ensure that composer.json is updated accordingly. The following sample code is to add the popular Monolog logging library like a dependency in a PHP project.
composer require monolog/monolog
bash
Step 3: Update dependencies¶
From time to time, it is necessary to update the dependencies of a PHP project. There is also a PHP Composer command to do this. She update all dependencies added to your project all at once: