How to Install and Use PHP Composer on Ubuntu 22.04

PHP is a popular, well-known programming language used for creating websites and web applications. The PHP Composer is an open-source, dependencies management program that will find out the required packages for PHP projects and install them. It is introduced under the MIT license.

This blog will discuss PHP Composer installation, usage, and uninstallation on Ubuntu 22.04.

PHP Composer Installation on Ubuntu 22.04

On Ubuntu 22.04, to install PHP Composer, follow the steps below-listed.

Step 1: Update System Packages

To open the Ubuntu terminal, use the “CTRL+ALT+T” shortcut key and update the system packages:

$ sudo apt update

Step 2: Install PHP

In the next step, install PHP on Ubuntu 22.04:

Step 3: Install curl

Install curl command-line utility on your system:

$ sudo apt install curl

Step 4: Download PHP Composer Setup File

Download the PHP composer setup file on Ubuntu 22.04 using the given “curl” command:

$ curl -sS https://getcomposer.org/installer -o /tmp/composer-setup.php

Step 5: Install PHP Composer

Finally, install PHP Composer with the help of the following command:

$ sudo php /tmp/composer-setup.php --install-dir=/usr/local/bin --filename=composer

Step 6: Verify PHP Composer Installation

Verify the PHP Composer installation on the system:

$ Composer

As you can see, we have successfully installed PHP Composer version “2.3.9” on Ubuntu 22.04:

Let’s move forward toward PHP Composer usage on Ubuntu 22.04.

PHP Composer Usage on Ubuntu 22.04

On Ubuntu 22.04, to use PHP Composer, follow the below-listed steps:

Step 1: Create and Move to PHP Composer Directory

First, make a new directory named “newcomposer” and move to it using the “cd” command:

$ mkdir ~/newcomposer 
$ cd ~/newcomposer

Step 2: Install Required Package

Use below mention syntax to install any package with the help of the PHP Composer

$ composer require <package name>

We will now execute the mentioned command to install the“psr/log” package:

$ composer require psr/log

View the “composer.json” file to check out the information about installed packages using “cat” command:

$ cat composer.json

To remove the required package, utilize the “remove” command along with the package name:

 $ composer remove psr/log

Let’s move ahead to remove PHP composer on Ubuntu 22.04.

PHP Composer Removal From Ubuntu 22.04

From Ubuntu 22.04, to remove PHP Composer, first, open the “/usr/local/bin/” directory by using the “cd” command:

$ cd /usr/local/bin/

Then, remove the related directory from Ubuntu 22.04 by utilizing the “rm” command:

$ sudo rm -r composer

We have demonstrated the procedure to install, use, and remove PHP Composer on Ubuntu 22.04.

Conclusion

On Ubuntu 22.04, to install PHP Composer, first, it is required to install PHP and curl command-line utility. Then, download Composer’s setup file and install it on the system. To use PHP Composer, make the new directory and install the required package for the PHP project using Composer. We have discussed the method to install, use, and remove PHP Composer on Ubuntu 22.04.