Here is a quick guide on how to install and get started with Composer on Ubuntu.

# TL;DR version

Run the following commands in your ubuntu terminal

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === 'e5325b19b381bfd88ce90a5ddb7823406b2a38cff6bb704b0acc289a09c8128d4a8ce2bbafcd1fcbdc38666422fe2806') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"
mv composer.phar /usr/local/bin/composer

# Detailed version

Here are the detailed explanation of steps.

# 1. Install Dependencies

Composer installer files comes as a PHP installer, and to run it you must have PHP installed in your ubuntu machine.

Run the following command to check if you have php installed in your system

php -v

If not, then Run the following commands to Install PHP

First, update the package manager cache by running:

sudo apt-get update
sudo apt-get-repository ppa:ondrej/php
sudo apt-get update
sudo apt-get install -y php 

Here is the detailed guide on How to Install PHP on Ubuntu 20.04

# 2. Download and Install Composer

Once you have the PHP installed in your machine, we can now go ahead and get started with Composer installation.

Navigate to your root directory and run the following command to download the composer-setup.php (Composer Installer) file

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"

This will create a new file named composer-setup.php

Next Up, Run the following command which matches the hash of downloaded file to the specified hash in the command. This basically denotes that the installer file is correct and safe to run.

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

If the output is Installer Verified that means that you can now proceed to next step.

php download composer file

Next Up, Run the installer by executing following command

php composer-setup.php

php composer-setup.php

Running this command will run the installer and create a bundled PHAR file named composer.phar. We now have the composer installed and we can run it by executing the command php composer.phar

Let's now execute the command to delete the installer file since it's no longer required

php -r "unlink('composer-setup.php');"

Time to take composer global. To be able to execute composer from anywhere in your machine, We need to move the executable phar file to a directory from where linux can access it globally.

sudo mv composer.phar /usr/local/bin/composer

There you go, We now have composer installed.

Run the following command to check if composer works globally.

composer -v

composer -v

Comments