Introduction
If you are a system administrator or manage a web server, knowing how to install and use multiple PHP versions on a single machine is essential. It comes in handy when you host several PHP applications on the same server and each one is compatible with a different PHP version. In that case you can configure Nginx or Apache to pick the PHP version each site should use.
In this guide we will install PHP 7.4, 8.0 and 8.1 on Ubuntu 20.04 and learn how to switch the default command-line version.
Prerequisites
- A server running Ubuntu 20.04.
- A user with
sudoprivileges, or the root password configured on the server.
Step 1 — Adding the PHP Repository
By default, Ubuntu 20.04 ships with PHP 7.4. To install additional versions you need to add a third-party PHP repository to your system.
First, install the required dependencies:
sudo apt-get install software-properties-common gnupg2 -yThe software-properties-common package provides the apt-add-repository command-line tool, which you will use to add the ondrej/php PPA (Personal Package Archive).
Now add the ondrej/php repository. It carries more up-to-date PHP builds than the official Ubuntu repositories and lets you install several PHP versions on the same system:
sudo add-apt-repository ppa:ondrej/phpUpdate the package index:
sudo apt-get update -yWith the repository in place, you can install the PHP versions you need.
Step 2 — Installing the PHP Versions (7.4, 8.0 and 8.1 in this example)
To install PHP 7.4 with PHP-FPM support, run:
sudo apt-get install php7.4 php7.4-fpm php7.4-mysql libapache2-mod-php7.4 libapache2-mod-fcgid -yWhat each package does:
php7.4is a metapackage used to run PHP applications.php7.4-fpmprovides the FastCGI Process Manager, which runs as a daemon and accepts FastCGI requests.php7.4-mysqlconnects PHP to MySQL databases.libapache2-mod-php7.4provides the PHP module for the Apache web server.libapache2-mod-fcgidcontainsmod_fcgid, a module that starts multiple instances of CGI programs to handle concurrent requests.
Now repeat the process for PHP 8.0. Install php8.0, php8.0-fpm, php8.0-mysql and libapache2-mod-php8.0:
sudo apt-get install php8.0 php8.0-fpm php8.0-mysql libapache2-mod-php8.0 -yAnd again for PHP 8.1:
sudo apt-get install php8.1 php8.1-fpm php8.1-mysql libapache2-mod-php8.1 -yAll three PHP versions are now installed on your system. If you only use Nginx you can leave out the libapache2-* packages; PHP-FPM is all Nginx needs.
Step 3 — Setting the Default PHP Version for the Command Line
First, check which PHP version the command line currently uses:
php --versionYou will see output like this:
Output
PHP 8.0.5 (cli) (built: May 3 2021 11:30:57) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.5, Copyright (c) Zend Technologies
with Zend OPcache v8.0.5, Copyright (c), by Zend TechnologiesAs you can see, PHP 8.0 is the default version.
Now set the default to PHP 7.4 using the following command:
sudo update-alternatives --config phpYou will be asked to choose the default version, as shown below:
Output
There are 3 choices for the alternative php (providing /usr/bin/php).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/bin/php8.0 80 auto mode
1 /usr/bin/php7.4 74 manual mode
2 /usr/bin/php8.0 80 manual mode
3 /usr/bin/php8.1 81 manual mode
Press "enter" to keep the current choice[*], or type selection number: 1
update-alternatives: using /usr/bin/php7.4 to provide /usr/bin/php (php) in manual modeSelect the version you want and press ENTER to make it the default.
Verify the change:
php --versionOutput
PHP 7.4.18 (cli) (built: May 3 2021 11:27:06) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
with Zend OPcache v7.4.18, Copyright (c), by Zend TechnologiesYou can switch at any time by running update-alternatives --config php again, or jump straight to a version without the menu:
sudo update-alternatives --set php /usr/bin/php8.1Step 4 — Picking a PHP Version per Site in Nginx
The CLI default only affects commands you run in the terminal. Web requests are handled by whichever PHP-FPM socket a server block points to. Each version listens on its own socket:
/run/php/php7.4-fpm.sock
/run/php/php8.0-fpm.sock
/run/php/php8.1-fpm.sockSo, to serve one site with PHP 8.1 and another with PHP 7.4, set fastcgi_pass accordingly in each server block:
server {
listen 80;
server_name modern.your_domain;
root /var/www/modern;
index index.php;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
}
}
server {
listen 80;
server_name legacy.your_domain;
root /var/www/legacy;
index index.php;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
}Make sure the matching FPM service is running, then test and reload Nginx:
sudo systemctl enable --now php7.4-fpm php8.1-fpm
sudo nginx -t
sudo systemctl reload nginxConclusion
You have installed several PHP versions side by side on Ubuntu 20.04, learned how to switch the default command-line version with update-alternatives, and seen how to point each Nginx site at the PHP-FPM version it needs. If you have not set up Nginx yet, start with How To Install Nginx on Ubuntu.

