Install multiple PHP versions on Ubuntu 20.04

by Pete Wan

You may need to work on various PHP applications which require different versions of PHP. In this post we will explain how to install multiple PHP versions and switch between them.

Step 1. Add ondrej/php PPA repository

The ondrej/php PPA repository enables you to easily install multiple PHP versions on your system. Before you add this repository you need the software-properties-common package which provides an apt-add-repository command-line utility.

1. Install software-properties-common package:

$ sudo apt-get install software-properties-common

2. Add ondrej/php PPA repository (press enter key when prompted):

$ sudo add-apt-repository ppa:ondrej/php

or if your machine is behind a corporate proxy:

$ sudo -E add-apt-repository ppa:ondrej/php

Step 2. Install Multiple PHP Versions

We will now install multiple PHP versions and check the current version.

1. Install PHP 7.2:

$ sudo apt install -y php7.2

2. Install PHP 7.3:

$ sudo apt install -y php7.3

3. Install PHP 7.4:

$ sudo apt install -y php7.4

4. Check current PHP version:

$ php -v

The terminal will response will look like:

PHP 7.4.11 (cli) (built: Oct 18 2020 19:39:42) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.11, Copyright (c), by Zend Technologies

Step 4. Switch between PHP Versions

The default PHP version can be set using update-alternatives command:

$ sudo update-alternatives --config php

The terminal response will look like:

  Selection    Path             Priority   Status
* 0            /usr/bin/php7.4   74        auto mode
  1            /usr/bin/php7.2   72        manual mode
  2            /usr/bin/php7.3   73        manual mode
  3            /usr/bin/php7.4   74        manual mode

Press <enter> to keep the current choice[*], or type selection number: 

Type the selection number to switch to the desired PHP version.

Leave a Comment