How to Switch Between Multiple PHP Versions on Ubuntu/Linux

Published on
-
3 mins read
Authors
How to Switch Between Multiple PHP Versions on Ubuntu/Linux

In this guide, we’ll walk through how to switch between multiple PHP versions installed on your Ubuntu or Linux system.

This tutorial was tested on Ubuntu 20.04 LTS, but it should work just fine on other Ubuntu-based distributions as well — whether older or newer versions.

Introduction

Sometimes, the latest version of a package or application doesn’t behave as expected. In many cases, your application might not be compatible with the newest PHP version and instead requires an older, specific version to run properly.

The good news? You don’t need to uninstall or downgrade anything.

If you already have multiple PHP versions installed, you can switch between them in just a few steps whenever needed.

Prerequisites

Before getting started, make sure you have:

  • A running Ubuntu/Linux system (or any of its derivatives)
  • Multiple PHP versions already installed on your system

Note: This guide assumes PHP is already installed. We won’t cover the installation process here.

Let’s Get Started

1. Check Your Current PHP Version

First, open your terminal and run:

php -v

You’ll see output similar to this:

PHP 7.3.33-1+ubuntu20.04.1+deb.sury.org+1 (cli) (built: Nov 19 2021 06:25:05) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.33, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.3.33-1+ubuntu20.04.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies

In this example, the active version is PHP 7.3.

Don’t worry if your version is different — this will vary depending on your system.


2. List Available PHP Versions

Next, run the following command to see all installed PHP versions and configure the default one:

sudo update-alternatives --config php

You’ll get a list like this:

There are 2 choices for the alternative php (providing /usr/bin/php).
Selection Path Priority Status
------------------------------------------------------------
0 /usr/bin/php7.4 74 auto mode
* 1 /usr/bin/php7.3 73 manual mode
2 /usr/bin/php7.4 74 manual mode
Press <enter> to keep the current choice[*], or type selection number:

3. Switch to Your Desired PHP Version

To switch versions, simply type the selection number and press Enter.

For example, enter:

0

This will switch your system to PHP 7.4:

There are 2 choices for the alternative php (providing /usr/bin/php).
Selection Path Priority Status
------------------------------------------------------------
0 /usr/bin/php7.4 74 auto mode
* 1 /usr/bin/php7.3 73 manual mode
2 /usr/bin/php7.4 74 manual mode
Press <enter> to keep the current choice[*], or type selection number: 0
update-alternatives: using /usr/bin/php7.4 to provide /usr/bin/php (php) in auto mode

4. Verify the Change

Finally, confirm the active PHP version by running:

php -v

You should now see something like:

PHP 7.4.27 (cli) (built: Dec 20 2021 21:28:15) ( NTS )
Copyright (c) The PHP Group Zend Engine v3.4.0, Copyright (c) Zend Technologies
with Zend OPcache v7.4.27, Copyright (c), by Zend Technologies

Nice — your PHP version has been successfully switched.


Conclusion

Switching between PHP versions on Ubuntu/Linux is quick and painless once you know the trick.

With update-alternatives, you can easily manage compatibility issues without uninstalling or reinstalling anything.

This approach is especially useful when working on multiple projects that depend on different PHP versions.

That’s all. I hope this was helpful. See you later.