How to install Composer on Ubuntu

Howto Ubuntu

Share

In this tutorial we will learn to install Composer on Ubuntu.

What is Composer?

Composer is a Dependency Manager for PHP.

Prerequisite

It is assumed that you have PHP installed on your Ubuntu machine.

Click here to check out how to install Php 7 on Ubuntu.

So, go ahead and open Terminal and let's start installing Composer on our Ubuntu machine.

I will cd to my Downloads directory and will download the composer file there.

Step #1: Get composer-setup.php

Run the following command to get the composer-setup.php file.

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

The above command will download the composer-setup.php file in the directory where you are current in.

You can run the pwd command to know the current directory.

If we ls we will get to see the downloaded file.

$ ls -la
total 308
drwxr-xr-x  2 yusuf yusuf   4096 Jun 17 13:35 .
drwxr-xr-x 22 yusuf yusuf   4096 Jun 17 11:05 ..
-rw-r--r--  1 yusuf yusuf 305459 Jun 17 13:35 composer-setup.php

Step #2: Verify Installer

Now run the following command to verify the Composer installer.

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

On success we will get the following output.

Installer verified

Please Note! If the composer-setup.php file is changed in future then the above hash will not work. In that case you can check the official website https://getcomposer.org/

Step #3: Get composer.phar

Run the following command to get composer.phar file.

$ php composer-setup.php

On success you will get to see the following output.

All settings correct for using Composer
Downloading...

Composer (version 1.6.5) successfully installed to: /home/sk/Downloads/composer.phar
Use it: php composer.phar

Step #4: Remove the composer-setup.php file

Once we have the composer.phar file we can remove the composer-setup.php file by running the following command in the terminal.

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

Using composer

To use composer we have to run the following command in the directory containing the composer.phar file.

$ php composer.phar
   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 1.6.5 2018-05-04 11:44:59

Note! At the time of writing this tutorial I was using v1.6.5. You may get a different version and that is totally fine.

Using composer by typing composer

If we want to use Composer by just typing composer in the terminal then we have to move the composer.phar in the /bin directory.

Run the following command in the terminal to move composer.phar file to the /bin directory.

$ sudo cp composer.phar /bin/composer

Now we will be able to run composer by just typing composer in the terminal.

$ composer
   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 1.6.5 2018-05-04 11:44:59

Thanks for reading. Please share if you find this tutorial and this website helpful and interesting.

Have fun developing :-)

Share