roomcays roomcays - 9 months ago 57
Linux Question

Use different PHP version CLI executable for one command

So I have Gentoo box with three PHP versions installed (nevermind the reasons):

  1. /usr/bin/php

  2. /usr/bin/php5.5

  3. /usr/bin/php5.6

I want to install Laravel framework using composer:

$ composer create-project laravel/laravel --prefer-dist

This however throws an error because Laravel requires PHP > 5.5.9 and the default
interpreter is 5.4.
So I issue another command:

$ /usr/bin/php5.6 /usr/bin/composer create-project laravel/laravel --prefer-dist

This takes me one step further, but then some post-install commands from Laravel's
comes into play, and installation crashes.

This is due to the fact, that
commands look like this:

"post-install-cmd": [
"php artisan clear-compiled",
"php artisan optimize"

As you can see, the "default" interpreter is used again!

Now, proper PHP files start with following shebang:

#!/usr/bin/env php

This is nice feature as PHP interpreter can be found under different locations on different systems.
Unfortunatelly, in this case
command returns path to the first executable it finds in
environmental variable.

How could I possibly alter current session environment or what kind of trick to perform so for the execution of whole Laravel installation process
command would invoke
instead of

I don't want to change
variable or modify files like
or Laravel's CLI utility

Edit: also assume that I want to do this from regular user account (i.e. with no root permissions).


Maybe you can try to fix the environnement!

$ php -v
PHP 5.4.x (cli) ...
$ set PATH="/usr/lib64/php5.6/bin:$PATH"
$ php -v
PHP 5.6.x (cli) ...

Or, if you don't want to modify the PATH for your shell session, you can scope the change for the current command only:

$ php -v
PHP 5.4.x (cli) ...
$ env PATH="/usr/lib64/php5.6/bin:$PATH" php -v
PHP 5.6.x (cli) ...
$ php -v
PHP 5.4.x (cli) ...