DEPRECATED: Multiple PHP CLIs (Ubuntu) - Gizra/KnowledgeBase GitHub Wiki
Instead of having multiple PHP installed locally, please use DDEV.
Taken from the next post: https://tecadmin.net/install-multiple-php-version-apache-ubuntu/
Apache installation
sudo apt update
sudo apt install apache2 libapache2-mod-fastcgi
# Ubuntu 18.04 Users:
sudo apt install apache2 libapache2-mod-fcgid
PHP Installation
# On Ubuntu 16.04
sudo apt install python-software-properties
sudo add-apt-repository ppa:ondrej/php
# On Ubuntu 18.04
sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php
Install any needed version
- Let's take version 7.2 for example:
apt update sudo apt install php7.2 php7.2-fpm
Restart the php-fpm services per version
sudo systemctl status php7.2-fpm
Apache configuration
Modify the virtual host that should use a specific version, add the next to it:
<VirtualHost *:80>
<FilesMatch \.php$>
SetHandler "proxy:unix:/var/run/php/php7.2-fpm.sock|fcgi://localhost"
</FilesMatch>
</VirtualHost>
Restart the apache
sudo systemctl restart apache2
Install php packages per version
For each php package/extension, need to install per version.
For example, mysql or gd or tidy or curl:
sudo apt install php7.2-mysql
sudo apt-get install php7.2-gd
sudo apt-get install php7.2-tidy
sudo apt-get install php7.2-curl
Switch between version
sudo update-alternatives --config php

Personally, I add an alias to ~/.bashrc to easily switch:
alias phpswitch='sudo update-alternatives --config php'