How to Install Multiple PHP Versions on Ubuntu 22.04
Tags: ubuntu, linux, php
Published: 2022-07-19 17:55:20

[PHP](https://www.php.net/?ref=techvblogs.com)  is a programming language used for developing web applications. You must install PHP packages on a Ubuntu system to run the application written on it. Generally, it is used to create e-commerce websites, blogs, and API applications. If you’re looking for an easy way to install PHP on Ubuntu 22.04, look no further. In this blog post, we’ll show you how to do it quickly and easily.

We will use the  [Ondrej PPA](https://launchpad.net/~ondrej/+archive/ubuntu/php?ref=techvblogs.com)  for installing PHP on Ubuntu 22.04 LTS system. Which contains PHP  [8.1](https://www.php.net/releases/8.1/en.php?ref=techvblogs.com),  [8.0](https://www.php.net/releases/8.0/en.php?ref=techvblogs.com),  [7.4](https://www.php.net/releases/7_4_0.php?ref=techvblogs.com),  [7.3](https://www.php.net/releases/7_3_0.php?ref=techvblogs.com),  [7.2](https://www.php.net/releases/7_2_0.php?ref=techvblogs.com).  [7.1](https://www.php.net/releases/7_1_0.php?ref=techvblogs.com),  [7.0](https://www.php.net/releases/7_0_0.php?ref=techvblogs.com)  & PHP  [5.6](https://www.php.net/releases/5_6_0.php?ref=techvblogs.com)  packages. You can install any of the versions as required for your application. The new application developers are suggested to use the latest PHP version ie PHP 8.1.

In this tutorial, you will learn how to install PHP on  [Ubuntu 22.04 LTS](https://releases.ubuntu.com/22.04?ref=techvblogs.com)  system. This tutorial is also compatible with  [Ubuntu 20.04](https://releases.ubuntu.com/20.04?ref=techvblogs.com), and  [18.04](https://releases.ubuntu.com/18.04?ref=techvblogs.com)  systems.

### Step 1: System Update

First, log in to **Ubuntu 22.04**  via console. Then update the Apt cache and upgrade the current packages of the system using the following command:

```bash
sudo apt-get update
sudo apt-get upgrade 
```

When prompted, press **y**  to confirm the installation.

### Step 2: Installing Multiple PHP Versions on Ubuntu 22.04

The easiest way to install multiple versions of PHP is by using the PPA from Ondřej Surý, who is a Debian developer. To add this PPA, run the following commands in the terminal. The software-properties-common package is needed if you want to install software from PPA. It’s installed automatically on the Ubuntu desktop but might miss on your Ubuntu server.

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

The SURY repository contains PHP 8.1, 8.0, 7.4, 7.3, 7.2, 7.1, 7.0 & PHP 5.6. As the latest stable version of  **PHP is 8.0**, but many websites still required  **PHP 7**. You can install any of the required PHP versions on your system.

#### Install PHP 8.1:

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

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

And install some common  `PHP8.1` extensions.

```bash
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.

You can view all available  `PHP8.1`  extensions by typing in  `sudo apt-get install php8.1` and pressing Tab key twice.

![Install Multiple PHP Version on Ubuntu 22.04 - TechvBlogs](https://res.cloudinary.com/techvblogs/image/upload/v1658116614/blog/install-multiple-php-version-on-ubuntu-22-04/Selection_048_s6ycys.png)

#### Install PHP 8.0:

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

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

And install some common  `PHP8.0` extensions.

```bash
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:

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

And install some common  `PHP7.4` extensions.

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

When prompted, press **y**  to confirm the installation.

#### Install PHP 7.3:

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

```bash
sudo apt-get install php7.3 php7.3-fpm
```

And install some common  `PHP7.3` extensions.

```bash
sudo apt-get install php7.3-mysql php7.3-mbstring php7.3-xml php7.3-gd php7.3-curl
```

When prompted, press **y**  to confirm the installation.

#### Install PHP 7.2:

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

```bash
sudo apt-get install php7.2 php7.2-fpm
```

And install some common  `PHP7.2` extensions.

```bash
sudo apt-get install php7.2-mysql php7.2-mbstring php7.2-xml php7.2-gd php7.2-curl
```

When prompted, press **y**  to confirm the installation.

#### Install PHP 7.1:

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

```bash
sudo apt-get install php7.1 php7.1-fpm
```

And install some common  `PHP7.2` extensions.

```bash
sudo apt-get install php7.1-mysql php7.1-mbstring php7.1-xml php7.1-gd php7.1-curl
```

When prompted, press **y**  to confirm the installation.

#### Install PHP 5.6:

Now you can install  `PHP5.6` on Ubuntu by executing the following command:

```bash
sudo apt-get install php5.6 php5.6-fpm
```

And install some common  `PHP5.6` extensions.

```bash
sudo apt-get install php5.6-mysql php5.6-mbstring php5.6-xml php5.6-gd php5.6-curl
```

When prompted, press **y**  to confirm the installation.

### Step 3: Check Active PHP Version

Now after installation, verify that the correct version of PHP is installed by checking the version number by the below-mentioned command:

```bash
php -v
```

```bash
# Output
PHP 8.1.8 (cli) (built: Jul 11 2022 08:30:39) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.8, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.8, Copyright (c), by Zend Technologies
```

### Step 4: Switch Default PHP Version for CLI

If you have multiple versions of PHP installed on Ubuntu 22.04, you can choose to make one of the versions the default PHP version.

To list the available versions, run the following command:

```bash
sudo update-alternatives --config php

```

![Install Multiple PHP Version on Ubuntu 22.04 - TechvBlogs](https://res.cloudinary.com/techvblogs/image/upload/v1658118453/blog/install-multiple-php-version-on-ubuntu-22-04/Selection_049_quica9.png)

As you can see, we have  `PHP 8.1`  as the default version of PHP.

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:

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

After, Check the active PHP version:

```bash
php -v
```

```bash
# Output
PHP 7.4.30 (cli) (built: Jun 27 2022 08:21:19) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.30, Copyright (c), by Zend Technologies
```

### Step 5: Uninstalling PHP Versions

If any PHP version is no more required, it can be removed from the system. That will free the disk space as well as system security.

To uninstall any PHP version, Run the following command:

```bash
sudo apt-get remove php5.6
```

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

```bash
sudo apt-get remove php5.6-*
```

**Thank you** for reading this blog.