How to Change PHP Version in Ubuntu!

Written by Adam. R on Mar 15th, 2022 Views Report Post

Introduction

Each PHP version contains more features, optimizations and security updates than its previous versions. It is advisable to regularly update your PHP version. But sometimes it may not be possible to upgrade your PHP version directly. For example, if you want to upgrade a major version of PHP like 5.x to 7.x, it may not be possible on some of the systems. In such cases, you may need to separately install the new PHP version and change your default PHP version in Ubuntu. On the other hand, sometimes your existing applications and packages may not work after PHP upgrade. In such cases, you may need to manually change to an older PHP version. In this article, we will learn how to switch PHP versions in Ubuntu.


How to Change PHP Version in Ubuntu

First, open terminal and run the following command to check PHP version:

php -v

You will see following kind of 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 per the above output, we have PHP 7.2 installed on our system. Let us say your applications/packages are not working properly on this version on PHP. In such cases, you don’t need to remove the installed PHP version. You can simply switch to an older version of PHP, say PHP 5.x, keep both the PHPs on your system, and switch between them as required. In case you have already uninstalled your older PHP version, run the following command to re-install PHP 5.6.

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

Now we will look at how to switch between PHP 7.x to 5.x and vice versa.


Switch from PHP 7.x to 5.x

Open terminal and run the following command to disable PHP 7.2.

sudo a2dismod php7.2

You will see the following output.

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

Enable PHP 5.6.

sudo a2enmod php5.6

Run the following command to set PHP 5.6 as default version.

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

The above command will set default PHP only for you. Here is the command to set system wide default PHP.

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

You will see a list of all available PHP versions installed on your system. Select the one you want to set as default version.

If you have installed other packages as well, set them as default with the following command. Here is an example to set default version of phar package.

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

Restart your Apache server to apply changes. Check if PHP version is updated or not.

php -v

Now you will see the following kind of output.

PHP 5.6.37-1+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

Conclusion

You have now learned how to upgrade or downgrade your PHP version on Ubuntu! If you found this usful then please share this and follow me! Also check out my Buy Me A Coffee

Buy Me A Coffee how2ubuntu

Comments (0)