Skip to content

Managing Multiple Versions

Laurence 'GreenReaper' Parry edited this page Dec 19, 2021 · 5 revisions

The ppa:ondrej/php personal package archive allows the installation of multiple PHP versions simultaneously; this currently includes PHP 5.6, 7.0, 7.1, 7.2, 7.3, 7.4, 8.0 and 8.1. With so many different versions of PHP, how do you call the one you want on a per-script basis, and how do you set one of them as your default? Let's find out!

Installation

Running the following will install the CLI SAPI of all available PHP versions:

sudo apt install php5.6-cli php7.0-cli php7.1-cli php7.2-cli php7.3-cli php7.4-cli php8.0-cli php8.1-cli

Calling Specific Versions

Whenever a version of PHP is installed, its executable is available at /usr/bin/php[version]. For example, to explicitly call version 7.3 of PHP, you could call /usr/bin/php7.3, or more simply just php7.3.

Setting Global Defaults

Depending on which versions of PHP you have installed, /usr/bin/php will point to a different default version, but in this example, let's assume you have all versions of PHP up to 7.1 installed: in this case version 7.1 will be the default. What this means is that the /usr/bin/php file is really a symbolic link to /usr/bin/php7.1.

The question then becomes, how does one change this default? It is changed by using the update-alternatives command. First, let's list all available versions:

update-alternatives --query php

Which will show output similar to the following:

Name: php
Link: /usr/bin/php
Slaves:
 php.1.gz /usr/share/man/man1/php.1.gz
Status: auto
Best: /usr/bin/php7.1
Value: /usr/bin/php7.1

Alternative: /usr/bin/php5.6
Priority: 56
Slaves:
 php.1.gz /usr/share/man/man1/php5.6.1.gz

Alternative: /usr/bin/php7.0
Priority: 70
Slaves:
 php.1.gz /usr/share/man/man1/php7.0.1.gz

Alternative: /usr/bin/php7.1
Priority: 71
Slaves:
 php.1.gz /usr/share/man/man1/php7.1.1.gz

As you can see, the link /usr/bin/php currently points to /usr/bin/php7.1, but lower priority alternatives are available, including /usr/bin/php5.6 and /usr/bin/php7.0. To change you default, simple run the following:

sudo update-alternatives --set php /usr/bin/php7.0

Now you can see the output of update-alternatives --query php shows that /usr/bin/php is pointing to version 7.0 instead of 7.1:

Name: php
Link: /usr/bin/php
Slaves:
 php.1.gz /usr/share/man/man1/php.1.gz
Status: manual
Best: /usr/bin/php7.1
Value: /usr/bin/php7.0

Alternative: /usr/bin/php5.6
Priority: 56
Slaves:
 php.1.gz /usr/share/man/man1/php5.6.1.gz

Alternative: /usr/bin/php7.0
Priority: 70
Slaves:
 php.1.gz /usr/share/man/man1/php7.0.1.gz

Alternative: /usr/bin/php7.1
Priority: 71
Slaves:
 php.1.gz /usr/share/man/man1/php7.1.1.gz

It's important to note that there are four important PHP commands you should update if changing the default versions. These include php, php-config, phpdbg and phpize. So, to fully migrate to PHP 7.0, execute the following:

sudo update-alternatives --set php /usr/bin/php7.0
sudo update-alternatives --set php-config /usr/bin/php-config7.0
sudo update-alternatives --set phpdbg /usr/bin/phpdbg7.0
sudo update-alternatives --set phpize /usr/bin/phpize7.0