How to Install and use PHP Composer on Debian 12
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 12
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@vps:~# wget -O composer-setup.php https://getcomposer.org/installer
--2023-06-13 20:51:32-- https://getcomposer.org/installer
Resolving getcomposer.org (getcomposer.org)... 142.44.245.229, 2607:5300:201:2100::4:d105
Connecting to getcomposer.org (getcomposer.org)|142.44.245.229|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 58140 (57K) [application/octet-stream]
Saving to: ‘composer-setup.php’
composer-setup.php 100%[=============================================================>] 56.78K --.-KB/s in 0.08s
2023-06-13 20:51:32 (738 KB/s) - ‘composer-setup.php’ saved [58140/58140]
root@vps:~#
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@vps:~# php composer-setup.php --install-dir=/usr/local/bin --filename=composer
All settings correct for using Composer
Downloading...
Composer (version 2.5.8) successfully installed to: /usr/local/bin/composer
Use it: php /usr/local/bin/composer
root@vps:~#
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@vps:~/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
Info from https://repo.packagist.org: #StandWithUkraine
./composer.json has been created
Running composer update nesbot/carbon
Loading composer repositories with package information
Updating dependencies
Lock file operations: 5 installs, 0 updates, 0 removals
- Locking nesbot/carbon (2.67.0)
- Locking symfony/polyfill-mbstring (v1.27.0)
- Locking symfony/polyfill-php80 (v1.27.0)
- Locking symfony/translation (v6.3.0)
- Locking symfony/translation-contracts (v3.3.0)
....
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@vps:~/my-first-composer-project# ls -l
total 28
-rw-r--r-- 1 root root 60 Jun 13 20:55 composer.json
-rw-r--r-- 1 root root 16387 Jun 13 20:55 composer.lock
drwxr-xr-x 6 root root 4096 Jun 13 20:55 vendor
root@vps:~/my-first-composer-project#
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@vps:~/my-first-composer-project# php testing.php
Now: 2023-06-13 20:58:35root@vps:~/my-first-composer-project#
Next, if you want to update the package then you can use the following command.
composer update
Output:
root@vps:~/my-first-composer-project# composer update
Do not run Composer as root/super user! See https://getcomposer.org/root for details
Continue as root/super user [yes]? yes
Composer is operating significantly slower than normal because you do not have the PHP curl extension enabled.
Loading composer repositories with package information
Updating dependencies
Nothing to modify in lock file
Installing dependencies from lock file (including require-dev)
Nothing to install, update or remove
Generating autoload files
5 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
No security vulnerability advisories found
root@vps:~/my-first-composer-project#
Done!
CrownCloud - Get a SSD powered KVM VPS at $4.5/month!
Use the code WELCOME
for 10% off!
1 GB RAM / 25 GB SSD / 1 CPU Core / 1 TB Bandwidth per month
Available Locations: LAX | MIA | ATL | FRA | AMS