Table of Contents

Choose a different version or distribution

Introduction

Before, we begin talking about how to install PHP Composer on Debian 11, let's briefly understand – What is PHP Composer?

PHP Composer is a powerful tool used for managing dependencies in PHP projects. It simplifies the process of including external libraries, frameworks, and packages, saving time and effort for developers.

With Composer, you can easily define the required libraries and their versions in a simple configuration file. It then fetches and installs these dependencies automatically, ensuring that your project has all the necessary components to run smoothly. Composer promotes code reusability, maintainability, and collaboration among developers, making PHP development more efficient and organized.

In this tutorial, you will install PHP Composer on Debian 11.

Advantages of PHP Composer

  1. Dependency Management: Simplifies managing external libraries and packages, ensuring easy inclusion and version control.
  2. Automatic Installation: Fetches and installs dependencies automatically, saving developers time and effort.
  3. Code Reusability: Promotes reusing existing code and components, enhancing efficiency and reducing redundancy.
  4. Version Compatibility: Resolves version conflicts and ensures compatibility among dependencies.
  5. Collaboration: Facilitates collaboration among developers by providing a standardized way to manage project dependencies.

Prerequisites to Install PHP Composer on Debian 11

  • Shell access to a functioning Debian system with sudo privilege.
  • Set up PHP 5.3 or higher version on Debian.
  • It is advised that all newly installed systems adhere to the Debian 11 initial server setup.

Step 1 – Install PHP Composer on Debian

The official team offers a PHP script that you may use to install the composer on your machine. It can be downloaded using the command-line tools curl or wget. You may also download it using the PHP script.

Launch a terminal and execute:

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

In the current directory, a composer-setup.php file will be generated. The composer will be installed at the desired location when you run this PHP script. You can use --install-dir to set the binary location and --filename to set the binary name. You can either install composer globally accessible for all users and projects, or install it locally for a specific project.

  • To install composer globally, enter the following command:
php composer-setup.php --install-dir=/usr/local/bin --filename=composer
chmod +x /usr/local/bin/composer
  • You can also install composer within a particular application. This is beneficial for common hosting environments, where you don’t have sudo or root access. To install composer locally for a specific project, type the command given below:
cd /path/to/php-application && mkdir -p bin 
php composer-setup.php --install-dir=bin --filename=composer
chmod +x bin/composer

Replace /path/to/php-application with the directory of the actual application.

Execute the binary command line argument -v to view the installed composer version.

composer --version
Output

Composer version 2.2.6 2022-02-04 17:00:38

Step 2 – Upgrade PHP Composer

The PHP composer may automatically update to the most recent versions. Simply use the command below to update the PHP composer to the most recent version, if the composer is already installed on your machine.

composer self-update

Here, I already have the latest version of the composer. So, my terminal displays the following message:

Output

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

Working with PHP Composer

The composer is already set up and installed on your computer system. You may manage modules for your application with the aid of Composer. For instance, to add a new module to your application.

Change to the PHP application.

cd /path/to/php-application 

Install the psr/log module in the application by running the following command.

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
💡
If you got an error like git was not found in your PATH then install git using sudo apt install git -y and again running that command.

The composer will automatically create or update the composer.json file in the application's root directory. Now, the application can make use of the module's features.

The most recent version of the module will be installed with the help of the aforementioned command. Additionally, you can specify the version of the module you want to install for your application. Depending on whether the module is already installed, the package may be automatically upgraded or downgraded to the desired version.

composer require psr/log=1.0

The following command can be used to remove the module that is no longer needed.

composer remove psr/log

Each of the aforementioned commands also makes the necessary updates to the composer.json file.

FAQs to Install PHP Composer on Debian 11

Do I need to have PHP installed before installing Composer?

Yes, you need to have PHP installed on your Debian 11 system before installing Composer.

How can I check if PHP is already installed?

You can check if PHP is installed by running the command: php --version.

Can I use Composer globally after installation?

Yes, once installed globally, you can use Composer from anywhere on your Debian 11 system.

What is the purpose of the "php-mbstring" package in the installation process?

The "php-mbstring" package provides multibyte string functions that Composer requires to function properly.

Can I update Composer after installation?

Yes, you can update Composer to the latest version by running the command: composer self-update

Can I uninstall Composer from Debian 11?

Yes, you can uninstall Composer by removing the composer.phar file and the directory it was installed in.

Conclusion

In this tutorial, you installed PHP Composer on Debian 11. To grant access to all users and applications, you can install Composer globally. You can also install composer in a specific directory.

If you have any queries, please leave a comment below, and we’ll be happy to respond to them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.