61

I've read the global installation documentation for Composer, but it's for *nix systems only:

curl -s https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer

I would be such happy doing the same on Windows, that's the OS of my development machine. I would be able to run

composer update

From an arbitrary folder where composer.json exists. Interpreter php.exe is already in PATH variable.

Any clue?

gremo
  • 47,186
  • 75
  • 257
  • 421

14 Answers14

88

Sure. Just put composer.phar somewhere like C:\php\composer.phar, then make a batch file somewhere within the PATH called composer.bat which does the following:

@ECHO OFF
php "%~dp0composer.phar" %*

The "%*" repeats all of the arguments passed to the shell script.

Then you can run around doing composer update all ya want!

hakre
  • 193,403
  • 52
  • 435
  • 836
Lusitanian
  • 11,012
  • 1
  • 41
  • 38
29

Install Composer

On Windows, you can use the Composer Windows Installer.

giannis christofakis
  • 8,201
  • 4
  • 54
  • 65
  • 1
    I use this installer but some how there is no composer in PATH – Milad Nov 21 '15 at 15:33
  • @Milad, I also experienced the same. What windows version were you using? Mine was windows 8. I had to add the path to composer manually too. – Osh Mansor Feb 25 '16 at 01:54
  • @OshMansor, I used windows 8.1 back then but i switch to windows 7 everything goes well i have lot of problem with windows8, Android studio, some of vs 2013.... i use Lusitanian answer and it work for me. – Milad Mar 04 '16 at 19:49
  • Any idea where the default composer.json is created? – Hunter Nelson May 20 '19 at 18:40
24

Go to php.exe located folder.

C:\wamp\bin\php\php5.5.12\

open cmd there, and execute below command.

php -r "readfile('https://getcomposer.org/installer');" | php

composer.phar will be downloaded in same folder.

Create folder named composer in C:// drive (or anywhere you wish, for upcoming steps, remember the path).

move composer.phar file to C://composer folder.

Create composer.bat file in same folder with contents below

@ECHO OFF
php "%~dp0composer.phar" %*

create file named composer without any extensions.

running command type NUL > composer in CMD will help to get it done quickly,

Open that file and place below contents inside it.

#!/bin/sh
dir=$(d=$(dirname "$0"); cd "$d" && pwd)
# see if we are running in cygwin by checking for cygpath program
if command -v 'cygpath' >/dev/null 2>&1; then
    # cygwin paths start with /cygdrive/ which will break windows PHP,
    # so we need to translate the dir path to windows format. However
    # we could be using cygwin PHP which does not require this, so we
    # test if the path to PHP starts with /cygdrive/ rather than /usr/bin.
    if [[ $(which php) == /cygdrive/* ]]; then
        dir=$(cygpath -m $dir);
    fi
fi
dir=$(echo $dir | sed 's/ /\ /g')
php "${dir}/composer.phar" $*

Save.

Now set path, So we can access composer from cmd.

  • Show Desktop.

  • Right Click My Computer shortcut in the desktop.

  • Click Properties.

  • You should see a section of control Panel - Control Panel\System and Security\System.

  • Click Advanced System Settings on the Left menu.

  • Click Environment Variables towards the bottom of the window.

  • Select PATH in the user variables list.

  • Append your PHP Path (C:\composer) to your PATH variable, separated from the already existing string by a semi colon.

  • Click OK

Restart your machine.

Or, restart explorer only using below command in CMD.

taskkill /f /IM explorer.exe
start explorer.exe
exit

Original Article with screenshots here : http://aslamise.blogspot.com/2015/07/installing-composer-manually-in-windows-7-using-cmd.html

Muhammed Aslam C
  • 847
  • 1
  • 8
  • 14
  • 1
    Thanks for solving both Windows Command Prompt and Cygwin. I took your scripts ([composer.bat](https://gist.github.com/mosbth/bba3e71b5f86394a0d44), [composer](https://gist.github.com/mosbth/ae5437cfe01d14b9707c)) and made them more generic to work with any phar, such as phpunit, phpcs, phpmd and so on. One script to rule them all, so to speak. – Mikael Roos Mar 14 '16 at 16:37
  • This is 2017, two years after. And this just saved me hours of searching online. Thank you. – NaijaProgrammer Oct 13 '17 at 08:52
12

This may be useful to someone:

On Windows 7, if you've installed Composer using curl, it can be found in similar path:

C:\Users\<username>\AppData\Roaming\Composer

Anibe Agamah
  • 221
  • 3
  • 6
6

Well, now this question is a bit obsolete as there is now an official installer which "will install the latest Composer version and set up your PATH so that you can just call composer from any directory in your command line."

You can get it at : http://getcomposer.org/doc/00-intro.md#installation-windows

LucasBeef
  • 118
  • 2
  • 9
  • it ask for php.exe but i can't find any – Mr X Jul 17 '14 at 07:41
  • @atjoshi: you need to install php on windows. [Xampp](https://www.apachefriends.org/) is pretty good and relatively easy to setup. – User Oct 05 '14 at 11:00
3

A bit more generic if you put the batch in the same folder as composer.phar:

@ECHO OFF
SET SUBDIR=%~dp0
php %SUBDIR%/composer.phar %*

I'd write it as a comment, but code isn't avail there

Johannes Reiners
  • 570
  • 5
  • 11
3

Start > Computer : Properties > Change Settings > Advanced > Environment Variables > PATH : Edit [add this string (without "") at the end of line ";C:\<path to php folder>\php5.5.3"].. open cmd and type composer thats it :-)

Muflix
  • 6,192
  • 17
  • 77
  • 153
3

I use Composer-Setup.exe and it works fine. Just in case you need to know where is the composer.phar (to use with PhpStorm) :

C:\ProgramData\ComposerSetup\bin\composer.phar
Climberdav
  • 61
  • 7
3

Unfortunately, all the good answers here didn't work for me. So after installing composer on windows 10, I just had to set system variable in environment variables and it worked.

Windows 10 environment variable -> system variables

Overcomer
  • 434
  • 5
  • 11
1

sorry to dig this up, I just want to share my idea, the easy way for me is to rename composer.phar to composer.bat and put it into my PATH.

xgenvn
  • 193
  • 1
  • 1
  • 7
1

An alternative variant (see Lusitanian answer) is to register .phar files as executable on your system, exemplary phar.reg file:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\.phar]
@="phar_auto_file"

[HKEY_CLASSES_ROOT\phar_auto_file\shell\open\command]
@="\"c:\\PROGRA~1\\php\\php.exe\" \"%1\" %*"

Just replace the path to php.exe to your PHP executable. You can then also extend the %PATHEXT% commandline variable with .PHAR which will allow you to type composer instead of composer.phar as long as composer.phar is inside the %Path%.

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836
1

I was having the same issue and when I checked the environment in Windows 7 it was pointing to c:\users\myname\appdata\composer\version\bin which didn't exists. the file was actually located in C:\ProgramData\ComposerSetup\bin Fixed the location in environment setting and it worked

Jose Ortiz
  • 705
  • 1
  • 9
  • 19
1

you can install it using this command line

 echo @php "%~dp0composer.phar" %* > composer.bat
Sakhri Houssem
  • 975
  • 2
  • 16
  • 32
0

I found that on Control Panel > Environment Variables > Variables for my localuser just inside PATH varible like this:

C:\Users\MY_USER_NAME\AppData\Roaming\Composer\vendor\bin
Rubén Ruíz
  • 453
  • 4
  • 9