Ubuntu offers the support of various PHP versions for website development. PHP offers regular updates to overcome the deficiencies of the previous versions. The project created on a specific PHP version may not work properly on other versions. To overcome this, the user needs to switch between them to attain the functionality of the specific version.
Considering the importance of PHP versions, this post will address various methods to switch between PHP versions on Ubuntu:
- Method 1: Using a2 Scripts to Switch Between PHP Versions
- Method 2: Determine the Symlink of php Command to Switch PHP Versions
Method 1: Using a2 Scripts to Switch Between PHP Versions
The “a2” apache module allows you to disable or enable the specific modules. This phenomenon helps in disabling the specific PHP version and then enabling the desired one. To do so, follow the steps demonstrated below:
Step 1: Check the Current PHP version
Let’s display the current version of the PHP available in the operating system. To do so, the following command is carried out:
$ php -v
The current PHP version is 8.1.
Step 2: Disable the PHP Version
The next step is to use the “a2dismod” command with sudo permission to disable the PHP module of 8.x:
$ sudo a2dismod php8.1
The current PHP version 8.1 will be disabled and the user needs to restart the apache2 service in order activate the new configuration.
Step 3: Enable the PHP Version
Make sure that the PHP version you want to switch to is installed on your system. As an example, we will enable PHP 5.6 on the system. Use the “a2enmod” command to enable the PHP, i.e., 5.6 in our case:
$ sudo a2enmod php5.6
The PHP module 5.6 has been enabled.
Step 4: Set the New PHP as Default
After that, set the PHP version (5.6 in this scenario) as default using the following command:
$ sudo update-alternatives --set php /usr/bin/php5.6
The default PHP version 5.6 has been set.
Step 5: Restart the Apache
Finally, restart the apache services to apply the changes:
$ sudo systemctl restart apache2
The apache service has been restarted.
Verify the Change
Let’s check the version of PHP to verify the above steps are successfully performed:
$ php -v
The PHP version has been downgraded to 5.x.
Method 2: Determine the Symlink of php Command to Switch PHP Versions
The “update-alternatives” command offers the functionality to update the symbolic links of the default commands. This can be used to change the default direction of the php command which will allow switching the version. The two steps procedure is provided below:
Step 1: List and Change the PHP Version
If you have multiple PHP versions installed in the operating system, then you can use the below-given command to switch between them:
$ sudo update-alternatives --config php
The execution of the below command will ask you to enter the selection number of your choice, then hit the enter button:
Step 2: Verify the Version
Let’s check the version of the PHP and verify it:
$ php -v
The PHP version has been switched to 5.6 as given in the above selection.
To switch between the multiple PHP versions, disable the current PHP using the “a2dismod” command and enable the new one using “a2enmod”. After that, set it as the default PHP module and restart the apache services. Using this, the user can downgrade or upgrade to the available PHP versions. Moreover, users can use the “sudo update-alternatives –config php” command to get the installed versions and set one of them as default. This write-up has illustrated the methods to switch between the multiple PHP versions in Ubuntu.