How to Install Multiple PHP Versions on Ubuntu 22.04

Hello, developers! In this article, we'll see how to install multiple php versions on Ubuntu 22.04. Here, we'll install multiple PHP versions in the Ubuntu 23.04 version. We will use the Ondrej PPA for installing PHP on the Ubuntu 22.04 LTS system. Which contains PHP 8.3, 8.2, 8.1, 8.0, 7.4, 7.3, 7.2. 7.1, 7.0 & PHP 5.6 packages.

In this tutorial, you will learn how to install PHP on Ubuntu 22.04 LTS system. This tutorial is also compatible with Ubuntu 20.04, and 18.04 systems.

Install Multiple PHP Versions Ubuntu

install multiple php versions ubuntu 22.04

 

Step 1: System Update

In this step, we'll update the system using the following command.

sudo apt-get update
sudo apt-get upgrade

 

Step 2: Installing Multiple PHP Versions on Ubuntu 22.04

Next, we'll install the PHP version from ondrej/php library using the following command.

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

Install PHP 8.1

Now you can install PHP8.1 on Ubuntu by executing the following command:

sudo apt-get install php8.1 php8.1-fpm

And install some common PHP8.1 extensions.

sudo apt-get install php8.1-mysql php8.1-mbstring php8.1-xml php8.1-gd php8.1-curl

When prompted, press y to confirm the installation.

Install PHP 8.0

Now you can install PHP8.0 on Ubuntu by executing the following command:

sudo apt-get install php8.0 php8.0-fpm

And install some common PHP8.0 extensions.

sudo apt-get install php8.0-mysql php8.0-mbstring php8.0-xml php8.0-gd php8.0-curl

Install PHP 7.4

Now you can install PHP7.4 on Ubuntu by executing the following command:

sudo apt-get install php7.4 php7.4-fpm

And install some common PHP7.4 extensions.

sudo apt-get install php7.4-mysql php7.4-mbstring php7.4-xml php7.4-gd php7.4-curl

 

Step 3: Check the Active PHP Version

Now, we'll check the PHP version using the following command.

php -v
 
Step 4: Switch the Default PHP Version for CLI

Next, we'll switch the PHP version using the following command.

sudo update-alternatives --config php

multiple_php_version_ubuntu

To change the default version, enter the number that matches the appropriate version you want to make as the default and press ENTER.

For example, to make PHP 7.4 the default version, type 3 and press ENTER.

You can execute the command below to change the version straight away:

sudo update-alternatives --set php /usr/bin/php7.4

 

Step 5: Uninstalling PHP Versions

Now, we'll uninstall PHP using the following command.

sudo apt-get remove php7.4

Also, uninstall all the modules for that version, Run the following command:

sudo apt-get remove php7.4-*

 


You might also like:

techsolutionstuff

Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of techsolutionstuff.com. Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.

RECOMMENDED POSTS

FEATURE POSTS