Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
538 views
in Technique[技术] by (71.8m points)

linux - 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 -> /usr/lib64/php5.4/bin/php
  2. /usr/bin/php5.5 -> /usr/lib64/php5.5/bin/php
  3. /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).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Default PHP executable can be found using:

$ which php

In most cases it is link to particular PHP version:

lrwxrwxrwx 1 root root      21 aug 15  2016 /usr/bin/php -> /usr/bin/php7.1

To change it to different version just relink it to another

$ sudo rm /usr/bin/php

$ sudo ln -s /usr/bin/php5.6 /usr/bin/php

Before relink you have to make sure target PHP version is installed.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...