Don’t like the intro? Skip to the Tutorial here.
Introduction
A long time ago, to include an external PHP library into a PHP development project, you had to:
- Download the PHP library (in zipped format);
- Extract it into a directory inside the project;
- Include it using a
require_once
function;
That was the process. When deploying your project, you have to upload everything including the library files. And when a package gets an update? You download it, extract, overwrite the files, all over again.
That’s why Composer is a savior for PHP developers, or in short, Composer is a PHP dependency manager.
What is Composer?
As the above introduction, Composer is a PHP dependency manager, which greatly helps the PHP development process. Its main functionalities are:
- Install, include, update PHP dependency libraries in a project quickly;
- Declare dependencies in a PHP package/project easily;
Whether you have a project depends on other libraries, and some of the libraries also depend on other libraries, Composer can handle this easily!
In Official Composer Introduction page, Composer can:
- Enables you to declare the libraries you depend on.
- Finds out which versions of which packages can and need to be installed, and installs them (meaning it downloads them into your project).
- You can update all your dependencies in one command.
Therefore, it’s a no-brainer to use Composer in your PHP development.
Composer supports both Windows, Linux and MacOS. This in-depth article will guide you on how to install Composer on Windows operating system.
Tutorial StartsInstall Composer on Windows – Step by step
As any other softwares, Composer has its own system requirements too.
1. System requirements for Composer on Windows
- You have PHP installed -OR- you have PHP package downloaded already with executable file (
php.exe
).
We have an in-depth tutorial on how to download and install PHP on Windows:
Latest Composer requires at least PHP 7.2.5 to run. If you run legacy PHP version (but still at least requires PHP 5.3.2), there are old Composer versions, but it will pose many incompatibilities issues, so we don’t recommend using legacy versions.
2. Download Composer on Windows
Composer makes it very straightforward to download and setup on your Windows system. To download the latest version, head to the official Composer Windows package download URL:
https://getcomposer.org/Composer-Setup.exe
https://getcomposer.org/Composer-Setup.exe
3. Installing Composer
Run the Composer-Setup.exe
file that you just downloaded.
First, you will choose the Install Mode. It’s recommended to install for all users if you have administrator privilege. But if not, you can just install for your account, Composer can still run normally.
Next, if you choose Developer mode, there will be no uninstaller included. It’s up to you on this one.
Choosing php.exe
executable path is required on the next step. This is the path where PHP is installed on your system. If there is one installed, Composer will automatically detect it, else you can manually Browse… and specify the file location.
Specify if you are using any proxy enabled for browsing the Internet.
This Ready to Install screen will have the main configuration displayed for you to check before the installation start. Click Install when everything is good.
The installation will start. You will see these screens when the installation is finished:
Now the installation is done. In summary, this installation will:
- Download the latest
composer.phar
file from Composer server; - Copy it and the configuration files, the uninstaller (if checked) into your user’s
\AppData\Local\ComposerSetup\
folder (in your user mode) orC:\ProgramData\ComposerSetup\
folder (all users mode); - Add Composer into Windows Environment Variables so it can be run from command lines.
We will verify the installation on the next step.
4. Verify Composer installation
To check if Composer has been probably installed, open any Windows terminal tools (cmd.exe, PowerShell, VSCode terminal, etc.) and enter this command:
composer --version
Alternatively, composer -V
(V
in capitalized) is the same command. If this similar message returns, Composer has been successfully installed:
C:\Users\php101dotnet>composer -V
Composer version 2.0.14 2021-05-21 17:03:37
5. Update Composer on Windows
To update Composer on Windows, you can just follow the steps above to download the latest Composer and install it again, which will update Composer to the latest version.
Example Usage
This is a quick example Composer usage to show how easy it is (but also powerful).
Assuming we have an empty PHP project that needs URL slug functionality which turns text into slug URL for SEO purposes (eg. an example project
to an-example-project
). Instead of writing this functionality from scratch, we will use the ausi/slug-generator library written by GitHub user ausi, which is already perfect to do that.
On the (empty) PHP project folder, use the terminal and enter this command:
composer require ausi/slug-generator
This Composer command will:
- Download the
ausi/slug-generator
package which is located on Packagist.org – the main Composer package repository - Install it into our PHP project directory under the
vendor
folder, - Add the package information at
composer.json
file - Record the version, time, hashes into
composer.lock
file to keep track of the packages’ states - Handle all library inclusion into
vendor\autoload.php
file
Graphical views of the package installation after running the command:
That is very handy, compared to downloading the package manually and copying it. So to use the package, we just have to include the autoload.php
file. For example on our project:
<?php
// Include all Composer vendor libraries
require_once('./vendor/autoload.php');
// Include Slug library
use Ausi\SlugGenerator\SlugGenerator;
// Start using it!
$generator = new SlugGenerator;
echo $generator->generate('php101.net Composer installation example');
// Output: php101-net-composer-installation-example
As you see, it is extremely convenience to use Composer for PHP projects.
Final thoughts
This marks the end of our tutorial on how to install Composer on Windows for PHP dependency management. With PHP development, knowing how to use Composer is a must.
We will constantly update this article with new information, as well as releasing other articles with more helpful Composer use cases.
Thanks for reading and hopefully this tutorial is helpful for you.