How to Change or Switch PHP Version on Ubuntu Easy Steps

In this tutorial you will learn How to Change or Switch PHP Version on Ubuntu. More then times we need to change the php version for our projects requirements beacuse all projects made on different-2 version such as php5.6, php7.1, php7.2, php7.3, php8.0 or above php version.

If your system, if you have installed multiple versions of PHP (eg PHP 7.3 and PHP 5.6 both) and the php5.6 is running as default PHP for Apache and CLI. For any requirement, you need to use PHP 7.3, then you don’t need to remove PHP 5.6, you just need to switch your PHP version to default used for Apache and command line.

Switch From PHP 5.6 to PHP 7.3

The PHP 5.6 is set as default version in your system, and you need to switch to PHP 7.3 the you need to tun the following commands to switch for Apache and command line.

Apache:-

sudo a2dismod php5.6  // (old PHP version)
sudo a2enmod php7.3  //  (new PHP version)
sudo service apache2 restart

Command Line:-

sudo update-alternatives --set php /usr/bin/php7.3
sudo update-alternatives --set phar /usr/bin/phar7.3
sudo update-alternatives --set phar.phar /usr/bin/phar.phar7.3

Switch From PHP 7.3 to PHP 5.6

If PHP 7.3 is set as a default version on your system and you need to switch to PHP 5.6. Run the following commands to switch for Apache and command line for switchig php version.

Apache:-

sudo a2dismod php7.3
sudo a2enmod php5.6
sudo service apache2 restart

Command Line:-

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

If Module don’t exits

If Module don’t exits Install the version first

sudo apt update
sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt update

sudo apt install php7.3

sudo apt install php7.3-common php7.3-mysql php7.3-xml php7.3-xmlrpc php7.3-curl php7.3-gd php7.3-imagick php7.3-cli php7.3-dev php7.3-imap php7.3-mbstring php7.3-opcache php7.3-soap php7.3-zip php7.3-intl -y

If you want switch php7.3 to 8.0 or php7.4 to php8.0 you can do the same as we have explained.

So dear friends today we learn how to switch or change multiple php version on ubuntu using command line easy way. Any queies or question let me know in comment box.

Leave a Comment