How To Install Composer on Windows, macOS, and Linux


If you know your way around the PHP programming language, you’ve probably used PHP libraries to make your code simpler and easier to manage. Composer is a useful open-source tool that any developer can use to manage those dependencies and libraries. Many well-known frameworks, like Laravel and Drupal, have development processes that begin with installing Composer.

Here’s how to add Composer to your development environment on Windows, Linux, and macOS and how to deploy a Composer-based PHP application on a hosting platform like Kinsta’s.

What Is Composer?

Composer is responsible for managing libraries on a per-project basis. It can be installed directly in the project’s directory, or it can be installed globally and accessed from anywhere in the system. It allows you to declare the libraries on which the code depends. It also finds out the version of related packages that are needed and installs them for you.

The requirements of each project are declared in a JSON file (named composer.json), which allows Composer to evaluate which package version is the best match for an application dependency. This streamlines the development process, meaning a developer can devote more of their time to building and improving their core application.

Composer Installation Instructions

To work with Composer, you’ll need to install PHP on your system.

Depending on your setup, you have different options for installing Composer, each involving slightly different steps. The following sections explain how to install Composer on the following operating systems:

Installing Composer on Linux

Composer can be installed using the terminal in Linux. For this tutorial, you’ll use Ubuntu as an example. If you’re using a different Linux distribution, then follow the instructions for downloading Composer provided by that distribution’s website.

To install Composer on Ubuntu, first download Composer from its website:

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

You can now proceed to install Composer either locally or globally on your system.

Run the following command for local installation:

php composer-setup.php

By installing Composer locally, you can ensure that the PHP project is compatible with the version of Composer you have installed. This will help prevent compatibility issues and make sure that your PHP project is running smoothly on a hosting platform like Kinsta.

Alternatively, the following commands will install Composer globally, which allows the dependency manager to run anywhere in the system without specifying the full path:

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

After you’ve completed the installation using your chosen method, remove the installer from your computer using the following commands:

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

Finally, run the following command in your terminal to test whether Composer has been installed successfully:

composer

When you run Composer without any arguments, program help is displayed, starting something like this:

Screenshot of the Composer help display.
An example of Composer’s help display.