How to Install and Use PHP Composer on Debian 10

Choose a different version or distribution

Introduction

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

PHP Composer is a powerful dependency management tool for PHP developers. It simplifies the process of managing libraries and packages in PHP projects. With Composer, developers can easily install, update, and remove dependencies, ensuring smooth integration of external code into their projects.

It resolves dependencies automatically, saving time and effort. Composer promotes code reuse, enhancing productivity and maintainability. It has become an indispensable tool in the PHP ecosystem, enabling developers to efficiently manage and organize their projects.

This tutorial will walk you through the process of installing Composer on Debian 10. We will also address a few FAQs on how to Install and Use PHP Composer on Debian 10.

Advantages of PHP Composer

  1. Dependency Management: Easily manage and integrate libraries and packages into PHP projects.
  2. Automatic Dependency Resolution: Composer automatically resolves dependencies, saving time and effort.
  3. Code Reusability: Promotes code reuse, enhancing productivity and maintainability.
  4. Efficient Updates: Quickly update dependencies to benefit from bug fixes and new features.
  5. Thriving Ecosystem: Composer is widely adopted, with a vast library of packages available for use in PHP projects.

Installing Composer on Debian

Make sure your Debian system has all the required packages installed before installing Composer:

sudo apt update
sudo apt install wget php-cli php-zip unzip

We will install Composer using the PHP written installer that comes with Composer.

You can download the installer using wget :

wget -O composer-setup.php https://getcomposer.org/installer

The aforementioned command will store the file in the current working directory as composer-setup.php.

Composer is a single-file CLI tool that may be set up either globally or specifically for a project. It is necessary to have sudo privileges for the global installation.

  • Just place the file in a directory in the system PATH to install Composer as a system-wide command that will be available to all users. Composer is installed in the /usr/local/bin directory with the following command:
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
Output

All settings correct for using Composer
Downloading...

Composer (version 1.10.10) successfully installed to: /usr/local/bin/composer
Use it: php /usr/local/bin/composer

Run composer in your terminal to start using Composer.

  • Download the following file into the root directory of your project to install composer locally:
sudo php composer-setup.php --install-dir=/path/to/project

As a result, a file named composer.phar will be downloaded. Navigate to the project directory and run php composer.phar to use Composer.

You can update the installation whenever a new Composer version is available by running the below command:

sudo composer self-update  

Getting Started with Composer

Now that Composer has been set up on your Debian system, we will walk you through the process of creating a PHP project.

First, create a directory that will serve as the project root and contain the composer.json file. This file defines your PHP project, along with PHP dependencies and other metadata.

To create the project directory and switch to it, use the following commands:

mkdir ~/my-first-composer-project
cd ~/my-first-composer-project

The next step is to use the composer require <package name> command to initialize a new composer.json file and specify the package you want to download. In this example, we will use a package named carbon to create a sample application that prints the current time.

To create a new composer.json file and install the carbon package, enter the following command:

composer require nesbot/carbon
Output

./composer.json has been created
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 5 installs, 0 updates, 0 removals
  - Installing symfony/translation-contracts (v2.1.3): Downloading (100%)         
  - Installing symfony/polyfill-php80 (v1.18.1): Downloading (100%)         
  - Installing symfony/polyfill-mbstring (v1.18.1): Downloading (100%)         
  - Installing symfony/translation (v5.1.3): Downloading (100%)         
  - Installing nesbot/carbon (2.38.0): Downloading (100%)         
symfony/polyfill-mbstring suggests installing ext-mbstring (For best performance)
symfony/translation suggests installing symfony/config
symfony/translation suggests installing symfony/yaml
symfony/translation suggests installing psr/log-implementation (To use logging capability in translator)
Writing lock file
Generating autoload files
5 packages you are using are looking for funding.
Use the `composer fund` command to find out more

Composer generates the composer.json file, and downloads and installs Carbon and each of its dependencies, as seen in the output.

If you use ls to display the directory of your project, you will notice that it contains two files, composer.json and composer.lock, as well as a vendor directory.

ls -l
Output

-rw-r--r-- 1 vegastack users   60 Aug 17 21:02 composer.json
-rw-r--r-- 1 vegastack users 6851 Aug 17 21:02 composer.lock
drwxr-xr-x 5 vegastack users 4096 Aug 17 21:02 vendor
  • vendor - It is the directory that stores the project dependencies.
  • composer.lock - This file keeps a list of all the installed packages, in addition to the version of the packages.
  • composer.json - This file describes the PHP project and all PHP dependencies.
PHP packages can be found in the Composer repository Packagist.

You can use PHP classes using Composer's autoload features without having to require or include any files.

Make a file and name it testing.php and paste the following code into it:

<?php

require __DIR__ . '/vendor/autoload.php';

use Carbon\Carbon;

printf("Now: %s", Carbon::now());

Let us examine each line of code individually.

The vendor/autoload.php file, which the Composer generates automatically, is included in the first line following the opening php tag. This file will automatically load all necessary libraries.

After that, Carbon\Carbon is aliased as Carbon, and in the final line, the Carbon now method is used to print the current time.

Run the script by entering the following:

php testing.php

The output should resemble the following:

Output

Now: 2022-12-14 21:08:45

If you need to update the project packages later, type:

composer update

The aforementioned command will search for updated versions of the installed packages. If an updated version is identified and its version constraint matches that of composer.json, Composer will update the package.

FAQs to Install and Use PHP Composer on Debian 10

How do I verify if Composer is installed correctly?

Run the command composer --version in the terminal. If Composer is installed correctly, it will display the installed version.

How can I use Composer to manage dependencies in my PHP project?

Create a composer.json file in your project's root directory, specify the required dependencies, and run composer install to install them.

How can I update dependencies with Composer?

Use the command composer update to update your project's dependencies to the latest compatible versions.

How do I autoload classes in my PHP project using Composer?

Define the autoloading configuration in your composer.json file and run composer dump-autoload to generate the autoload files.

Can I install specific versions of dependencies with Composer?

Yes, you can specify version constraints in the composer.json file to install specific versions or ranges of versions.

How can I remove unused dependencies with Composer?

Run the command composer remove <package-name> to remove a specific package or edit the composer.json file and run composer update.

Can Composer be used globally on Debian 10?

Yes, you can install Composer globally by placing the executable in a directory listed in the PATH environment variable.

Conclusion

Hope this tutorial helped you understand how to install Composer on Debian 10 and how to use it to create a basic PHP project. Check out the official documentation page, for more information about the Composer.

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