Composer is basically 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.
First, 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
root@vps:~# wget -O composer-setup.php https://getcomposer.org/installer --2020-12-25 09:18:26-- https://getcomposer.org/installer Resolving getcomposer.org (getcomposer.org)... 18.104.22.168, 2607:5300:201:2100::4:d105 Connecting to getcomposer.org (getcomposer.org)|22.214.171.124|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 58460 (57K) [application/octet-stream] Saving to: ‘composer-setup.php’ composer-setup.php 100%[===================>] 57.09K --.-KB/s in 0.07s 2020-12-25 09:18:26 (838 KB/s) - ‘composer-setup.php’ saved [58460/58460]
To install Composer globally inside '/usr/local/bin' directory by running the following command.
php composer-setup.php --install-dir=/usr/local/bin --filename=composer
root@vps:~# php composer-setup.php --install-dir=/usr/local/bin --filename=composer All settings correct for using Composer Downloading... Composer (version 2.0.8) successfully installed to: /usr/local/bin/composer Use it: php /usr/local/bin/composer
To install Composer locally by running following command.
php composer-setup.php --install-dir=/path/to/project
If a new Composer version is available, update the package by running the following command.
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
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 Using version ^2.43 for nesbot/carbon ./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.43.0) - Locking symfony/polyfill-mbstring (v1.20.0)
After the installation is complete, you can see that Composer created two files
composer.lock along with a
root@vps:~/my-first-composer-project# ls -l total 20 -rw-r--r-- 1 root root 60 Dec 25 09:22 composer.json -rw-r--r-- 1 root root 15627 Dec 25 09:22 composer.lock drwxr-xr-x 6 root root 82 Dec 25 09:22 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 script by running the following command.
root@vps:~/my-first-composer-project# php testing.php Now: 2020-12-25 09:25:21
Next, if you want to update the package then you can use the following command.