Sep 30, 2023 4 min read

How Do I Switch Between PHP Versions on Ubuntu?

Switch Between PHP Versions on Ubuntu with our step-by-step tutorial. Ubuntu supports multiple PHP versions for website development.

Switch Between PHP Versions on Ubuntu
Table of Contents

Introduction

Before we begin talking about how to switch between PHP Versions on Ubuntu, let's briefly understand – What is PHP Version?

Ubuntu provides support for a number of PHP versions for the creation of websites. To fix the flaws in earlier versions, PHP releases frequent upgrades. It's possible that a project made with one version of PHP won't function correctly with another. To get around this, the user has to switch between them in order to use the features of the particular version.

In this tutorial, you will switch between PHP Versions on Ubuntu. We will also address a few FAQs on how to switch between PHP Versions on Ubuntu.

Method 1: Switching Between PHP Versions Using a2 Scripts

You can enable or disable the particular modules using the "a2" apache module. This phenomenon makes it easier to disable a certain PHP version and subsequently enable the one you want. To do this, adhere to the instructions illustrated below:

Step 1: Check the Current PHP version

Let's display the most recent PHP version that the operating system supports. To achieve this, the command is executed as follows:

php -v

The most recent PHP release is 8.1.

Step 2: Disable the PHP Version

The next step is to disable the PHP module for version 8.x using the "a2dismod" command with sudo permission:

sudo a2dismod php8.1

The user must restart the apache2 service in order to activate the new configuration and disable the current PHP version 8.1.

Step 3: Enable the PHP Version

Ensure that your system has the PHP version you want to switch to is installed. We'll enable PHP 5.6 as an example for the system. To enable the PHP, which is version 5.6 in our case, use the "a2enmod" command:

sudo a2enmod php5.6

The PHP 5.6 module is now active.

Step 4: Set the New PHP to Default

Then, use the following command to make the PHP version (5.6 in this case) the default:

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

The default PHP version is now 5.6.

Step 5: Restart the Apache

To apply the modifications, lastly restart the apache services:

sudo systemctl restart apache2

There has been a restart of the apache service.

Verify the Change

In order to confirm that the previous procedures were successfully carried out, let's check the PHP version:

php -v

PHP has been downgraded to version 5.x.

The "update-alternatives" command provides the ability to update the default commands' symbolic links. This can be used to alter the php command's default orientation, enabling version switching. Below is a description of the two-step process:

Step 1: List and Change the PHP Version

If your operating system has numerous versions of PHP installed, you can switch between them by using the command below:

sudo update-alternatives --config php

When the following command is run, you will be prompted to enter the selection number of your choosing before pressing the enter key:

Step 2: Verify the Version

Let's check the PHP version and confirm it:

php -v

As specified in the aforementioned selection, PHP version has been changed to 5.6.

FAQs to Switch Between PHP Versions on Ubuntu

Can I have multiple PHP versions running simultaneously on Ubuntu? 

Yes, you can have multiple PHP versions installed and running simultaneously. Different projects can use different PHP versions by configuring the web server appropriately.

How can I configure Apache to use a different PHP version on Ubuntu? 

You need to install the appropriate PHP module for Apache using the package manager (apt). Then, enable the module and restart Apache: sudo a2enmod php7.4 && sudo systemctl restart apache2.

Can I switch PHP versions for the command-line interface (CLI) too?

Yes, you can switch PHP versions for the command-line by using the update-alternatives command or by specifying the absolute path to the desired PHP executable. For example, php7.4 -v will execute PHP 7.4.

Are there any tools to make switching between PHP versions easier on Ubuntu? 

Yes, tools like phpswitch or phpbrew can simplify the process of installing and switching between multiple PHP versions on Ubuntu.

Is it possible to automatically switch PHP versions based on project directories? 

Yes, using tools like phpenv or configuring your web server's virtual host settings, you can automatically switch PHP versions based on the project directory.

How do I set the default PHP version on Ubuntu? 

You can set the default PHP version on Ubuntu by running the command sudo update-alternatives --config php and selecting the desired version.

Can I switch between PHP versions only for specific projects? 

Yes, you can switch between PHP versions for specific projects by using tools like phpenv or phpbrew which enable per-project PHP version management.

Conclusion

Use the "a2dismod" and "a2enmod" commands to enable and disable the various PHP versions, respectively, to switch between them. Set it as the default PHP module after that, and then restart the Apache services. The user can utilize this to downgrade or upgrade to the various PHP versions. Additionally, users can obtain the installed versions and designate one of them as the default by using the "sudo update-alternatives -config php" command. This tutorial has provided instructions on how to change between the various PHP versions on Ubuntu.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.