Use different PHP version CLI executable for one command - php

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).

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.

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) ...

Identify where the current generic php command is and to which binary it points to with which 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 following
alias php='/usr/bin/php5.6'
see if this works out

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:
kkarski#debian:~ $ php5 -v
PHP 5.6.26-0+deb8u1 (cli) (built: Sep 21 2016 12:37:50)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies
kkarski#debian:~ $ php -v
PHP 7.0.9-1~dotdeb+8.1 (cli) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
with Zend OPcache v7.0.9-1~dotdeb+8.1, Copyright (c) 1999-2016, by Zend Technologies
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:
root#debian:~# a2dismod php5 && a2enmod php7.0
Module php5 disabled.
To activate the new configuration, you need to run:
service apache2 restart
Considering conflict php5 for php7.0:
Enabling module php7.0.
To activate the new configuration, you need to run:
service apache2 restart
and vice versa if you want to use the lower PHP version.
Mentioning it in case someone has similar problems on Debian.

I find the easiest to achieve the same like just create a softlink like for example
ln -s /opt/php-7.0.32/bin/php /usr/bin/php7
ln -s /opt/php-7.1/bin/php /usr/bin/php71
ln -s /opt/php-5.6/bin/php /usr/bin/php56
then as you use your default version say it is php7.2 as just php for alternative version you can you php7 or php71 or php56
here ln -s /opt/php-7.1/bin/php /usr/bin/php71 is the source/orginal file and /usr/bin/php7 is the destination / link

For anyone else who found no solution in the above, because they use composer update and somehow the wrong PHP version gets used. By using composer self-update I got some more info and eventually found out that in the composer.json you can specify a platform in the config 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"
},

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

I worked on a "script + docker image" to make multiple php versions available whenever I want during development: https://github.com/jclaveau/docker-php-multiversion
You can use it this way:
$ php 5.6 -v
PHP 5.6.40-15+ubuntu18.04.1+deb.sury.org+1 (cli)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies
with Xdebug v2.5.5, Copyright (c) 2002-2017, by Derick Rethans
$ php 7.3 -v
PHP 7.3.13-1+ubuntu18.04.1+deb.sury.org+1 (cli) (built: Dec 18 2019 14:48:49) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.13, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.3.13-1+ubuntu18.04.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies
with Xdebug v2.9.0, Copyright (c) 2002-2019, by Derick Rethans
As it uses Docker.io, you do not need to alter any part of your system configuration.
Hoping it would help you

Related

Laravel 5.4 with MongoDB MAC Mojave

Hello I having trouble to to install "jenssegers/mongodb": "^3.2" on my local environment.
i have this error:
I have added to my /etc/php.ini full path to extentions="/usr/lib/php/extensions/no-debug-non-zts-20160303/mongodb.so"
restart the php and apache
I have try to find mongodb.ini, fail on that.
I try all the tutorial is google... still no luck
Can any one help me please?
PHP 7.1.23 (cli) (built: Feb 22 2019 22:08:13) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies
It seems that you forgot to install MongoDB PHP Driver itself.
To do that just install it with pecl:
sudo pecl install mongodb-1.5.3
1.5.3 is the last stable version, so I'd recommend to use it rather than the most recent 1.6.0alpha.
Then, don't forget to add it to your php.ini (run php --ini to see where it's located):
extension=mongodb.so
To test that it works just run:
php -m | grep mongo
If you see mongodb in output, then it works.

Composer install Error in argument 1, char 2: option not found r

I'm trying to install composer on my Website. The Composer documentations suggests running this command:
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"`
but when I do, I get an error:
Error in argument 1, char 2: option not found r
I use PHP Version 7.0
What's going on here?
I suspect php in your case is referring to PHP's CGI-SAPI binary instead of the CLI that it should be. As documented in the PHP manual, the CGI-SAPI does not include the -r option:
Note:
-r is available in the CLI SAPI, but not in the CGI SAPI.
You can confirm that this is the case by checking "php's" version with the -v flag.
Proper setup should show that php is a CLI interupter:
C:\Users\HPierce>php -v
PHP 7.0.8 (cli) (built: Jun 21 2016 15:27:20) ( ZTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
Improper setup might show that it is the CGI SAPI:
C:\Users\HPierce>php-cgi -v
PHP 7.0.8 (cgi-fcgi) (built: Jun 21 2016 15:27:08)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
You can resolve this by referencing the CLI binary with an absolute path instead of the php shortcut that utilizies your OS's $PATH environment variable:
C:\php\php.exe -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
I had the same problem, and used curl and it worked:
curl -sS https://getcomposer.org/installer | /usr/bin/php7.1-cli
Additionally, to do specific commands to install a package, I needed to specify the php version Composer was installed on, and write "composer.phar" instead of only composer:
php7.1-cli composer.phar require exampleAppDirectory/exampleAppName

Set alias php=/bin/php56 but today it has reverted to it's original path: /bin/php

I'm using CentOS 7's remi and remi.safe repository. Remi installs PHP 5.4 to /bin/php and Remi Safe installs PHP 5.6 to /bin/php56.
When php is executed from the console, I want it to reference php56. Yesterday I set alias php=/etc/php56 which seemed to have resolved the issue. Today, when I execute php -v it outputs:
PHP 5.4.16 (cli) (built: Jun 23 2015 21:17:27)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
Edit:
I added alias php=/bin/php56 to /home/{user}/.bashrc, logged out and logged back in and it did resolve it for that user.
Is it recommended, to set this globally, to do so via /etc/bashrc?
Do you need both version (5.4 and 5.6) ?
If you want a single version, enable "remi-php56" and yum update.
See: http://rpms.remirepo.net/wizard/
Else run "scl enable php56 bash" before other commands to switch to php 5.6.
See: http://blog.remirepo.net/post/2014/08/25/PHP-5.6-en-Software-Collection
Permanent solutions:
in .bashrc => source /opt/remi/php56/enable
ln -s /usr/bin/php56 /usr/bin/php
But again, if you need a single version, seems much more simpler to use "base" packages instead of "SCL" packages, designed for parallel installation of multiple versions.
use this to run the PHP command from the command line
php -c /etc/php5.4/cli/php.ini -f scripts.php

PHP --version shown incorrectly on osX

I've installed in my osX php7 from homebrew, however when I type in the shell eitherphp --version or php-fpm --version I get as result an older version
PHP 5.5.30 (fpm-fcgi) (built: Oct 23 2015 17:22:03)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies
From the shell I looked for every php file or folder sudo find / -name "php" but nothing linked to PHP 5.5
What should I do?
After creating ~/.bash.profile as written in comments by Michael, I found the solution thanks to change the PHP path to MAMPs PHP
In ~/.bash_profile I wrote:
export PATH=/usr/local/Cellar/php70/7.0.2/bin:$PATH
export PATH=/usr/local/Cellar/php70/7.0.2/sbin:$PATH
First row is for php, the second for php-fpm
Then, to be sure that changes take effect type in terminal source ~/.bash_profile

Multiple PHP versions using phpenv, php-build and php-version

I'm on Arch Linux, trying to configure multiple PHP versions for testing my scripts.
I'm basing myself on these three tools:
https://github.com/phpenv/phpenv
https://github.com/CHH/php-build/
http://wilmoore.com/php-version/
First I installed Apache and PHP (and their integration package) with pacman:
# pacman -S php apache php-apache
Next, I followed the instructions for installing the tools on the links, everything is working fine.
I manage to install two versions with phpenv-install from php-build tool. I got something like this:
-- ~
|---.phpenv
|---lib
|---versions
|---5.5.1
|---5.4.17
|--- (some more folders)
On CLI environment everything is working fine, I can easily switch between the two versions with php-version:
$ php-version 5.5.1
$ php --version
PHP 5.5.1 (cli) (built: Aug 5 2013 22:54:47)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2013 Zend Technologies
with Zend OPcache v7.0.2-dev, Copyright (c) 1999-2013, by Zend Technologies
with Xdebug v2.2.3, Copyright (c) 2002-2013, by Derick Rethans
$ php-version 5.4.17
$ php --version
PHP 5.4.17 (cli) (built: Aug 5 2013 23:19:44)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
with Xdebug v2.2.3, Copyright (c) 2002-2013, by Derick Rethans
Now the problem: integrate this with apache.
As I said in the beginning of this post, I also had to install PHP from Arch repositories, which coincidentally is on version 5.4.17. This installation is working fine with Apache.
On php-env readme there are these instructions:
phpenv support dynamic switching for Apache apxs libraries and install
will build and install a libphp5.so shared library for Apache under
the versions libexec folder.
By calling phpenv global to show or change the global PHP version a
link is created under ~/.phpenv/lib/libphp5.so for the appropriate
release build. This link can be used for Apache's LoadModule
php5_module directive and requires Apache to restart when changed.
The problem is that there is no file or symlink named libphp5.so in lib directory neither in the whole .phpenv folder, because
$ find ~/.phpenv -name libphp5.so
returns nothing.
Maybe because english is not my first language, I'm having some problem to intepret the second paragraph of the quote above.
There is a package in the AUR for phpenv
https://aur.archlinux.org/packages/phpenv/
As well as packages for many different versions of PHP
https://aur.archlinux.org/packages/?O=0&C=0&SeB=nd&K=phpenv&outdated=&SB=n&SO=a&PP=50&do_Search=Go
First install phpenv:
yaourt phpenv
Then install the php versions you need, for example:
yaourt php53_29_env
Note: even with this though, you will likely have a problem. Checking the package build, it looks like it is missing the patch to disable PHP automatically being compiled with thread safe features when apache is running a thread safe mpm. Not quite sure since on my systems, I want PHP to enable thread safe code so it will work for me.

Categories