PHP Composer is basically a dependency management tool for PHP applications. It provides hassle-free installation of PHP modules for the applications. The composer keeps track of all the modules required for the application and installs them with a single command. It also allows users to keep modules updated. You can easily install all the required packages using Composer. The composer maintains a list of required packages in a JSON file called composer.json.

Composer is a similar tool to npm for Node.js, pip for Python, and bundler for ROR. Composer 2 is the latest available version for your system with enhanced performance. We will use that version to install on our system.

This tutorial helps you to install and use PHP composer on Debian 11 Bullseye Linux system.

Prerequisites

Installing PHP Composer on Debian

A PHP script is provided by the official team to configure the composer on your system. You can download it with curl or wget command-line utility. Also, you can download it with the PHP script.

Open a terminal and run:

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

A composer-setup.php file will be created in current directory. Now execute this PHP script to install the composer at the desired location. Use --install-dir to set the binary location and --filename to set the binary name. You can install composer globally accessible for all users and projects or install locally for a specific project.

  • To install composer globally, type:
    php composer-setup.php --install-dir=/usr/local/bin --filename=composer
    chmod  x /usr/local/bin/composer
    
  • You can also install composer under the specific application. This is helpful for shared hosting environments, where you don’t have sudo or root access. To install composer locally for a specific project, type:
    cd /path/to/php-application && mkdir -p bin 
    php composer-setup.php --install-dir=bin --filename=composer
    chmod  x bin/composer
    

    Change /path/to/php-application with the actually application directory.

To see the installed composer version execute binary with -v command parameter.

composer -v

Output:

Composer version 2.1.8 2021-09-15 13:55:14

Upgrade PHP Composer

The PHP composer has the ability to self-upgrade to the latest versions. If the composer is already installed on your system, just type the below command to upgrade PHP composer to the latest version.

composer self-upgrade

In my case, I already have latest version of composer. So receive the following message on terminal:

Output:

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

Working with PHP Composer

You have already installed and configured the composer on your system. Composer will help you to manage modules for your application. For example, to install a new module for your application.

Switch to the PHP application.

cd /path/to/php-application 

Run the following command to install psr/log module in the application.

composer require psr/log

Output:

Using version ^1.1 for psr/log ./composer.json has been created Running composer update psr/log Loading composer repositories with package information Updating dependencies Lock file operations: 1 install, 0 updates, 0 removals - Locking psr/log (1.1.4) Writing lock file Installing dependencies from lock file (including require-dev) Package operations: 1 install, 0 updates, 0 removals - Downloading psr/log (1.1.4) - Installing psr/log (1.1.4): Extracting archive Generating autoload files

Composer will automatically create or update composer.json file at application root directory. Now, the application can use the functionality provided by the module.

The above command will install the latest version of the module. You can also define the module version you want to install for your application. If the module is already installed, it will automatically downgrade/upgrade package to the specified version.

composer require psr/log=1.0

The module no longer required can be removed with the following command.

composer remove psr/log

All the above commands also update composer.json file accordingly.

Conclusion

In this tutorial, you have found instructions to install composer on a Debian Linux system. You can install composer globally to allow access to all users and applications. Also, you can install composer for a specific directory.