Getting composer to work in a windows environment

in PHP/Tutorials & Samples/Web Development

This tutorial will show you how to get composer working for you if you are using a windows environment. It”s a pretty simple tutorial with only 3 steps required. However, I am assuming that you already have Xampp or some other Apache distribution containing PHP. In my case, I have Xampp installed.

So first of all what is composer? Composer is a tool for dependency management in PHP. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you. Composer is not a package manager in the same sense as Yum or Apt are. Yes, it deals with “packages” or libraries, but it manages them on a per-project basis, installing them in a directory (e.g. vendor) inside your project. By default it does not install anything globally. Thus, it is a dependency manager.

1 – Update your Environment Variables to include the path to PHP

This step must be done so that PHP becomes a recognizable commend in the command prompt. You can verify if PHP is installed by typing “php”. If your path you will get the error: “‘php’ is not recognized as internal or external command, operable program or batch file.”

composer-for-windows-1

If this is the case, you can fix this by editing your System path. Here are the steps that you need to follow:

  1. Go to Control Panel and open the System icon (Start → Control Panel)
  2. Go to the Advanced tab.
  3. Click on the “Environment Variables” button.
  4. Look into the “System Variables” pane.
  5. Find the Path entry (you may need to scroll to find it)
  6. Double click on the Path entry.
  7. Add the path to your PHP executable at the end of the current path string. In my case, the path to my PHP executable is C:\xampp\php

composer-for-windows-2

With this, the path to php.exe is present and Windows can now handle php requests from any path.

2-Install Composer using the Command-line

Now that the path to PHP has been defined, stay in the php binary folder path and run the following commands on your terminal:

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('SHA384', 'composer-setup.php') === 'e115a8dc7871f15d853148a7fbac7da27d6c0030b848d9b3dc09e2a0388afed865e6a3d6b3c0fad45c48e2b5fc1196ae') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"

This installer script will simply check some php.ini settings, warn you if they are set incorrectly, and then download the latest composer.phar in the current directory. The 4 lines above will, in order:

  • Download the installer to the current directory
  • Verify the installer SHA-384
  • Run the installer
  • Remove the installer

The lines are copied directly from download page of composer at https://getcomposer.org/download/

3 – Test the install

Test the everything went well by typing the following command:

php composer.phar

If all is well, you will see the composer interface. Which looks like so:

composer-for-windows-3

You now have composer installed and ready to go. The last thing to do is create a composer.bat file that is used as a windows alias for composer.phar . To do so, simple user the following command:

echo @php "%~dp0composer.phar" %*>composer.bat

Now you can simply type “composer” from the command line, in any folder, and it should work.

composer-for-windows-4

Tags:

Mifty Yusuf is a Montreal-based software developer who enjoys playing with new web technologies as well as comic books and illustrations. He beleives that, no matter what the question is, the answer is always Batman!

Leave a Reply

Your email address will not be published.

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Latest from PHP

Go to Top