30

Composer doesn't run correctly in Cygwin if you try to install it "globally".

Putting composer.phar into /usr/local/bin/composer, then trying to run it will result in the error:

Could not open input file: /usr/local/bin/composer
Populus
  • 7,470
  • 3
  • 38
  • 54

13 Answers13

65

Just tripped over the same problem and found a solution. Posting it here, just in case I'll ever have to look it up again.

  1. Set up a bin directory right under /home/my-username:

    cd ~
    mkdir bin
    
  2. Move the composer.phar (or any other of those nifty new PHP imps that are on the rise) into the ~/bindirectory and make sure to set it's execution bit:

    # Notice how I got rid of the superfluous `.phar` extension
    mv /path/to/composer.phar ~/bin/composer
    chmod +x ~/bin/composer
    
  3. Tell cygwin to include your ~/bin directory in the search path:

    Open up the file ~/.bash_profile and uncomment the following paragraph ...

    # Set PATH so it includes user's private bin if it exists
    if [ -d "${HOME}/bin" ] ; then
      PATH="${HOME}/bin:${PATH}"
    fi
    
  4. Now, for the most important part:

    A wrapper script that helps Win's native PHP resolve Unix style paths (which is causing the problem after all as Windows doesn't know how to handle /cygdrive/... paths).

    cd ~/bin
    touch php
    chmod +x php
    

    After editing the wrapper script ~/bin/php should read:

    #!/bin/bash
    
    # e.g. php="/cygdrive/c/Program Files (x86)/php/php.exe"
    php="/path/to/php.exe"
    
    for ((n=1; n <= $#; n++)); do
        if [ -e "${!n}" ]; then
            # Converts Unix style paths to Windows equivalents
            path="$(cygpath --mixed ${!n} | xargs)"
    
            case 1 in
                $(( n == 1 )) )
                    set -- "$path" "${@:$(($n+1))}";;
                $(( n < $# )) )
                    set -- "${@:1:$((n-1))}" "$path" ${@:$((n+1)):$#};;
                *)
                    set -- "${@:1:$(($#-1))}" "$path";;
            esac
        fi
    done
    
    "$php" "$@"
    
  5. Now restart your shell and it should correctly invoke the PHP interpreter whenever it stumbles upon a #!/usr/bin/env php shebang. Simply issue a:

    composer --help
    
aefxx
  • 24,835
  • 6
  • 45
  • 55
  • Wow, I cannot thank you enough. This was exactly what I needed all in one place. – Douglas.Sesar May 23 '14 at 08:11
  • 1
    Seriously. Can you look me up online [just google php experts], send me your Dwolla / Paypal address, because I *really* want to tip you for the wonderful PHP cygwin wrapper script. – Theodore R. Smith Jul 25 '14 at 12:38
  • 2
    @TheodoreR.Smith Very kind of you but please refrain from sending any money. The simple fact that it is of use to you, is credit enough. – aefxx Jul 25 '14 at 13:58
  • 3
    I fixed it specifically for composer without the need of the wrapper script (which is a more generic solution), using a simple `alias composer='php "$(cygpath -w ~/bin/composer.phar)"' (with php on the PATH of course) – dregad Sep 18 '14 at 16:17
  • Brilliant idea to write a wrapper script for windows' php for cygwin. Thank you. :) – Johnny Wong May 20 '15 at 09:57
  • Accepting answer due to comments & up votes. I don't use windows to develop anymore so I cannot personally verify... – Populus Jul 29 '15 at 20:18
  • 2
    just a quick comment, you dont need to close down the shell just do `. ~/.bashrc` or `source ~/.bashrc` to reload the file, 2nd note, also works brilliantly on MobaXterm (which runs gygwin in its local sessions) – DarkMukke Oct 03 '15 at 20:00
  • Still works. Cannot upvote enough, thank you. Many other answers are specific to Composer, as that is probably the most common .phar out there these days, but there are still lots of others and this solution works great. – zombat Jul 11 '17 at 21:10
  • @zombat Thanks. Glad it's still a viable option for our fellow Cygwin users. Btw. have you checked out Microsofts Subsystem for Linux? If it works as advertised, it should render Cygwin obsolete in the long run. – aefxx Jul 11 '17 at 23:06
  • Thanks a lot for your script. It was invaluable in an answer to my [own question here](https://wordpress.stackexchange.com/questions/340648/could-not-open-input-file-error-even-when-wp-cli-is-in-the-path). I'd be grateful if you could point out anything that might be inaccurate in it. – Hashim Aziz Jun 17 '19 at 21:49
14

How about this one?

  1. In ~/.bashrc, add: alias composer='php c:\\your\\path\\to\\composer.phar'
  2. Restart cygwin or reload the bashrc by running source ~/.bashrc

Works for me using both Cygwin's native php.exe and XAMPP's Windows-specific one.

rkok
  • 1,047
  • 14
  • 17
7

I think what might work is to build a proxy instead:

  • Put composer.phar in /usr/local/bin/composer.phar
  • Create a bash proxy as /usr/local/bin/composer with the following:

    #!/bin/sh
    c:/path/to/php c:/path/to/composer.phar $@
    
  • chmod +x /usr/local/bin/composer

Seldaek
  • 40,986
  • 9
  • 97
  • 77
5

Try this:

  • Install Cygwin PHP and use it to run composer in Cygwin.

You can choose one of two ways to work fine with PHP+Composer in Cygwin:

  1. Install Cygwin PHP and install composer.phar in Cygwin.

  2. Install Windows PHP and install Winodows Composer, then call then from Cygwin. (Windows XAMMP+Composer with alias in cmd)


Your problem is caused by that Cygwin Composer is ruining by Windows PHP on Cygwin, so it can not recognize the file path.

After install Cygwin PHP, the composer will integrates with Cygwin PHP then fit to Cygwin's filepath.

Cygwin PHP extensions for composer usage:

php
php-json
php-mbstring
php-phar 
php-zip
php-posix

You can install other PHP extension when composer's package is needed such as php-xmlwriter, php-tokenizer, php-ctype.


Installation commands guide:

If you have already installed apt-cyg, there are installation command above:

apt-cyg install php php-json php-mbstring php-phar php-zip php-posix
apt-cyg install php-xmlwriter php-tokenizer php-ctype

Then install Composer via Cygwin PHP:

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php composer-setup.php
php -r "unlink('composer-setup.php');"
chmod +x ./composer.phar
mv ./composer.phar /usr/bin/composer
composer -v

Conclusion

I recommend using Windows PHP + Windows Composer, and call then in Cygwin, it because the Cygwin PHP is not much stable than Windows PHP.

Nick Tsai
  • 3,799
  • 33
  • 36
4

I had the same problem. After reading the final instruction from composer installer, it says that the $PATH variable should be reloaded. I rebooted my computer and then linked the Composer bin to /usr/local/bin.

First locate the composer bin : /cygdrive/c/ProgramData/ComposerSetup/bin/composer

Then according to Seldaek answer, after trying a non-working symlink, I did :

nano /usr/local/bin/composer

#!/bin/sh
/cygdrive/c/ProgramData/ComposerSetup/bin/composer $@

chmod +x /usr/local/bin/composer

And it's working.

Community
  • 1
  • 1
soyuka
  • 8,839
  • 3
  • 39
  • 54
3

What I did (very simple):

  • Install Composer normally with the Windows installer
  • Open a Cygwin console an type composer you will see something like Could not open input file: /c/route/to/your/composer/installation/bin
  • Create an alias composer='composer.bat' on your bash profile or bashrc or whatever
  • Done

I'm running Windows 8.1

nikoskip
  • 1,860
  • 1
  • 21
  • 38
2

I was having this issue. Most of the responses were over my head but I fixed it with no problems.

Step 1) Reinstall Composer globally as per https://getcomposer.org/download/

Step 2) Leave it alone. All the special stuff you want to do, don't do that.

Step 3) Download the latest setup-xxx.exe from https://cygwin.com/

Step 4) Click it with your mouse.

NOTE: During step 5 you will feel a sudden urge to read instructions, browse the web for info and start checking little boxes on the menu. Don't do that.

Step 5) Click 'next' until it stops asking. It will look at your existing settings, fix things up and add any missing dependencies.

This worked for me.

Gregor Macgregor
  • 495
  • 6
  • 12
0

Getting composer to work globally inside Cygwin is a pain in the butt...

The solutions I have come up with:

  1. Don't use it globally. If you are using the PHP CLI from a Windows installation, it won't recognize the Linux paths that Cygwin uses.

    What I have done is put it in the base directory of all the projects I use composer with, and do ../composer.phar to run it.

    It works fine this way, and it's almost globally available...

  2. Download, and compile your own PHP binaries within Cygwin... Yea, kind of a overkill.

Populus
  • 7,470
  • 3
  • 38
  • 54
0

The easiest way is to install composer using the Windows installer from their website and then copy the two files 'composer' and 'composer.phar' from "C:\ProgramData\Composer" into a directory which is in the PATH variable. E.g. you could copy the files into the /bin/ directory of cygwin. Afterwards you can again uninstall the "Windows version" of composer.

Update! This is what I did:

  1. Install PHP and needed modules from the Cygwin Ports project
  2. Download the latest composer snapshot
  3. Rename 'composer.phar' to 'composer' and save it to /usr/local/bin
  4. Open /bin/dash.exe and run '/usr/bin/rebaseall'
  5. Also if 4. gives you an error, composer should run now
thasmo
  • 9,159
  • 6
  • 27
  • 32
0

I solved the problem like this in a Cygwin/XAMPP setup:

  • Install composer.phar to XAMPP's php directory

  • Create an executable Bash script named composer in XAMPP's php directory:

    #!/bin/bash
    script_dir=$(cygpath -w $(dirname $0))
    php "$script_dir/composer.phar" $@
    

    It's important to use cygpath -w to convert the path to a path in Windows form.

  • Make sure XAMPP's php directory is accessible in Cygwin's $PATH:

    $ export PATH=$PATH:/cygdrive/i/dev/server/xampp/php
    


Now it's possible to call composer from anywhere you like without problems:

    $ composer -V
    Composer version 264f433ca3f007d39568b3722b4bf418f58ba15b
Oliver
  • 490
  • 4
  • 10
0

I fixed it by adding a /usr/local/bin/composer file:

nano /usr/local/bin/composer

with the following content:

#!/bin/bash
/cygdrive/c/wamp/bin/php/php5.4.3/php "C:\wamp\bin\php\php5.4.3\composer"

Basically, you have to call PHP with a Windows style path, not a cygwin path.

Matthieu Napoli
  • 48,448
  • 45
  • 173
  • 261
  • @Populus Haha me neither, I guess I was having the problem with both and I posted the solution for PHPUnit. I fixed my answer thanks ;) – Matthieu Napoli May 18 '13 at 17:26
0

I suggest you use babun, it is based cygwin, but you can install package by pact, you can do this:

pact install php php-json php-phar

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

then enjoy yourself.

avatas
  • 155
  • 2
  • 7
0

I was having trouble getting Composer to work in Cygwin and none of the solutions above were resolving my problem. Eventually I stumbled across this comment in the Composer github bugs discussion:

Yeah TBH using php from cygwin isn't a great idea, cygwin is just too different an environment. Too many hacks that create failures.. In any case closing here as there isn't much we can do I'm afraid :)

I don't know precisely where composer/Cygwin/php was tripping up, but, broadly, my problem was that I had two conflicting installations of PHP in environmental variables, one from an installation of WAMP, and another installed with Cygwin. The Cygwin PHP installation seemed to be struggling with some sort of path issue. I removed it, using only the WAMP PHP, and composer ran in Cygwin just fine.

ISOAnswers
  • 11
  • 3