So I have Gentoo box with three PHP versions installed (nevermind the reasons):
/usr/bin/php
->/usr/lib64/php5.4/bin/php
/usr/bin/php5.5
->/usr/lib64/php5.5/bin/php
/usr/bin/php5.6
->/usr/lib64/php5.4/bin/php
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 php
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 composer.json
comes into play, and installation crashes.
This is due to the fact, that composer.json
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 env
command returns path to the first executable it finds in $PATH
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 php
command would invoke /usr/bin/php5.6
instead of /usr/bin/php
?
I don't want to change $PATH
variable or modify files like composer
, composer.json
or Laravel's CLI utility artisan
.
Edit: also assume that I want to do this from regular user account (i.e. with no root permissions).
Since PHP7 came around Debian Linux creates different executables for PHP versions 5 and 7 in /usr/bin by default (if you install both versions that is).
Calling those different versions from the command line is as simple as ever now:
This is obviously only good for simple scripts. For anything larger (composer, artisan etc.) you'll have to change the PATH variable.
To change the version your Apache server is using all you have to do is:
and vice versa if you want to use the lower PHP version.
Mentioning it in case someone has similar problems on Debian.
It's possible to do using
alias
, but keep in mind that aliases are not expanded by default.You must also enable expanding of those.
shopt -s expand_aliases alias php="/usr/local/bin/php-5.6" ./some-script.sh unalias php # back to previous version
Maybe you can try to fix the environnement!
Or, if you don't want to modify the PATH for your shell session, you can scope the change for the current command only:
For anyone else who found no solution in the above, because they use
composer update
and somehow the wrong PHP version gets used. By usingcomposer self-update
I got some more info and eventually found out that in thecomposer.json
you can specify aplatform
in theconfig
section, which overrides what php version is used by composer. Simply changing this value or removing this config solved my issue.composer.json
"config": { "platform": { "php": "7.1" },
Default PHP executable can be found using:
In most cases it is link to particular PHP version:
To change it to different version just relink it to another
Before relink you have to make sure target PHP version is installed.
Identify where the current generic
php
command is and to which binary it points to withwhich php
.It will give you a path to a symlink like you mention in your question
/usr/bin/php -> /usr/lib64/php5.4/bin/php
Edit the symlink to point to which ever php version you want for now, see here https://unix.stackexchange.com/questions/88824/how-can-i-edit-symlinks
When you are done just reverse the process.
UPDATE: you can also add an
alias
for the current user by editing~/.bashrc
and adding the followingalias php='/usr/bin/php5.6'
see if this works out