How to change PHP version on the command line (CLI) using alternatives

Written by - 0 comments

Published on - Listed in PHP Linux


Thanks to version-named paths of PHP configurations, libraries and binaries, different PHP versions can be installed in parallel on the same server. This is a great help when switching a PHP web application to a new PHP version. By using different PHP-FPM versions running in parallel, the Apache (or Nginx) config can simply be adjusted for the PHP-FPM listener or socket.

While switching the PHP versions of web-applications is a simple one line changer in the web server (vhost) config, there are other scripts on the command line, where the switch isn't so simple.

Looking at the Wordpress command line (wp cli) for example. The wp command is a PHP script, which is executed on the command line:

ck@server:~$ head -n 1 /usr/local/bin/wp
#!/usr/bin/env php

The script uses the php command from the system. wp can tell us which PHP version this is:

ck@server:~$ wp cli info
OS:    Linux 4.19.0-17-amd64 #1 SMP Debian 4.19.194-2 (2021-06-21) x86_64
Shell:    /bin/bash
PHP binary:    /usr/bin/php8.1
PHP version:    8.1.2

php.ini used:    /etc/php/8.1/cli/php.ini
MySQL binary:    /usr/bin/mysql
MySQL version:    mysql  Ver 15.1 Distrib 10.5.12-MariaDB, for debian-linux-gnu (x86_64) using  EditLine wrapper
SQL modes:   
WP-CLI root dir:    phar://wp-cli.phar/vendor/wp-cli/wp-cli
WP-CLI vendor dir:    phar://wp-cli.phar/vendor
WP_CLI phar path:    /home/ckadm
WP-CLI packages dir:   
WP-CLI global config:   
WP-CLI project config:   
WP-CLI version:    2.6.0

But if wp (or any other PHP script) should be used on a web application running on a different PHP version, this might cause problems.

Workaround 1: Launch script with specific PHP cli

Instead of running wp directly (which will lookup the "default" PHP version on the system), launch the script with a specific PHP CLI instead:

ck@server:~$ /usr/bin/php7.4 /usr/local/bin/wp cli info
OS:    Linux 4.19.0-17-amd64 #1 SMP Debian 4.19.194-2 (2021-06-21) x86_64
Shell:    /bin/bash
PHP binary:    /usr/bin/php7.4
PHP version:    7.4.27

php.ini used:    /etc/php/7.4/cli/php.ini
MySQL binary:    /usr/bin/mysql
MySQL version:    mysql  Ver 15.1 Distrib 10.5.12-MariaDB, for debian-linux-gnu (x86_64) using  EditLine wrapper
SQL modes:    
WP-CLI root dir:    phar://wp-cli.phar/vendor/wp-cli/wp-cli
WP-CLI vendor dir:    phar://wp-cli.phar/vendor
WP_CLI phar path:    /home/ckadm
WP-CLI packages dir:    
WP-CLI global config:    
WP-CLI project config:    
WP-CLI version:    2.6.0

On the plus side, this gives you a flexible way to use different PHP versions on PHP scripts. But the chances that you forget the specific PHP version prior to the PHP script are pretty high.

Workaround 2: Using alternatives

Alternatives is a system setting available in most Linux distributions and allow to change the path to a binary or script. This also allows to manually (compile and) install newer software and run the new version alongside the version from the package repositories (see Install/upgrade cmake on Ubuntu using alternatives for an example).

On this system, the current path of /usr/bin/php points to /etc/alternatives/php:

ck@server:~$ ls -la /usr/bin/php
lrwxrwxrwx 1 root root 21 Dec  5  2018 /usr/bin/php -> /etc/alternatives/php

This is typically proof that alternatives are configured. By using the update-alternatives command (on Debian-based distributions), the current configuration can be checked:

ck@server:~$ sudo update-alternatives --get-selections | grep php
php                            auto     /usr/bin/php8.1

The current configuration lets /usr/bin/php point to /usr/bin/php8.1.

To change the PHP version in alternatives, we can install a new alternative with a higher priority (99):

ck@server:~$ sudo update-alternatives --install /usr/bin/php php /usr/bin/php7.4 99 --force
update-alternatives: using /usr/bin/php7.4 to provide /usr/bin/php (php) in auto mode

Verify:

ck@server:~$ sudo update-alternatives --get-selections | grep php
php                            auto     /usr/bin/php7.4

This change is immediately working and can be tested on a PHP script, such as wp:

ck@server:~$ /usr/local/bin/wp cli info
OS:    Linux 4.19.0-17-amd64 #1 SMP Debian 4.19.194-2 (2021-06-21) x86_64
Shell:    /bin/bash
PHP binary:    /usr/bin/php7.4
PHP version:    7.4.27

php.ini used:    /etc/php/7.4/cli/php.ini
MySQL binary:    /usr/bin/mysql
MySQL version:    mysql  Ver 15.1 Distrib 10.5.12-MariaDB, for debian-linux-gnu (x86_64) using  EditLine wrapper
SQL modes:   
WP-CLI root dir:    phar://wp-cli.phar/vendor/wp-cli/wp-cli
WP-CLI vendor dir:    phar://wp-cli.phar/vendor
WP_CLI phar path:    /home/ckadm
WP-CLI packages dir:   
WP-CLI global config:   
WP-CLI project config:   
WP-CLI version:    2.6.0



Add a comment

Show form to leave a comment

Comments (newest first)

No comments yet.

RSS feed

Blog Tags:

  AWS   Android   Ansible   Apache   Apple   Atlassian   BSD   Backup   Bash   Bluecoat   CMS   Chef   Cloud   Coding   Consul   Containers   CouchDB   DB   DNS   Database   Databases   Docker   ELK   Elasticsearch   Filebeat   FreeBSD   Galera   Git   GlusterFS   Grafana   Graphics   HAProxy   HTML   Hacks   Hardware   Icinga   Influx   Internet   Java   KVM   Kibana   Kodi   Kubernetes   LVM   LXC   Linux   Logstash   Mac   Macintosh   Mail   MariaDB   Minio   MongoDB   Monitoring   Multimedia   MySQL   NFS   Nagios   Network   Nginx   OSSEC   OTRS   Office   PGSQL   PHP   Perl   Personal   PostgreSQL   Postgres   PowerDNS   Proxmox   Proxy   Python   Rancher   Rant   Redis   Roundcube   SSL   Samba   Seafile   Security   Shell   SmartOS   Solaris   Surveillance   Systemd   TLS   Tomcat   Ubuntu   Unix   VMWare   VMware   Varnish   Virtualization   Windows   Wireless   Wordpress   Wyse   ZFS   Zoneminder