22

How do i get composer to use a newer version of php i have installed? I have downloaded and am using php 5.4 in all my local servers but when i download composer it points to my mac's default #!/usr/local/bin/env version, which is 5.3.15. I tried editing the composer executable and change the php used but it broke the executable.

I'm trying to use composer to install Laravel and it is downloading the wrong version because of this.

This is what the top of my composer executable looks like but then there's a bunch of weird characters below.

#!/usr/bin/env php
<?php
/*
 * This file is part of Composer.
 *
 * (c) Nils Adermann <naderman@naderman.de>
 *     Jordi Boggiano <j.boggiano@seld.be>
 *
 * For the full copyright and license information, please view
 * the license that is located at the bottom of this file.
 */

Phar::mapPhar('composer.phar');
define('COMPOSER_DEV_WARNING_TIME', 1366931166);
require 'phar://composer.phar/bin/composer';

composer.json for laravel project

{
    "require": {
        "laravel/framework": "4.0.*@dev"
    },
    "autoload": {
        "classmap": [
            "app/commands",
            "app/controllers",
            "app/models",
            "app/database/migrations",
            "app/database/seeds",
            "app/tests/TestCase.php"
        ]
    },
    "scripts": {
        "post-update-cmd": "php artisan optimize"
    },
    "minimum-stability": "dev"
}
David
  • 10,418
  • 17
  • 72
  • 122
  • what is your `php -v` output? – Rob Mar 28 '13 at 02:44
  • well i aliased "php" in my bash_profile to point to my new version so it's telling me it's 5.4.11 – David Mar 28 '13 at 02:46
  • So if you run `php composer.phar laravel` it doesn't use php 5.4.11? – Rob Mar 28 '13 at 02:47
  • i'm not sure where you're telling me to run that... "command 'laravel' is not defined'" – David Mar 28 '13 at 02:50
  • 1
    Can you post your `composer.json`? There's also [`/COMPOSER/config.json`](http://getcomposer.org/doc/03-cli.md#composer-home-config-json). – Jared Farrish Mar 28 '13 at 02:53
  • sorry, i meant `php composer.phar install` – Rob Mar 28 '13 at 02:57
  • Have you tried adding [`{ "require": { "php": ">=5.4.0", ... }}`](http://bundles.laravel.com/bundle/composer) to your project `composer.json`? – Jared Farrish Mar 28 '13 at 02:59
  • hmm i think i may have found the problem. I moved the composer.phar file to my /usr/local/bin/ directory and no matter what i did, it was always using 5.3.15. I deleted the executable from my bin and redownloaded the .phar and moved that my projects root and ran "php composer.phar install" and it looks like that worked – David Mar 28 '13 at 03:03
  • You might still want to get in the habit of adding your package minimum requirements in your `composer.json`. – Jared Farrish Mar 28 '13 at 03:06
  • If you need a one time hack - then create a symlink from `/full/path/to/php-5.4` to, say `/home/david/tmp/php` and run composer like `PATH=/home/david/tmp:$PATH composer` – zerkms Mar 28 '13 at 03:13
  • `#!/usr/bin/env php` - this line specifies the PHP version used. Check `env --help` on the commandline for an introduction, you can also read the manpage with `man env`. – hakre Mar 29 '13 at 11:41

4 Answers4

17

If you don't care for permanent settings, below command worked for me:

/Applications/MAMP/bin/php/php5.6.27/bin/php /usr/local/bin/composer install

Syntax is:

{PATH TO YOUR PHP VERSION} {PATH TO COMPOSER EXECUTABLE} {COMPOSER COMMAND}

To know the executable path: which {EXECUTABLE} can be very helpful. Ex: which composer gave below output which I used in command above:

/usr/local/bin/composer
Diwaker Tripathi
  • 438
  • 3
  • 12
13

i found the problem. I moved the composer.phar file to my /usr/local/bin/ directory and no matter what i did, it was always using 5.3.15. I deleted the executable from my bin and redownloaded the .phar and moved that to my projects root and ran "php composer.phar install" and it looks like that worked.

To clean things up i did the following:

I left composer.phar at the root of my user profile

/Users/davidadams/composer.phar

I then opened my .bash_profile and added the following alias

alias composer='/usr/local/php5/bin/php /Users/davidadams/composer.phar'

That way it's available to me globally and i can define which php version i want to use. Hopefully others will find this useful.

David
  • 10,418
  • 17
  • 72
  • 122
  • 1
    `#!/usr/bin/env php` - this line specifies the PHP version used. Check `env --help` on the commandline for an introduction, you can also read the manpage with `man env`. – hakre Mar 29 '13 at 11:43
  • 2
    You may need to run `source ~/.bash_profile` to reload your aliases before they will work – Steve Tauber Jul 25 '14 at 12:24
5

By default composer on *nix systems uses the PHP binary/executeable specified by the environment. You can see that in the first line which is the shebang:

#!/usr/bin/env php

You can tell the shell to bang against a more concrete PHP version instead, e.g.:

#!/usr/local/php5/bin/php

On Windows systems if you use the composer setup you can specify the PHP binary and the setup then will change the environment to match those needs. But you can as well easily rewrite the batchfile -or- configure the windows operating system to execute .phar files with the specific PHP binary.

hakre
  • 193,403
  • 52
  • 435
  • 836
  • for windows users, upgrade or downgrade download here [link](http://windows.php.net/download/). copy it into your /wamp/bin/php. Then go to control panel->System and security->system->advanced System settings->environment variables, find "path", edit it, find "wamp/bin/php/(change to the version you just finished downloading). restart cmd and try using composer again. Worked for me.. hope it helps someone too – Uche Dim Sep 21 '16 at 00:09
  • Won't this get overwritten by `composer self-update` every time? – Yes Barry May 30 '19 at 20:29
  • 1
    @YesBarry: If you would edit that file, yes. But instead take care `/usr/bin/env php` resolves to the concrete php version you'd like to use. Alternatively invoke the PHP version you want and command it to open composer where the system provides it: `php -f "$(which composer)" -- list`. This circumvents the shell for the interpreter. – hakre Jun 01 '19 at 15:07
0
sudo vim ~/.bash_profile

add

export PATH="/Applications/MAMP/bin/php/php7.3.1/bin:$PATH"

in the above path I have php 7.3.1 so change that to whatever path you have

then hit Esc then :wq

Raul
  • 963
  • 2
  • 11
  • 31