RackNerd Billboard Banner

How To Switch Between Multiple PHP Versions In Ubuntu

Running multiple PHP versions on the same Ubuntu system is common—especially if you’re juggling different web projects. Some apps need PHP 7.4, others demand PHP 8.2. Luckily, switching between them is quick once you know the commands.

This guide will walk you through checking installed PHP versions, switching between them, and setting the default version.


1. Check Installed PHP Versions

First, see what PHP versions are already installed:

php -v

This shows the version currently in use.

To list all installed versions:

update-alternatives --list php

If you don’t see multiple versions listed, you’ll need to install them first.


2. Install Another PHP Version

If you need a different PHP version, you can install it from the Ondřej Surý PPA, which is the go-to source for multiple PHP versions on Ubuntu.

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

Now install the version you need, for example:

sudo apt install php8.2

3. Switch Between PHP Versions

To switch PHP versions for the CLI (Command Line Interface):

sudo update-alternatives --set php /usr/bin/php8.2

Or, use the interactive method to choose from available versions:

sudo update-alternatives --config php

You’ll see a list like:

There are 2 choices for the alternative php (providing /usr/bin/php).

  Selection    Path             Priority   Status
------------------------------------------------------------
* 0            /usr/bin/php8.1   81        auto mode
  1            /usr/bin/php7.4   74        manual mode
  2            /usr/bin/php8.1   81        manual mode
  3            /usr/bin/php8.2   82        manual mode

Type the number for the version you want and press Enter.


4. Switch PHP for Apache

If you’re using Apache, you also need to switch the PHP module:

sudo a2dismod php8.1
sudo a2enmod php8.2
sudo systemctl restart apache2

5. Switch PHP for Nginx + PHP-FPM

For Nginx, you’ll adjust the PHP-FPM service:

sudo nano /etc/nginx/sites-available/your-site.conf

Look for the fastcgi_pass line and change it to match the correct PHP-FPM socket, for example:

fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;

Then restart services:

sudo systemctl restart php8.2-fpm
sudo systemctl restart nginx

6. Confirm the Active Version

Run:

php -v

Or create a phpinfo.php file in your web root to confirm Apache/Nginx changes:

<?php phpinfo();

Final Tips

  • Use update-alternatives for CLI PHP.
  • Apache needs a2dismod / a2enmod to switch PHP versions.
  • Nginx requires updating the PHP-FPM socket in its config.
  • Keep older PHP versions updated for security—or uninstall them if unused.

With these steps, you can juggle multiple PHP versions on Ubuntu without breaking your projects.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
RackNerd Billboard Banner
© 2025 Computer Everywhere
Your Everyday Guide to the Digital World.
Terms of Service | Privacy Policy
Copy link