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

Shell access to a running Debian system with sudo privilege. Install PHP 5.3 or higher version on Debain. All newly installed systems recommended to follow initial server setup with Debian 11

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: 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.

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. In my case, I already have latest version of composer. So receive the following message on terminal:

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. Run the following command to install psr/log module in the application. 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. The module no longer required can be removed with the following command. 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.