Install Composer: A Complete Guide

If you’ve ever worked on a PHP project, you know that managing scripts and packages can be tricky. Each one is like a puzzle piece with its own special place, and it’s up to you to fit them all together. This is where Composer comes in, like a helpful friend, organizing and managing all these pieces for you.

Composer is like the backstage crew at a concert, ensuring that all the different elements of your PHP project work together smoothly. It manages all the tasks that you don’t need to worry about, so you can focus on the important part – coding.

But before Composer can start helping you, it needs to be installed on your computer. That’s what we’re going to walk through today. Whether you’re new to PHP or have been coding for years, this guide will show you step-by-step how to install Composer on various operating systems. So, let’s roll up your sleeves and get started.

What is Composer?

Composer is a tool for dependency management in PHP. It allows you to declare the libraries your project depends on, and it will manage (install/update) them for you. 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 software development, a “dependency” refers to a software package that a given project relies on to function correctly. For example, your project might require a certain library to handle HTTP requests or to interact with a database. These libraries are your project’s dependencies.

Composer is not a package manager in the same sense as Yum or Apt are. Yes, it deals with “packages” or libraries, but it manages them on a per-project basis, installing them in a directory (e.g., vendor) inside your project. By default, it does not install anything globally.

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.

How to Install 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.


How to Install 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.


How to Install 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.



Conclusion

Installing Composer is a significant first step towards efficiently managing your PHP projects. With this tool in your developer’s arsenal, you’re now equipped to handle dependencies in a structured, streamlined manner, saving you time and potential headaches down the line.

Remember, the power of Composer lies not just in installing dependencies but also in tracking, updating, and maintaining them. It’s a living part of your project, helping you stay on top of your game.

Whether you’re building a small personal project or a complex application for a large team, Composer is a tool that can grow with you, adapting to your needs. With this guide, you now know how to install Composer and set the stage for a smoother, more efficient PHP development experience.

As always, keep exploring, keep learning, and keep coding. The world of PHP and Composer is vast, and this is just the beginning. Happy coding!

Mehul Gohil
Mehul Gohil

Mehul Gohil is a Full Stack WordPress developer and an active member of the local WordPress community. For the last 10+ years, he has been developing custom WordPress plugins, custom WordPress themes, third-party API integrations, performance optimization, and custom WordPress websites tailored to the client's business needs and goals.

Articles: 127

Leave a Reply

Your email address will not be published. Required fields are marked *