In the previous article in this series, we looked at the Composer dependency manager. In this one, we'll look at installing and configuring Composer.
In many cases installing and configuring these tools is actually more difficult than developing and running the tests.
When the tools are not configured correctly, it's sometimes difficult to figure out why things aren't working. It could be because of an error in a configuration file, or because some file or files are not in the expected location. The error messages you see (if any) won't always be helpful.
I've done my best to break down the installation and configuration process for you, but because of the differences in platforms and your personal choices, it's difficult to cover all the possibilities.
It's difficult to give exact steps for installing Composer, since the process will vary from platform to platform. The executable file is called
composer.phar. You need to think about whether you will install Composer locally in each project or globally (one copy for all projects).
I like to install it globally so I can use it anywhere, but it's up to you. When composer is installed globally (i.e., outside any particular project), you can still have a separate
composer.json file for each specific project. That means you can run it to update or install packages inside that project.
You'll need to either put Composer somewhere in your path, adjust the path to include the directory where you've put Composer, or use the full path to composer when you want to run it.
You'll also need PHP if you don't have it already, which I prefer to get in a package like XAMPP.
Once Composer and PHP are both installed properly and both are in your path, you should be able to type
composer -V and see a version number. Typing
composer should give you a list of commands. If that doesn't work, try typing
php composer -V. The Windows version of the Composer installer provides a batch file called
composer.bat which launches Composer when you just type
composer (assuming that the batch file is in your path). On my Windows machine, the batch file is in the same directory as
composer.phar and looks like this (the second line is a comment):
@echo OFF :: in case DelayedExpansion is on and a path contains ! setlocal DISABLEDELAYEDEXPANSION php "%~dp0composer.phar" %*
You can also try
php composer.phar. If nothing else works, you can try
You could create an alias that lets you just type
composer, but if the
php are both in your path, you shouldn't need to.
It's not a bad idea to increase the
memory_limit in your
php.ini file. Composer can use a lot of memory:
Making sure Composer Works
Before going any further, try these steps. We'll use the Monolog package here because it's small. Create an empty directory somewhere and go there. At the command line type this command:
composer require monolog/monolog
You should see a brief progress report. When it's finished, you should also see a new
vendor directory in the directory where you issued the command as well as a new
composer.json file listing the dependencies. There should also be a
composer.lock file showing the installed versions and configuration options. You can type
composer show to see the list of installed packages and their versions. You'll want to give that command while in the directory containing your
composer.json file or Composer will complain. Notice that
psr/log has also been installed because Monolog lists it as a dependency.
In the next article in this series, we'll learn more about installing packages with Composer.
Looking for high-quality, MODX-friendly hosting? As of May 2016, Bob's Guides is hosted at A2 hosting. (More information in the box below.)