PHP Composer is a dependency manager for PHP. Dependency managers aid in application development and the process of invoking libraries and frameworks. Composer is a dependency manager for PHP with support for library and framework dependencies. It helps you manage your project’s dependencies, whether they are from Packagist, Github, or elsewhere.

Composer is a real timesaver, especially when you need to use several different libraries that are incompatible with each other. Downloading and installing these packages manually can take hours, depending on the size of the project. Composer will manage all dependencies for your application or library and download them in one go allowing you to quickly get back to coding instead of spending time compiling code and managing external libraries.

When we use PHP composer, we define our project’s dependencies in a single text file called “composer.json.” This file contains the names and version numbers (and optionally URLs ) of the external libraries that we want to use. The Composer then uses this file to download and install all the external library dependencies into our project folder and creates a dependency tree based on these details.

This means if you add new libraries to your project, you should update your composer.json file with their version number, and it will download them for you. It also allows you to keep track of the versions used in production, staging, or development easily by defining which environment it is for through an environment variable, which you set once when starting up your application, at the beginning of bootstrap, and then forget about it afterward because it will always get altered when needed changing anything else in your codebase automatically.

In this article, we will show you how to install and use PHP Composer on a Debian 11 server. The following steps will walk you through downloading and installing the package onto your Debian 11 server. Once that is done, it will be time to create a simple project and show you how to use Composer to download and install some packages to your project.

Prerequisites

  • A server running Debian 11.
  • A non-root user with sudo privileges is recommended.
  • PHP Composer requires PHP 5.3.2 and above. It works best with PHP 7.0 and above, but you might get away with using PHP 5.6 or 7.1. Make sure PHP 5.3.2 is installed on the server.

Updating the System

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

Run the following command to update the system.

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

Once the upgrade process is completed, run the following command to install the necessary dependencies.

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

After the system is fully updated, reboot the server. Run the following command to reboot the system. When making changes to the system, such as hardware configuration, kernel, or packages updating, it is important to restart the computer to allow the changes to take effect. For example, if you have just updated the kernel, reboot the system will load the new kernel after installation.

sudo reboot -r now

Once the reboot is completed, log back in using the same non-root user and continue to the next step.

Installing PHP Composer

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

The PHP Composer developer provides an install script which is written in PHP, to make the installation process easy. We will download the script, check its signature to make sure it hasn’t been corrupt, and run the installer.

First, download the latest version of the PHP Composer install script from the composer website using the curl command.Advertisement

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

Now, run the command below to check the PHP script’s signature. This step is to make sure the installer hasn’t been tampered with (i.e., corrupted or modified) during its 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 should look like the one below. This output verifies that the installer is not corrupted. The HASH may change in 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 exit printing Installer corrupt 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 Debian linux shell

In short, there are two ways to install Composer on your Debian 11 system: locally or globally.

Installing Composer globally allows you to use the composer command from any directory. You can easily update your dependencies from any project directory from the same terminal with Composer without having to move back and forth between directories. To install Composer globally, 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 named composer in the /usr/local/bin directory, and make it available to all users.

You’ll see the following output.

How to Install and Use PHP Composer on Debian 11 Debian linux shell

To test that Composer has been installed correctly, run the following command to call the composer executable.

composer

The output should look like this.

How to Install and Use PHP Composer on Debian 11 Debian linux shell

Local installation is for installing Composer in your home directory or somewhere else inside the directory you specify because you will not need access to the composer command from outside that directory unless using a symbolic link.

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

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

Testing the PHP Composer Installation

Now that you have successfully installed Composer on your Debian 11 system, it is time to test the installation. To do so, we will create a simple project and download some packages for your project using PHP Composer.

First, create a directory to hold the project files and move into it by running the following command.

cd && mkdir example_composer_project && cd example_composer_project

Once you are inside the project directory, run the following command to initialize a composer.json file for your project. In this example, we will install the nesbot/carbon package for our project.

composer require nesbot/carbon

You’ll see the following output.

How to Install and Use PHP Composer on Debian 11 Debian linux shell

As you can see in the output, PHP Composer creates a new composer.json file for your project. This file is created with the minimum requirement of the nesbot/carbon package to be installed in your project.

PHP Composer also installs the latest stable version of the nesbot/carbon package and its dependencies to your project directory.

You can use the ls command to list all files in your project directory.

ls

You’ll see the following output.

How to Install and Use PHP Composer on Debian 11 Debian linux shell

As you can see in the output, in your project directory, a new composer.json file has been created to allow PHP Composer to keep track of the version numbers for all packages you have in your project.

It also contains a new composer.lock file, which is used to lock the version numbers of all packages. And a new vendor directory where Composer stores all dependencies for your project.

Conclusion

In this tutorial, you have learned how to install PHP Composer on Debian 11. You know now that you can use it for local installations or global installation if you want to access the composer command from anywhere on your system. And you have learned how to use Composer for your project by installing new dependencies using PHP Composer.