PHP Switch Version on Ubuntu

Sheeraz Gul Feb 17, 2022
PHP Switch Version on Ubuntu

Running multiple versions of PHP might be required for different tasks.

You may need to switch the PHP versions by running two sites on the same server or testing the code of older versions with obsolete methods.

We can switch PHP versions on Ubuntu using the command line with just a few commands.

This tutorial demonstrates the full procedure of switching PHP versions on Ubuntu.

How to Switch PHP Versions on Ubuntu

First of all, let’s check the PHP version installed on our system. Open Ubuntu bash and insert the following command:

$ php -v

The output will show the PHP version used on our system.

Output:

PHP 7.4.3 (cli) (built: Nov 25 2021 23:16:22) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies

The output shows that only PHP 7.4.3 is installed on our system. Install another version of PHP first and then try to switch.

Insert the following command to install PHP 5.6:

#add php repository; this repository might not install earlier or later versions of PHP.
$ sudo add-apt-repository -y ppa:ondrej/php
#update sudo
$ sudo apt update
#install php 5.6
$ sudo apt install php5.6

The output for each command should not retrieve an error. Otherwise, php 5.6 will not be installed.

If you check the PHP version again, the output will still be the same.

To switch the PHP version from 7.4.3 to 5.6, insert the following commands in the bash.

To disable the PHP 7.4.3:

$ sudo a2dismod php7.4

Output:

Module php7.4 already disabled

If the output is not like the above, you may need to install the apache mod and run the above command to disable PHP 7.4. Run this command to install apache mod.

$ sudo apt-get install libapache2-mod-php7.4

Once PHP 7.4 is disabled, run this command to enable PHP 5.6:

$ sudo a2enmod php5.6

Output:

Considering dependency mpm_prefork for php5.6:
Considering conflict mpm_event for mpm_prefork:
Considering conflict mpm_worker for mpm_prefork:
Module mpm_prefork already enabled
Considering conflict php5 for php5.6:
Module php5.6 already enabled

You might need to restart apache between running these commands.

$ sudo service apache2 restart

PHP is switched from 7.4 to 5.6. If you need to set PHP 5.6 as the default version, use the following commands.

#This command will directly set PHP 5.6 as default. Manual Switching
$ sudo update-alternatives --set php /usr/bin/php5.6
#This command will give you the option to select a default PHP version. Interactive Switching
$ sudo update-alternatives --config php

Output:

There are 3 choices for the alternative php (providing /usr/bin/php).

  Selection    Path             Priority   Status
------------------------------------------------------------
  0            /usr/bin/php7.4   74        auto mode
* 1            /usr/bin/php5.6   56        manual mode
  2            /usr/bin/php7.0   70        manual mode
  3            /usr/bin/php7.4   74        manual mode

Press <enter> to keep the current choice[*], or type selection number:

You can select any version by just entering the corresponding number. The select version will be set a default.

You also need to set other PHP extensions as default. For example:

$ sudo update-alternatives --config phar

This command will also give you a list of phar versions, and you can select a default just like above. It would help if you restarted the apache once you were done setting default.

$ sudo service apache2 restart

To switch back to PHP 7.4, you have to run these commands vice versa.

$ sudo a2dismod php5.6
$ sudo a2enmod php7.4
$ sudo service apache2 restart

The output will show PHP 5.6 disabled, and 7.4 enabled. Restart the apache if required.

Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

Related Article - PHP Version