Good content takes time and effort to come up with.

Please consider supporting us by just disabling your AD BLOCKER and reloading this page again.







How To Switch Different PHP Versions In Ubuntu / Linux


Share On     Share On WhatsApp     Share On LinkedIn


You will have lot of problems when your local development PHP version might be different then that of production PHP version, this might further cause some issues when you run composer commands.


Not only this if you need to upgrade your PHP frameworks like LARAVEL, SYMFONY you have to install newer version of PHP. You might also upgrade because fo security patches. So in this tutorial I will teach you how to install multiple PHP versions and how to switch to different PHP version as per your requirements.


Step 1 - Update OS Packages & Install Libraries


First login to your server and update the Ubuntu/Linux packages with the following command


sudo apt-get update


You can check if you had earlier installed PHP by using the following command. It will list the PHP version installed


sudo php -v


You might need to install additional packages like PPA, if your operating system has not yet released the PHP version your looking out for then 3rd party PPA Ondrej will help to download the latest one.


sudo apt-get install python-software-properties
sudo add-apt-repository ppa:ondrej/php


Once you install the above packages again you have to update your ubuntu server packages so that it will download the latest software packages from these repositories


sudo apt-get update

Step 2 - Install Different PHP Versions


Now install the different versions of PHP so that you can see how easy it is to switch between them


For APACHE web server


sudo apt-get install php5.6
sudo apt-get install php7.1
sudo apt-get install php7.2


Additionally if you need you can install other packages along with them like the following


sudo apt-get install php5.6 php5.6-cli php5.6-curl php5.6-mysql php5.6-mbstring
sudo apt-get install php7.1 php7.1-cli php7.1-curl php7.1-mysql php7.1-mbstring
sudo apt-get install php7.2 php7.2-cli php7.2-curl php7.2-mysql php7.2-mbstring


For NGINX web server


sudo apt-get install php5.6-fpm
sudo apt-get install php7.1-fpm
sudo apt-get install php7.2-fpm


Additionally if you need you can install other packages along with them like the following


sudo apt-get install php5.6-fpm php5.6-cli php5.6-curl php5.6-mysql php5.6-mbstring
sudo apt-get install php7.1-fpm php7.1-cli php7.1-curl php7.1-mysql php7.1-mbstring
sudo apt-get install php7.2-fpm php7.2-cli php7.2-curl php7.2-mysql php7.2-mbstring


You can see list of all the php version releases in official website PHP Releases


Step 3 - Switching Between PHP Versions


By default any one of the PHP version will be activated after installing all the packages. At any point of time you can check which PHP version is currently active with the following command


sudo php -v


You may get the output like the following


PHP 7.2.24-0ubuntu0.18.04.4 (cli) (built: Apr 8 2020 15:45:57) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
  with Zend OPcache v7.2.24-0ubuntu0.18.04.4, Copyright (c) 1999-2018, by Zend Technologies


SET PHP 7.1 As Default Version


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


SET PHP 5.6 As Default Version


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


Manual Switching From 7.2 to 5.6


First disable the PHP 7.2 version with a2dismod, then enable PHP 5.6 version with a2enmod then restart APACHE server


sudo a2dismod 7.2
sudo a2enmod 5.6
sudo service apache2 restart


To get back PHP 7.2 as default version just follow the above steps by changing the version you want to enable and disable.


Conclusion


Now you got to know how to switch between the PHP versions. You might be interested to learn few more articles that I have written

How To Do Basic Server Security Setup For Ubuntu / Linux,

How To Install Linux, NGINX, MYSQL, PHP (LEMP Stack) on Ubuntu

How To Install Linux, Apache, MYSQL, PHP (LAMP Stack) on Ubuntu


Hope it help you. Happy Coding :)








Author Image
AUTHOR

Channaveer Hakari

I am a full-stack developer working at WifiDabba India Pvt Ltd. I started this blog so that I can share my knowledge and enhance my skills with constant learning.

Never stop learning. If you stop learning, you stop growing