January 8, 2021
5min Read
Domantas G.
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.
Install and use Composer with powerful web hosting solutions!
This section will show you how to install Composer on shared hosting and operating systems like Linux, macOS, and Windows.
The commands to install Composer on shared 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:
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
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.
php composer-setup.php
php composer-setup.php --install-dir=/usr/local/bin --filename=composer
You will get this result:
All settings correct for using Composer Downloading... Composer (version 1.10.5) successfully installed to: /usr/local/bin/composer
php -r "unlink('composer-setup.php');"
composer
The command line will return with this result:
______ / ____/___ ____ ___ ____ ____ ________ _____ / / / __ / __ `__ / __ / __ / ___/ _ / ___/ / /___/ /_/ / / / / / / /_/ / /_/ (__ ) __/ / ____/____/_/ /_/ /_/ .___/____/____/___/_/ /_/ Composer version 1.10.5 2020-02-12 16:20:11
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:
composer
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.
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:
mkdir phptimer
cd phptimer
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.
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 '/vendor/autoload.php'
We’ll give you an example to help you understand better.
Let’s say we want to test our phptimer project:
nano demo.php
Then, paste the following lines to your file:
<?php require __DIR__ . '/vendor/autoload.php' Timer::start(); // your code $time = Timer::stop(); var_dump($time); print Timer::secondsToTimeString($time);
php demo.php
The terminal should display an output similar to the following:
double(1.0893424438611E-5) 0 ms
Lastly, you have to know how to update your packages. This can be done in one of two ways:
composer update
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:
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.
January 12 2018
Thanks for this. It was straight forward and very easy to follow. I really appreciate the help.
November 04 2018
Danke schön . Ich habe viel gelesen , aber deine Artikel ist am besten . Danke wieder
August 15 2019
thanks bro
January 31 2020
Thanks so much, was happier i found the information here.
June 23 2020
great post!
August 31 2020
This guide was simple to understand and usefull
Gediminas B.
Replied on November 06 2018
Hello, Nour. I'm glad to hear you found the article helpful. Viel Glück!