How To Switch Between Multiple PHP Versions in Ubuntu

| | 1 min read

PHP is an open-source, popular general-purpose scripting language. Based on our requirement, we might need to switch the PHP version from one to another.

We can install multiple versions of PHP in Ubuntu and switch between one version to another without the need to uninstall the other. Let us see the below steps on how we could switch the versions.

For example, We expect you already have PHP 7.2, and PHP 7.4 installed on the server, and the default one running might be PHP 7.2.

Step 1

sudo a2dismod php7.2

Step 2

sudo a2enmod php7.4

Step 3

sudo service apache2 restart

The above commands will switch the PHP version from 7.2 to 7.4

Also, You could run the below command to set the default PHP versions on the server based on your requirement.

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