Introduction
Managing multiple PHP versions on a single Ubuntu server is a common requirement for developers and system administrators. Different projects may rely on specific PHP versions, and being able to switch between them seamlessly ensures compatibility and flexibility.
In this guide, we’ll cover how to check installed PHP versions, switch between them globally, and configure them for Apache web servers.
if you don’t know how to install PHP in ubuntu server or still you did not install PHP, then please read my other article.
So, I assume you have already install at least 2 PHP versions in your ubuntu system.
Today, I will guide how to switch PHP7.4 to PHP8.1 and vice-versa.
Steps for switch PHP versions

Check current PHP version
Let us check the default installed version of PHP using command:
php -v
Switch From PHP7.4 to PHP8.1
1. First, disable the currently active PHP module (e.g., PHP 7.4):
sudo a2dismod php7.4

2. Enable the PHP version you want to use (e.g., PHP 8.1):
sudo a2enmod php8.1

3. Update the default PHP version to match the one you’ve enabled:
sudo update-alternatives --set php /usr/bin/php8.1

4. To see all installed PHP versions and configure them manually:
sudo update-alternatives --config php

Select the desired PHP version from the list.
5. Finally, restart Apache to apply the changes:
sudo systemctl restart apache2
6. Verify the Active PHP Version
php -v

You should now see the selected PHP version as the default.
If you getting an error
ERROR: Module php8.1 does not exist!
,
Just execute this command: sudo apt-get install libapache2-mod-php8.1
Using above command, you issue will fix.
Switch PHP using shell script
1. Create switch.sh
file anywhere. Here, I am creating this file to /var/www/html
path.
2. Add below content to this file and save it
#!/bin/bash
# This is a bash script that allows you to switch between different versions of PHP.
# Reference: https://github.com/vinugawade/s-php/blob/master/s-php
# s-php script version.
VERSION="1.0.0"
# Function to display error/help messages.
function show_msg() {
[[ $1 == "invalid" ]] && echo -e "sphp: Invalid argument"
[[ $1 == "help" ]] && echo "Usage:
s-php <version> | [options]
Easily switch PHP versions on Linux.
Options:
-h, --help display this help and exit
-v, --version display s-php script version"
# Display versions separated by `,`.
echo -e "\nPHP Versions :-"
versions=$(printf '%s, ' ${php_ver[*]})
echo " ${versions::-2}"
exit 1
}
# Define available PHP versions.
php_ver=("5.6" "7.0" "7.1" "7.2" "7.3" "7.4" "8.0" "8.1" "8.2")
# php_ver=("7.1" "7.2" "7.3" "7.4" "8.0" "8.1" "8.2")
# Check input value.
if [ -z "$1" ]; then
show_msg invalid # Show invalid input message.
elif [ $# -gt 1 ]; then
echo -e "Too many arguments:- $*"
exit 1
else
# Check input and show help message.
if [[ ($* == "--help") || ($* == "-h") ]]; then
show_msg help
fi
# Check input and script version.
if [[ ($* == "--version") || ($* == "-v") ]]; then
echo -e "s-php v${VERSION} \nVisit :- https://vinugawade.github.io/s-php"
exit 1
fi
# Check valid PHP version input.
if [[ ${php_ver[*]} =~ (^|[[:space:]])"${*}"($|[[:space:]]) ]]; then
php="php${*}"
phar="phar${*}"
echo -e "Disabling PHP versions."
echo "---------------------------"
# Disable active PHP of apache.
for i in "${php_ver[@]}"; do
sudo a2dismod "php${i}" > /dev/null
printf 'php%s x \n' "${i}"
done
else
show_msg invalid # Show invalid input message.
fi
echo -e "\nActivating PHP version. \u2714"
echo "---------------------------"
# Change PHP version of system.
sudo update-alternatives --set php /usr/bin/"${php}" > /dev/null
sudo update-alternatives --set phar /usr/bin/"${phar}" > /dev/null
sudo update-alternatives --set phar.phar /usr/bin/phar."${phar}" > /dev/null
printf '%s \u2714 \n' "${php}"
# Check apache server is active or not.
if pgrep -x apache2 > /dev/null; then
# Enable PHP version for apache.
echo -e "\nSwitch apache PHP version \u2714"
echo "---------------------------"
sudo a2enmod "${php}" > /dev/null
# Restart apache server.
echo -e "\nRestart apache server \u2714"
echo "---------------------------"
sudo systemctl restart apache2 > /dev/null
sudo service apache2 restart > /dev/null
else
echo -e "\nApache server not running x"
fi
# Print new PHP cli version.
echo -e "\nCurrent PHP version :-"
echo "---------------------------"
php -v
exit 1
fi
3. Give permission to switch.sh
file:
sudo chmod u+x switch.sh
4. Move script file for accessing globally by run following command:
sudo mv switch.sh /usr/bin/switch
5. Run below command to switch PHP version, like if you want to switch PHP version 8.1:
switch 8.1
6. See output

References
Conclusion
Hopefully!!! you find the easiest way to switch multiple PHP versions in Ubuntu. With the help of the update-alternatives
command, we can quickly change from one PHP version to another in Ubuntu and set default PHP version as per your requirement.
Thank you for reading this article!!!