How to Install Composer: A Step-By-Step Guide and Best Practices

How to Install Composer: A Step-By-Step Guide and Best Practices

Download eBook: Speed Up Your Website. 8 Practical Tips That Work

In this article, we are going to show you how to install and use Composer on various platforms. Composer is a dependency manager for PHP. It is a simple and reliable tool that developers use to manage and integrate external packages or libraries into their PHP-based projects. This way, they don’t have to build their webpages or web applications from the ground-up.

To help you master this tool, you will also learn how to create a basic PHP project.

Before you learn how to install Composer, make sure that you have access to the command line interface on your system or server.

Installing Composer

This section will show you how to install Composer on shared hosting and operating systems like Linux, macOS, and Windows.

1. Installing Composer on Shared Hosting, Linux, or macOS

The commands to install Composer on shared website hosting, Linux (PC or server), and macOS are the same.

Note that Composer comes pre-installed on Hostinger’s Premium and Business shared hosting plans. If you are using one of them, skip this part. However, if you find out that it’s outdated, you will need to update it by installing the correct version locally.

Follow this instruction to know how to install Composer on your system:

  1. Connect to your hosting account using SSH connection. You can learn how to do so from this SSH tutorial.
  2. Download Composer from the official website using the following command:
    php -r "copy('', 'composer-setup.php');"
  3. Verify the installer’s signature (SHA-384) to ensure that the installer file is not corrupt. Enter:
    php -r "if (hash_file('sha384', 'composer-setup.php') === 'e0012edf3e80b6978849f5eff0d4b4e4c79ff1609dd1e613307e16318854d24ae64f26d17af3ef0bf7cfb710ca74755a') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"

    The long string of characters in the above command (‘e0012edf…’) is the installer’s signature. It changes every time a new version of Composer comes out. Therefore, be sure to fetch the latest SHA-384 from this page.

  4. Once it’s done, you may install Composer locally or globally. Local installation means that the dependency manager will be stored in your current directory, and you have to specify the path before executing corresponding commands. Meanwhile, global installation allows you to run Composer from anywhere on your system by storing it in /usr/local/bin directory. Here is how to complete both methods:
    • Local installation:
      php composer-setup.php
    • Global installation:
      php composer-setup.php --install-dir=/usr/local/bin --filename=composer

      You will get this result:

      All settings correct for using Composer
      Composer (version 1.10.5) successfully installed to: /usr/local/bin/composer
  5. Once it’s done, remove the installer:
    php -r "unlink('composer-setup.php');"
  6. Test the Composer installation:

    The command line will return with this result:

      / ____/___ ____ ___ ____ ____ ________ _____
     / / / __ / __ `__ / __ / __ / ___/ _ / ___/
    / /___/ /_/ / / / / / / /_/ / /_/ (__ ) __/ /
    ____/____/_/ /_/ /_/ .___/____/____/___/_/
    Composer version 1.10.5 2020-02-12 16:20:11

2. Installing Composer on Windows

Getting started with Composer on a Windows machine is a bit different. No command-line instructions are necessary for downloading and installing the software.

Simply follow these steps:

  1. Install PHP on your computer. We recommend using XAMPP for this purpose, as the process is straightforward and you can complete it in a few minutes.
  2. Once XAMPP is installed, download the latest version of Composer.
  3. Run Composer installation wizard. When it asks you to activate the developer mode, ignore it and continue with the installation process.Composer installation wizard on Windows
  4. Another window will pop up and ask you to locate the PHP command line. By default, it is in C:/xampp/php/php.exe. After specifying the location, click Next.
  5. You will be prompted with Proxy Settings. Leave the box unchecked and skip this part by hitting Next. Then, on the last window, click Install.
  6. After completing the installation, open the command prompt. Press CTRL +R, type in “cmd,” and click OK.
    The Run application to open command prompt on Windows
  7. Enter the following command:

Great job! You now have Composer installed on your Windows computer. The installer will automatically add Composer to your PATH variable. Now you can open the command prompt and run the software from anywhere.

Generating and Understanding composer.json

Now comes the interesting part — using Composer in your PHP project.

To achieve this, you need to generate a composer.json file. You can think of it as a way to lookup data from a list for Composer. This file contains packages (dependencies) that should be downloaded.

Furthermore, composer.json also checks for version compatibility with your project. This means if you are using an older package, composer.json will let you know in order to avoid future issues.

You have the option to create and update composer.json yourself. However, considering that this is a tutorial on automating redundant tasks, we don’t recommend you to create the file manually.

Let’s demonstrate the usefulness of composer.json by creating a sample project.

Our project is a simple PHP timer, which allows developers to find out how much time code takes to execute. This is highly useful for debugging and optimization purposes.

You can follow these steps:

  1. Create a new directory for the project. Since our project is a timer, we’ll simply name the folder phptimer. To do this, execute the following command:
    mkdir phptimer
  2. Enter the newly created directory:
    cd phptimer
  3. Find a package or library for the project. The best place to achieve that is Packagist, where you’ll find tons of libraries to help your project development. For this tutorial, we need a timer package. To get it, simply type timer in the search bar:The interface of Packagist website where you can download dependency for Composer
    As you can see, several timer packages are available and each has a name and a small description of what it does. In this example, we choose phpunit/php-timer as it has the most downloads and most GitHub stars.
  4. Specify the desired package so Composer can add it to your project:
    composer require phpunit/php-timer

    The output will show the version of phpunit/php-timer:

    Using version ^1.0 phpunit/php-timer

The caret (^) symbol is defined as the option for maximum interoperability. This means Composer will always update the package until a certain version breaks the package in some way.

In our case, the package update range is >=1.0.9 <2.0.0, as version 2.0.0 will break the backward compatibility. For detailed information about versioning in Composer, visit the documentation page.

After executing the above command, your project directory will have two new files — composer.json and composer.lock — and a folder named vendor. This is the directory where Composer will store all of your packages and dependencies.

Using Autoload Script

Your project is almost good to go, and the only thing left to do is load the dependency into your PHP script. And fortunately, Composer’s autoload file helps you to complete this process faster.

To use autoloading, write the following line before you declare or instantiate new variables in your script:

require __DIR__ . '/vendor/autoload.php'

We’ll give you an example to help you understand better.

Let’s say we want to test our phptimer project:

  1. Open nano text editor to create a script named demo.php.
    nano demo.php

    Then, paste the following lines to your file:

    require __DIR__ . '/vendor/autoload.php'
    // your code
    $time = Timer::stop();
    print Timer::secondsToTimeString($time);
  2. Run the script:
    php demo.php

    The terminal should display an output similar to the following:

    0 ms

Updating Your Project Dependencies

Lastly, you have to know how to update your packages. This can be done in one of two ways:

  • Universal update. To check and install updates for all of your packages and dependencies at once, type in the following command:
    composer update
  • Package-specific update. Execute this command to check the updates for one or more specific packages:
    composer update vendor/package vendor2/package2

Remember to replace vendor/package with the name of the package that you want to update.

By running the update command, Composer also updates the composer.json and composer.lock files to match the current state of your project dependencies.


Composer helps developers in managing dependencies of PHP projects. Thanks to this software, they can easily integrate and manage open source packages in a single place.

What’s great, Composer can also resolve dependencies on a per-project basis. Thus, developers can control packages for each project and keep the project size in check.

In this article, you have learned how to install and use Composer effectively. To summarize, let’s review all the steps once again:

  1. Install Composer on shared hosting, Linux, macOS, or Windows system.
  2. Generate and understand the composer.json file.
  3. Use Autoload script to load dependencies into your PHP file.
  4. Update your project dependencies.

We hope that by following our guide, you have a strong foundation to create amazing projects with PHP. Good luck and feel free to ask any questions in the comment section below.

Was this tutorial helpful?

The author

Domantas G.

Domantas leads the content and SEO teams forward with fresh ideas and out of the box approaches. Armed with extensive SEO and marketing knowledge, he aims to spread the word of Hostinger to every corner of the world. During his free time, Domantas likes to hone his web development skills and travel to exotic places.