How to Install and use PHP Composer on Debian 11

Composer is a dependency manager for the programming language, PHP. It functions as some sort of project manager that helps the programmer manage dependencies that will be used on a project-to-project basis.

Installing Composer on Debian 11

First, need to check for any pending system updates,

apt update

apt upgrade

Install the required packages.

apt install wget php-cli php-zip unzip

Download the composer installer file.

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

Output:

root@server:~# wget -O composer-setup.php https://getcomposer.org/installer
--2021-08-06 18:37:10--  https://getcomposer.org/installer
Resolving getcomposer.org (getcomposer.org)... 54.36.53.46, 2001:41d0:302:1100::8:104f
Connecting to getcomposer.org (getcomposer.org)|54.36.53.46|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 58460 (57K) [application/octet-stream]
Saving to: ‘composer-setup.php’
composer-setup 100%  57.09K  --.-KB/s    in 0.01s         
2021-08-06 18:37:10 (5.34 MB/s) - ‘composer-setup.php’ saved [58460/58460]

To install Composer globally inside the /usr/local/bin directory by running the following command.

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

Output:

root@server:~# php composer-setup.php --install-dir=/usr/local/bin --filename=composer
All settings correct for using Composer
Downloading...

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

If a new Composer version is available, update the package by running the following command.

composer self-update 

Use Composer in PHP Project

mkdir ~/my-first-composer-project

cd ~/my-first-composer-project

Initialize a new composer.json and install the carbon package by running the following command.

composer require nesbot/carbon

Output:

root@server:~/my-first-composer-project# composer require nesbot/carbon
Do not run Composer as root/super user! See https://getcomposer.org/root for details
Continue as root/super user [yes]? yes
Using version ^2.51 for nesbot/carbon
./composer.json has been created
Running composer update nesbot/carbon
Loading composer repositories with package information
Updating dependencies

After the installation is complete, you can see that Composer created two files composer.json and composer.lock along with a vendor directory.

ls -l

Output:

root@server:~/my-first-composer-project# ls -l
total 28
-rw-r--r-- 1 root root    60 Aug  6 19:43 composer.json
-rw-r--r-- 1 root root 18210 Aug  6 19:43 composer.lock
drwxr-xr-x 6 root root  4096 Aug  6 19:43 vendor

Create a new file named testing.php and paste the following content.

<?php

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

use Carbon\Carbon;

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

Run the script by running the following command.

php testing.php

Output:

root@server:~/my-first-composer-project# php testing.php 
Now: 2021-08-06 19:49:13

Next, if you want to update the package then you can use the following command.

composer update