Composer - A Dependency Manager for PHP
To install composer in an ubuntu machine we need to follow the below steps. Please note we can install composer locally for only one user and globally for all users on the server
To install composer locally for one user we can execute the below commands under a specific directory where we need the composer to be installed. ( Ref: https://getcomposer.org/download/ )
Let's create a directory initially and execute the below-mentioned commands one by one
mkdir composer-dir
cd composer-dir
Step -1: Download the installer to the current directory, with the below command
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
Note: no output will be shown
Step -2: Verify the installer SHA-384, which you can also cross-check here
php -r "if (hash_file('sha384', 'composer-setup.php') === '906a84df04cea2aa72f40b5f787e49f22d4c2f19492ac310e8cba5b96ac8b64115ac402c8cd292b8a03482574915d1a8') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
If the above command executes successfully then we get an output like this
Step -3: Run the installer
php composer-setup.php
If the above command executes successfully then we get an output like this
Step -4: Remove the installer
php -r "unlink('composer-setup.php');"
Note: no output will be shown
Once the above steps are completed successfully we can now find a composer.phar file under the directory
To make the composer work globally on the machine we need to put the composer.phar into a directory on your PATH, so we can simply call composer from any directory (Global install), using for example.
now let's switch to root and move the file composer.phar to location /usr/local/bin/composer
sudo mv composer.phar /usr/local/bin/composer
Now from any normal (local) user, we can run the command composer
composer
We can also use below command to check the composer version with -V option
composer -V
As of now, the composer is installed on the server, now let's check some additional options and features
We can use composer to install 3rd party libraries with all their dependencies, actually, composer reduces the difficulty of installing these 3rd party libraries.
For example, we are trying to install a 3rd party library called Dompdf with the help of composer, we are following the same steps mentioned in its official git repo. (ref: https://github.com/dompdf/dompdf)
composer require dompdf/dompdf
Once the command is executed we get an output like this
PHP related reference: https://www.fosstechnix.com/how-to-install-php-on-ubuntu-20-04/