Install Different PHP Versions in Ubuntu

To install different PHP versions in Ubuntu, you can use the ondrej/php PPA (Personal Package Archive), which provides the latest PHP versions for Ubuntu. Here are the steps:

  1. Add the ondrej/php PPA to your system:
sudo add-apt-repository ppa:ondrej/php
  1. Update the package lists:
sudo apt-get update
  1. Install the PHP version you need, e.g. PHP 7.4:
sudo apt-get install php7.4
  1. Check if the installation was successful by running the following command:
php -v

This should output the version of PHP you just installed.

  1. To switch between PHP versions, you can use the update-alternatives command. For example, to switch to PHP 7.4, run:
sudo update-alternatives --set php /usr/bin/php7.4

And to switch back to the default PHP version, run:

sudo update-alternatives --auto php

That’s it! You now have multiple PHP versions installed on your Ubuntu system.