Change php version xampp ubuntu

This brief tutorial walks you through the steps to switch between multiple PHP versions in Ubuntu Linux and its derivatives. This guide is officially tested on Ubuntu 18.04 LTS server edition. However, It may work on newer as well as older Ubuntu versions.

Contents

Introduction

Sometimes, the most recent version of an installed package might not work as you expected. Your application may not be compatible with the updated package and it support only a specific old version of package.

In such cases, you can simply downgrade the problematic package to its earlier working version in no time. Refer our old guides on how to downgrade a package in Ubuntu and its variants here and how to downgrade a package in Arch Linux and its derivatives here.

In some situations, you need not to downgrade the problematic packages. In Linux, we can use multiple versions of the same package at the same time, side by side.

For instance, let us say you are testing a PHP application in LAMP stack deployed in Ubuntu 18.04 LTS. After a while you find out that the application worked fine in PHP 5.6, but not in PHP 7.2 (Ubuntu 18.04 LTS installs PHP 7.x by default).

Are you going to reinstall PHP or the whole LAMP stack again? Not necessary.

You don't even have to downgrade the PHP to its earlier version. You can simply switch to the previous working PHP version in couple minutes.

1. Switch Between Multiple PHP Versions

Let us check the default installed version of PHP using command:

$ php -v

Sample output:

PHP 7.2.7-0ubuntu0.18.04.2 (cli) (built: Jul 4 2018 16:55:24) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.2.7-0ubuntu0.18.04.2, Copyright (c) 1999-2018, by Zend Technologies

As you can see, the installed version of PHP is 7.2.7.

After testing your application for couple days, you find out that your application doesn't support PHP 7.2. In that case, it is a good idea to have both PHP 5.x version and PHP 7.x version, so that you can easily switch between any supported version at any time.

You don't need to remove PHP 7.x or reinstall LAMP stack. You can use both PHP 5.x and 7.x versions together.

I assume you didn't uninstall php 5.6 in your system yet. Just in case, you removed it already, you can install it again using a PPA like below.

You can install PHP 5.6 from a PPA:

$ sudo add-apt-repository -y ppa:ondrej/php
$ sudo apt update
$ sudo apt install php5.6

1.1. Switch from PHP 7.x to PHP 5.x

1.1.1 First disable PHP 7.2 module using command:

$ sudo a2dismod php7.2

Sample output:

Module php7.2 disabled.
To activate the new configuration, you need to run:
systemctl restart apache2

1.1.2. Next, enable PHP 5.6 module:

$ sudo a2enmod php5.6

1.1.3 Set PHP 5.6 as default version using command:

$ sudo update-alternatives --set php /usr/bin/php5.6

1.1.4. Alternatively, you can run the following command to set which system wide version of PHP you want to use by default.

$ sudo update-alternatives --config php

1.1.5. Enter the selection number to set it as default version or simply press ENTER to keep the current choice.

Change php version xampp ubuntu
Change PHP default version

1.1.6. If you have installed other PHP extensions, set them as default as well.

$ sudo update-alternatives --set phar /usr/bin/phar5.6

1.1.7. Finally, restart your Apache web server:

$ sudo systemctl restart apache2

Now, check if PHP 5.6 is the default version or not:

$ php -v

Sample output:

PHP 7.2.7-0ubuntu0.18.04.2 (cli) (built: Jul 4 2018 16:55:24) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.2.7-0ubuntu0.18.04.2, Copyright (c) 1999-2018, by Zend Technologies
1

2. Switch from PHP 5.x to PHP 7.x

Likewise, you can switch from PHP 5.x to PHP 7.x version by running the following commands one after another.

PHP 7.2.7-0ubuntu0.18.04.2 (cli) (built: Jul 4 2018 16:55:24) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.2.7-0ubuntu0.18.04.2, Copyright (c) 1999-2018, by Zend Technologies
2
PHP 7.2.7-0ubuntu0.18.04.2 (cli) (built: Jul 4 2018 16:55:24) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.2.7-0ubuntu0.18.04.2, Copyright (c) 1999-2018, by Zend Technologies
3
PHP 7.2.7-0ubuntu0.18.04.2 (cli) (built: Jul 4 2018 16:55:24) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.2.7-0ubuntu0.18.04.2, Copyright (c) 1999-2018, by Zend Technologies
4
$ sudo systemctl restart apache2

A word of caution:

The final stable PHP 5.6 version has reached the end of active support as of 19 Jan 2017. However, PHP 5.6 will continue to receive support for critical security issues until 31 Dec 2018. So, It is recommended to upgrade all your PHP applications to be compatible with PHP 7 .x as soon as possible.

If you want prevent PHP to be automatically upgraded in future, refer the following guide.

  • How to prevent a package from being automatically installed, upgraded or removed in Ubuntu

And, that's all for now. This is how we install multiple PHP versions in Ubuntu. Using Update-alternatives command, we can quickly change from one PHP version to another in Ubuntu and set default PHP version.

LAMP StackLinuxLinux tipsPHPPHP TipsSwitch between multiple PHP versionsSwitch PHP to other versionUbuntu

Can I change PHP version in XAMPP?

Download PHP from the latest Xampp: Extract the folder and copy php folder from the latest xampp. Now, go to your existing xampp folder and rename the current PHP folder to php7 or whatever you want. Then paste the new php folder in the existing xampp. That's it!

How to change PHP version in Ubuntu?

Install PHP (5.6, 7..
First start by adding Ondřej Surý PPA to install different versions of PHP – PHP 5.6, PHP 7. x, and PHP 8.0 on the Ubuntu system. ... .
Next, update the system as follows. $ sudo apt-get update..
Now install different supported versions of PHP as follows..

How to downgrade PHP version in XAMPP Ubuntu?

In short what you need to do is:.
Ensure the php packages for the version you want are installed..
Update any configuration for that PHP version..
Update your web server configuration to point to the correct PHP version..
Point PHP CLI to the correct PHP version..
Restart your web server and php-fpm..
Mask the old php-fpm daemon..

How to downgrade PHP version 7.4 to 7.2 in XAMPP?

To switch major XAMPP PHP version, some Apache configuration of XAMPP has to be adjusted..
Edit LoadFile on php DLL file. Find this section: # # PHP-Module setup # ... .
Edit LoadModule on Apache DLL file. ... .
Edit IfModule..