Hello friends. For many PHP Composer developers is an essential tool in their daily work. So if you are a PHP developer or you are just starting with this, you should know how to install PHP Composer on Debian 11. The process is really simple but it’s never too much to have it at hand.
Composer is a project dependency manager for PHP programming. That means that it allows us to manage, i.e. specify, download, and keep up to date, the software packages or dependencies on which our PHP project is based.
The reason for using it is that when you develop a PHP application it requires functionality that is not in the default PHP libraries. That is, you have to add others for your project to work or to be able to be carried out.
So, when you start the project you have to go to the page of each of the software components that you want to use, download them, copy them to the folder of your project. This is a bit torturous because at some point these libraries will be updated and then you have to start this process all over again.
Composer was born to avoid this. Because just by defining the right dependencies, Composer will download them and add them to your project in a matter of seconds. And if they are updated, it only takes a minimal effort or command to update them. All without wasting time and in an orderly fashion.
So, let’s get started.
As we can guess, one of the main requirements is to have PHP installed on the system. This shouldn’t be a problem because PHP is present in the Debian 11 repositories and is quite lightweight.
So, open a terminal and as usual update the distribution.
sudo apt update sudo apt upgrade
Then, you can install PHP in case you don’t have it.
sudo apt install php
Note: If you don’t have
sudo available then you have to run these commands as root. Also, you can enable it by reading our post about it.
Now to install Composer on the system, you have to run this single command
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
This command downloads and “compiles” the Composer installer for us.
Then install it in a directory that is in our PATH so that we can use it throughout the system. Also, you have to give it run permissions.
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer sudo chmod +x /usr/local/bin/composer
Then, verify the installed version by running
composer --version composer version 2.1.8 2021-09-15 13:55:14
Before using Composer it is always advisable to upgrade to the latest version. This process can be done by running
If you already have the latest stable version, you will get an output screen like this
You are already using the latest available Composer version 2.1.8 (stable channel)
Composer works with a file called
composer.json which is where the project dependencies are stored. To add one you can visit packagist to find one you need.
For example, I will add
sebastian/comparator with the command
composer require sebastian/comparator
Just like that, Composer recognizes the dependency, downloads it along with its dependencies, and installs them for our project that we just need to use. And so on with each of the dependencies you need.
To update the versions of the dependencies of your project run
Or you can update a single one
composer update sebastian/comparator
Also, you can update several at once.
To remove a dependency, just run
composer remove sebastian/comparator
So you can use Composer on Debian 11 in a basic but useful way.
In this post, we have covered the installation of PHP Composer on Debian 11 and a small introduction to its particular usage. This tool has become indispensable for many developers so it is not surprising that it is popular.