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. link
https://getcomposer.org/Composer-Setup.exe
3. Run Composer installation wizard. When it asks you to activate the developer mode,
ignore it and continue with the installation process.
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.
7. Enter the following command:
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.
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:
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:
<?php
require __DIR__ . '/vendor/autoload.php'
Timer::start();
// your code
$time = Timer::stop();
var_dump($time);
print Timer::secondsToTimeString($time);
2. Run the script:
php demo.php
The terminal should display an output similar to the following:
double(1.0893424438611E-5)
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.