How To Install and Use PHP Composer on Debian 11

Composer is a dependency manager for PHP. That is, it is a PHP application that helps you to manage the libraries developed by third parties that you are going to incorporate into your project.

Today it is considered the default dependency manager for a few very good reasons:

  • It is very simple to use. So you can handle it in no time.
  • Composer has a very complete repository (Packagist).
  • It significantly reduces the problems of changing runtime environment (through its dependency freeze functionality).

Now, do I really need it?

The short answer is definitely YES. Because we need a tool that can be used to install libraries and manage application dependencies. These dependencies emerge as we create our application and it needs the functionality that is not in the PHP libraries. In this, Composer does a great job and is the application-level package manager for PHP. It has gained immense popularity and has become almost a standard for managing dependencies in PHP applications.

So with Composer we can download, uninstall, update our dependencies and make them available for our project.

Install PHP Composer on Debian 11

The installation will be done from the main menu, so you have to open it from the main menu.

Then, you have to make sure that the system is up to date to avoid problems.

sudo apt update
sudo apt upgrade

As we are talking about a dependency manager for PHP, then, we have to have PHP installed on the system.

sudo apt install php

This way we already have what we need on the system so we can install PHP Composer.

Execute this command to download the installer to our computer

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"

Now we need to install it in a directory included in our PATH. So, run

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

This will generate a file called composer to which we have to give execution permissions.

chmod +x /usr/local/bin/composer

And now you can use the command without any problems.

First steps with PHP Composer on Debian 11

Before using it, it is always advisable to work with the latest version available. To make sure PHP Composer is up to date, just run the following command

composer self-update

If the application is up to date, you will get a similar message

You are already using the latest available Composer version 2.1.8 (stable channel)

Composer works with two files called composer.json and composer.lock . The second of these is best left untouched.

The first one is where the dependencies of our project are defined. To create it, go to your project folder and start Composer.

composer init

This will create it interactively. There you can define the features of your project.

You can then edit the file and within the Require section specify each of your project’s dependencies as follows:

"fabpot/goutte": "v4.0.1"

Where “fabot/goutte “ is the dependency and “v3.2.0” is the version.

And so on we have to declare these dependencies.

Also, we can do it more conveniently with the require command where we can define the dependency and composer will edit the file for us.

composer require fabot/goutte

And if we want to remove a dependency, then we just need to run

composer remove fabot/goutte

To install all the dependencies of our project, you have to run

composer install

If you know that a dependency has been upgraded to a higher version, then run

composer update

Then you can use Composer to restart your projects.

Conclusion

PHP Composer has become the default PHP dependency manager for many developers who see it as the best and easiest to use. And for them, this tool is part of their everyday life so you can make it part of yours too.

So, what do you think about Composer? have you used it? tell us your experiences using it and share this post.