Install Composer: A Complete Guide

In this article, I will be showing you how to install Composer irrespective of which operating system you’re using.

What is Composer?

Composer is a dependency manager for PHP. It is like a go-to toolkit for PHP developers that can help reduce the development time and cost, as well as improve the security and stability of the applications.

In simple words, Composer allows you to declare the libraries your project depends on and helps you manage (install/update) them.

Nowadays, Composer comes pre-installed on majority of the hosting providers who cares for the developers because majority of the websites are managed and developed by the developers.


There are 3 most popular operating systems available in the market. So, I would explain on how you can install composer on all those operating systems.

  1. macOS or Linux (Both operating systems have similar steps and commands)
  2. Windows

Before proceeding, you must know how to access and use the Terminal on your system.

Installing Composer on macOS

There are multiple ways to install composer on macOS. The easiest way to install composer on macOS is using Homebrew.

Homebrew is the missing package manager for macOS (also works on Linux).

1. Install Homebrew

If you have not installed Homebrew then install it using the below mentioned command:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
2. Install Composer
brew install composer
3. Test the composer installation

To test whether composer is installed successfully or not, then type the command below:

composer

If the composer is installed successfully without any errors, then you will see the response as below followed by all the supported commands.

   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 2.0.2 2020-10-25 23:03:59

That’s it! You’ve successfully installed Composer on macOS.


Installing Composer on Linux

Please follow through the instructions step-by-step to successfully install composer on your Linux.

Note: This installation method will also run on macOS

1. Open the terminal

For Linux users: Press ctrl + alt + T to open the terminal.

For macOS users: Press cmd + space to open the spotlight search and type terminal and hit return

2. Download Composer using the below mentioned command
php -r "copy('[<https://getcomposer.org/installer>](<https://getcomposer.org/installer>)', 'composer-setup.php');"
3. Verify the installer’s signature

You need to verify the installer’s signature to ensure that the installer file is not corrupted while downloading the file. Use the below mentioned command to verify. Make sure you replace <unique-hash> with the latest SHA-384 from here because the hash changes every time a new version of the composer comes out.

php -r "if (hash_file('sha384', 'composer-setup.php') === '<unique-hash>') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
4. Install Composer Locally or Globally

In Local installation, the composer installation will be stored in your current working directory, and you have to specify the path before executing corresponding commands.

However, in Global installation, you will be allowed to run Composer from anywhere on your system by storing the installation in /usr/local/bin directory.

Commands you need to execute for local as well as global installation of composer:

Local Installation:

php composer-setup.php

Global Installation:

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

Once composer is installed successfully, you will see the message as mentioned below:

All settings correct for using Composer
Downloading...

Composer (version 1.10.5) successfully installed to: /usr/local/bin/composer
5. Test the composer installation

For locally installed composer, you need to type the path where you installed the composer followed by the term “composer” (without quotes).

/installation/directory/composer

For globally installed composer, you just need to type the term “composer” (without quotes).

composer

If the composer is installed successfully without any errors, then you will see the response as below followed by all the supported commands.

   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 2.0.2 2020-10-25 23:03:59
6. Remove the installer
php -r "unlink('composer-setup.php');"

That’s it! You have successfully installed Composer on Linux.


Installing Composer on Windows

Installing Composer on a Windows operating system is quite easy.

1. Download the installer

Click here to download the installer

Start the installation

Double-click the installer package to start the installation process and then click on the popup where you need to click on “Install for all users”. This option will install composer for all the users on Windows.

Install Composer: A Complete Guide 1
Start the installation of Composer using the installer for Windows

Now, choose you installation type and then click Next

Install Composer: A Complete Guide 2
Installation Options for the composer installation

Now, choose the path to the PHP command line which you want to use and then tick the box which says “Add this PHP to your path”, and then click Next

Install Composer: A Complete Guide 3
Select the path for the PHP command line

Composer also provides an ability to run via proxy server. So, if you’re using proxy server to connect to the internet then you need to tick the option which says “Use a proxy server to connect to internet” and provide a proxy url in the field below, and then click Next

Install Composer: A Complete Guide 4
Add proxy settings to your composer installation

Now, the composer installer is ready to install composer on your Windows. You need to review the settings you selected and then click. on Install

Install Composer: A Complete Guide 5
Composer is now ready to install on Windows

After successfully installing composer, the installer will provide you detailed information about the next steps before finalizing the installation which you should read and then click Next

Install Composer: A Complete Guide 6
Read the important information before completing composer installation

Now, at last, click Finish to complete the installation.

Install Composer: A Complete Guide 7
Composer installation completed successfully

Don’t forget to close your current terminal. You need to test running composer with a new terminal: This is important since the PATH only gets loaded when the terminal starts.

If the composer is installed successfully without any errors, then you will see the response as below followed by all the supported commands.

   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 2.0.2 2020-10-25 23:03:59

That’s it! You have successfully installed Composer on Windows.


I hope you enjoyed reading this article. Don’t forget to share this article with your friends and colleague.

Leave a Reply