How To Install And Use Composer

How To Install And Use Composer

What is Composer?

If you are here then you might have been managing your dependencies manually, which is such a pain, or you are new to this. Either way, a Composer will help you! A Composer is a tool that automates the download of packages. You can tell a Composer the package and which version of it you want and it would go ahead and download it for you. It’s basically a dependency manager.

Installing Composer

The installation is going to be the same for shared hosting, Linux and macOS and different for windows.

On Shared Hosting, Linux and macOS

You can use the command line to execute the installation. Connect to your hosting account through SSH connection and follow these steps:

Step 1.  Enter the following command in the terminal:

php -r "copy('',    'Composer-setup.php');"

This will check for the latest version of the Composer and then download it.

Step 2. Then you’ll need to make sure that the file you’ve just downloaded isn’t corrupted. Use the following command:

php -r "if (hash_file('sha384', 'Composer-setup.php') === 
    echo 'Installer verified'; 
      echo 'Installer corrupt'; 
echo PHP_EOL;"

So you’re ready for the installation. You can install the Composer either locally, storing the dependency manager in your current directory, or globally, in the usr/local/bin directory. Basically, you’ll need to explicitly state the path before executing commands if you install locally or you can install it globally making it system-wide executable.

Step 3. Use the following command if you wish to install Composer locally:

php Composer-setup.php

To install it globally, use the following command:

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

Step 4. Enter the following command to delete the installer after the installation:

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

On Windows

Installing Composer on Windows involves different steps from the ones discussed above.

Step 1. Install XAMPP to install PHP on your system.

Step 2. Now go to the following link to download Composer:

Things to keep in mind while installing:

The installer will ask you if you want to install it in developer mode. Make sure not to install it in developer mode.

Installing and using composer

You’ll also need to locate the PHP command line which, by default, is C:/xampp/php/php.exe

Installing and using composer

Also, leave the proxy settings box unchecked.

Step 3. You have successfully installed Composer on your system. Now open the command prompt and enter the following command:


Installing and using composer

Understanding Composer

To understand Composer we’ll make a simple PHP timer. Just follow these steps and you’ll be able to make you PHP timer which will tell you the time the code takes to execute:

Step 1. Make a new directory and execute the following command by replacing ‘name’ with the name you wish to give it:

mkdir phptimer

Step 2. Now you’ll have to enter the directory you’ve just created:

cd phptimer

Step 3. Now you’ll need to log on to the Packagist (feel free to use something else but this is the best place to find libraries for your project development) and search for ‘timer’. Choose the one with the most starts and most downloads from the search result.

Installing and using composer

Step 4. Use the following command after replacing ‘something/package-name’ by the name of the package you chose:

Composer require phpunit/php-timer

If you look up in your project directory now you will find two new files and one new folder, namely Composer.json, Composer.lock, and vendor. Vendor will be used as the directory to save all the packages and dependencies.

Now all you need to do is load the dependencies in your PHP script and your project would be up and running. You can either do the process manually or save a lot of time and energy by using Composer’s autoload file. And using it is fairly simple. So before declaring a new variable in the script you’ll have to write the following code:

1.require '/vendor/autoload.php'

Following are the steps to test the timer project we made and you can tweak the code a little bit to do it for some other project similarly:

Step 1. Use the following code to create a script in the nano text with the name demp.php after replacing demo.php with the name of your choice:

nano demo.php

Step 2. Now head to the file and put the following code there:


require __DIR__ . '/vendor/autoload.php'


// your code starts here

$time = Timer::stop();


print Timer::secondsToTimeString($time);

Step 3. Use the following code to run the script:

php demo.php

Now your project is good to go but you must also learn to update your packages. You can either do a universal update, which will check for updates available for all your packages and install them, or a package-specific update, which will check for updates and install them for just the specified packages.

Use the following code for a universal update:

Composer update

Use this code for package-specific updates, after changing random/package and randon2/package2 with the actual name of the package that has to be updated:

Composer update vendor/package vendor2/package2

Now you are all set to create great projects in PHP using Composer.

How To Install PHP Composer on Ubuntu 18.04

This tutorial will help you install, configure, and use Composer on your Ubuntu
2 min read
David Malcom
David Malcom

How To Install PHP Composer on a CentOS 7 VPS or Dedicated Server

This tutorial will help you install, configure, and utilize Composer on your Cen
3 min read
David Malcom
David Malcom

How to Manage Drupal 8 Web Servers Using Composer

This Tutorial will show how to manage Drupal 8 web servers, including configurat
3 min read
Jeffrey Scott
Jeffrey Scott
Hosting Expert provides professional web hosting reviews fully independent of any other entity. Our reviews are unbiased, honest, and apply the same evaluation standards to all those reviewed. While monetary compensation is received from a few of the companies listed on this site, compensation of services and products have no influence on the direction or conclusions of our reviews. Nor does the compensation influence our rankings for certain host companies. This compensation covers account purchasing costs, testing costs and royalties paid to reviewers.
Click to go to the top of the page
Go To Top