How to Install and Use PHP Composer on Debian 11 Linux

Composer is a PHP dependency management utility designed to ease the process of maintaining and distributing PHP packages as separate application components. It revolutionized the PHP ecosystem by providing a base for current PHP programming, including component-based frameworks and applications.

The PHP Composer holds all of the required modules for an application and installs them using a single command. You can also keep the modules updated from time to time. Today’s post will show you how can install and use PHP Composer on your Debian 11 system. So, let’s move ahead!

How to install PHP Composer on Debian 11

To begin the procedure of installing PHP composer, firstly, press “CTRL+ALT+T” to open your Debian 11 terminal and update your system packages:

$ sudo apt update

After updating system repositories, we will install some required packages to work with PHP Composer:

$ sudo apt install wget php-cli php-zip unzip

To download the PHP Composer, write out this command in your terminal:

$ wget -O composer-setup.php

From the output, you can see that the PHP Composer installer file is saved as “composer-setup.php” in our present working directory:

You can either install the PHP Composer as a single CLI application locally or globally. If you want to install PHP Composer for all of the users present on your system, then execute this command:

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

The execution of the above-given command will install PHP Composer in the “/usr/local/bin” directory:

We have installed PHP Composer globally. However, if you want to install only for your local system, then execute the below-given command and specify the path of the installation directory in the “/path/to/project”:

$ sudo php composer-setup.php --install-dir=/path/to/project

Till this point, we have successfully installed PHP Composer on our Debian 11 system. In a case when a newer version of PHP Composer is released, you can update your installed version with the “composer self-update” command:

$ sudo composer self-update 

How to use PHP Composer on Debian 11

This section demonstrate the method of using PHP Composer on a Debian 11 system. For this, firstly, we will create a directory “php-composer-project” with the help of the “mkdir” command:

$ mkdir ~/php-composer-project

Now, you have to switch to the created “php-composer-project” directory:

$ cd ~/php-composer-project

Utilize the “composer require” command with the package name for creating a new composer.json file and specify the package name you want to download in it. For instance, we will create a test application that prints the current time using the “carbon” package:

$ composer require nesbot/carbon

Wait for few minutes, as the command executed above will firstly create a “composer.json” file and then install all other dependencies along with the “carbon” package:

The error-free output indicates that we have successfully performed the specified operation. Now, write out the “ls” command with the “-l” option for listing the files and directories present inside of the “php-composer-project”:

$ ls -l

The output will show you a vendor directory, composer.lock, and composer.json files in the “php-composer-project” directory. Here, the “vendor” directory stored the project dependencies, “composer.json” is a file that describes the PHP project and all of its related dependencies, and the “composer.lock” file has a list of installed packages for the PHP project:

Now, create the “testfile.php” and add the below-given code in it:

$ sudo nano testfile.php

 First of all, we will include the “/vendor/autoload.php” file which the PHP Composer generated. This “/vendor/autoload.php” file will help the PHP project to autoload the required libraries. In the other lines, we are utilizing “Carbon” as alias for printing the current time using its “now()” method:

require __DIR__ . '/vendor/autoload.php';
use Carbon\Carbon;
printf("Now: %s", Carbon::now());

Press “CTRL+O” to save the code you have added in the “testfile.php”:

Now, you can execute the “testfile.php” in your Debian 11 terminal:

$ php testfile.php

Here, you can see that our PHP file successfully showed the current time as output:

If you want to update your PHP project later, then execute this command:

$ composer update


Composer is a PHP dependency management system. On a project-by-project basis, PHP Composer manages the required dependencies. It can download and handle all of the essential libraries and dependencies with the help of a single command. This write-up demonstrated how you can install and use PHP Composer on your Debian 11. Feel free to try out this dependency manager on your system!