How to install and use PHP Composer on Debian 11

PHP Composer is a dependency manager for PHP. This dependency manager supports application development and the process of calling libraries and frameworks.

Composer is a dependency manager for PHP with support for dependencies to libraries and frameworks. It helps you manage your project's dependencies, whether they come from Packagist, Github or anywhere else.

In this article, TipsMake will share you how to install and use PHP Composer on a Debian 11 server.

System update

The Linux system is constantly updated every day with new security fixes, kernel patches, and performance improvements. Some updates simply change the version number (such as from 3.2.0-4 to 3.2.0-5), while others may provide bug fixes or improvements security. The best way is to keep your system up to date to take advantage of the latest features, maintain stability, and keep your computer safe from vulnerabilities that can be exploited by attackers.

Run the following command to update the system.

sudo apt-get update && sudo apt-get upgrade -y

Once the upgrade is complete, run the following command to install the required dependencies.

sudo apt-get install curl unzip git php-cli php-zip php-mbstring -y

After the system is fully updated, restart the server. Run the following command to reboot the system. When making changes to the system, such as hardware configuration, kernel, or package updates, it is important to restart the computer for the changes to take effect.

sudo reboot -r now

Once the reboot is complete, log back in as the non-root user yourself and continue to the next step.

Install PHP Composer

Now that your system is up to date and all the necessary dependencies are installed, it's time to download and install the latest version of PHP Composer.

The PHP Composer developer provides an installation script written in PHP, to make the installation process easy. Download the script, check the script's signature to make sure it's not corrupted, and run the installer.

First, download the latest version of the PHP Composer installation script from the official website using the curl command.

curl -o composer-setup.php https://getcomposer.org/installer

Now, run the command below to check the signature of the PHP script. This step is to ensure that the installer has not been tampered with (i.e. corrupted or modified) during the download.

HASH=906a84df04cea2aa72f40b5f787e49f22d4c2f19492ac310e8cba5b96ac8b64115ac402c8cd292b8a03482574915d1a8 php -r "if (hash_file('SHA384', 'composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"

The output will look like the image below. This output verifies that the installer is not corrupted. HASH may change in the future, you can find the latest hash on this page https://composer.github.io/pubkeys.html

If the file has been tampered with, the command will get rid of the Installer corrupt export and unlink. In that case, you will need to download the installer and verify the hash again until you get the installer verified message.

How to install and use PHP Composer on Debian 11 Picture 1How to install and use PHP Composer on Debian 11 Picture 1 The output verifies that the installer is not corrupted

To summarize, there are two ways to install Composer on a Debian 11 system: locally or system-wide.

The system-wide Composer installation allows you to use the composer command from any directory. You can easily update your dependencies from any project folder from the same Terminal as Composer, without having to move back and forth between folders. To install Composer overall, run the following command.

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

This command will install Composer as a system-wide command called composer in the /usr/local/bin directory and make it available to all users.

You will see the following result.

How to install and use PHP Composer on Debian 11 Picture 2How to install and use PHP Composer on Debian 11 Picture 2

To check if Composer has been installed properly, run the following command to invoke the Composer executable.

composer

The output will be like this.

How to install and use PHP Composer on Debian 11 Picture 3How to install and use PHP Composer on Debian 11 Picture 3

A local install is to install Composer in your home directory or somewhere else inside the directory you specify, since you won't need to access the composer command from outside that directory, unless using symbolic link.

To install Composer locally, run the following command. Replace path/to/directory in the command below with the directory where you want to install Compose.

sudo php composer-setup.php --install-dir=path/to/directory --filename=composer

In this tutorial, you learned how to install PHP Composer on Debian 11. Now you can use it for local or system-wide installation if you want to access composer command from anywhere on the system mine.

  1. Notable changes and additions in Debian 11 'Bullseye'
  2. How to Install and Use Ansible on Debian 10
  3. How to Install Gradle on Debian 10
  4. Configure mouse settings on a Debian system
  5. Use Speedtest CLI to test Internet speed on Debian 10
  6. How to set JAVA_HOME path in Debian 10
4 ★ | 3 Vote