How to install multiple PHP version on ubuntu/linux

To install different versions of php on your linux/ubuntu machine follow the commands below:

# first check to see if we have php installed
sudo apt show php  

//OR 
sudo apt show php -a

// to install default php version
// from ubuntu software repository
sudo apt install php

// Install PHP (5.6, 7.x, 8.0) on Ubuntu Using PPA
sudo apt install python-software-properties
sudo add-apt-repository ppa:ondrej/php
sudo apt-get update


// for apache server
// choose only the version you need
// from the following list
sudo apt install php5.6
sudo apt install php7.0
sudo apt install php7.1
sudo apt install php7.2
sudo apt install php7.3
sudo apt install php7.4
sudo apt install php8.0

// for nginx server
// choose only the version you need
// from the following list
sudo apt install php5.6-fpm
sudo apt install php7.0-fpm
sudo apt install php7.1-fpm
sudo apt install php7.2-fpm
sudo apt install php7.3-fpm
sudo apt install php7.4-fpm
sudo apt install php8.0-fpm

// if you want to install php extentions
// use following command after you ran above commands
// choose one of the following as per your php requirements
sudo apt install php5.6-cli php5.6-xml php5.6-mysql
sudo apt install php7.0-cli php7.0-xml php7.0-mysql
sudo apt install php7.1-cli php7.1-xml php7.1-mysql
sudo apt install php7.2-cli php7.2-xml php7.2-mysql
sudo apt install php7.3-cli php7.3-xml php7.3-mysql
sudo apt install php7.4-cli php7.4-xml php7.4-mysql
sudo apt install php8.0-cli php8.0-xml php8.0-mysql

// check installed php version
php -v

// how to switch to different version
sudo update-alternatives --set php /usr/bin/php5.6
sudo update-alternatives --set php /usr/bin/php7.0
sudo update-alternatives --set php /usr/bin/php7.1
sudo update-alternatives --set php /usr/bin/php8.0