How to install composer on Ubuntu 18.04

Preliminaries required. Execute these commands to install dependencies and the composer installer file firstly.

sudo apt update

sudo apt install curl php-cli php-mbstring git unzip

cd ~

curl -sS https://getcomposer.org/installer -o composer-setup.php

Then verify the hash through this step;

Firstly click HERE for checking the HASH of downloaded file.

In this site you will see Installer Signature (SHA-384) HASH at the beginning of the page. Copy it and define it as HASH in your commandline with a command like this below;

HASH={{ $hash_you_got_from_the_link_above }}

For example;

HASH=93b54496392c062774670ac18b134c3b3a95e5a5e5c8f1a9f115f203b75bf9a129d5daa8ba6a13e2cc8a1da0806388a8

You may execute this command to check wether the hash and file is correct or not. PAY ATTENTION! if this code returns an error the downloaded file will be deleted and you will have to begin from the first line of this guide again. I recommend you to check the hash you defined in HASH variable twice before executing below command.

php -r "if (hash_file('SHA384', 'composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"

If the return message from above command like this -> “Installer verified” than you can install composer as a system-wide application with this command below;

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

Lastly and optionally you can check the version of composer with;

composer --version

That’s all.

kaynak: digitalocean