Want to use multiple PHP versions on Ubuntu? Don’t worry — with Ubuntu you can easily switch between PHP versions.
This special feature only works on Ubuntu because it depends on the PPA repository. And we have Ondřej Surý to thank for creating this repository and maintaining it so well.
For other distros, you need to build from the source code, and that will probably be covered in a different article.
- Ondřej Surý’s PPA repository
sudo add-apt-repository ppa:ondrej/phpsudo add-apt-repository ppa:ondrej/apache2sudo apt update - Install Apache
sudo apt updatesudo apt install apache2 - Install PHP 8.3
sudo apt install php8.3 php8.3-cli libapache2-mod-php8.3 php8.3-mysql php8.3-pgsql - Install PHP 7.4
sudo apt install php7.4 php7.4-cli libapache2-mod-php7.4 php7.4-mysql php7.4-pgsql - Switch the PHP version on the CLI
sudo update-alternatives --config php
FAQ
Can this be used on WSL 2 Ubuntu?
Yes, and this tutorial uses WSL 2 Ubuntu. But note that it requires the latest version of Windows, because it needs SystemD, which is only available in the newer WSL versions.
References
- https://www.digitalocean.com/community/tutorials/how-to-run-multiple-php-versions-on-one-server-using-apache-and-php-fpm-on-ubuntu-20-04
- https://launchpad.net/~ondrej/+archive/ubuntu/php
- https://ubuntu.com/tutorials/install-and-configure-apache#1-overview
- https://www.digitalocean.com/community/tutorials/how-to-install-lamp-stack-on-ubuntu