How to Install Different / Multiple PHP Versions of PHP in Ubuntu

Share:
how-to-install-different-multiple-php-versions-of-php-in-ubuntu

PHP (recursive acronym for PHP: Hypertext Preprocessor) is a server-side scripting language designed for web development but also used as a general-purpose programming language. Different applications require various versions of PHP, so in this tutorial, we are going to install multiple versions of PHP on Ubuntu 21.04.

Although this article use Ubuntu 21.04, it should also applies to any modern Ubuntu versions.

If you have older Ubuntu versions, you must add PPA repository first. You can read more detailed instructions about PHP 7.4 installation on Ubuntu 18.04 here.

We will also show how to set the default version of PHP to be used on the Ubuntu system as when required. We will be installing multiple PHP versions, PHP 7.4 and PHP 8 on a single Ubuntu Server.

Currently, the supported stable version of PHP in the Ubuntu APT software repositories is the PHP 7.4. You can confirm this by running the apt command below.

$ sudo apt show php-fpm -a

The output should be similar with below:

Package: php-fpm
Version: 2:7.4+76ubuntu1
Priority: optional
Section: universe/php
Source: php-defaults (76ubuntu1)
Origin: Ubuntu
Maintainer: Ubuntu Developers <[email protected]>
Original-Maintainer: Debian PHP Maintainers <[email protected]>
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Installed-Size: 13.3 kB
Depends: php7.4-fpm
Download-Size: 3048 B
APT-Sources: http://mirrors.digitalocean.com/ubuntu hirsute/universe amd64 Packages
Description: server-side, HTML-embedded scripting language (FPM-CGI binary) (default)
This package provides the Fast Process Manager interpreter that runs
as a daemon and receives Fast/CGI requests.
.
PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used
open source general-purpose scripting language that is especially suited
for web development and can be embedded into HTML.
.
This package is a dependency package, which depends on latest stable
PHP version (currently 7.4).

We can verify that the default PHP version is 7.4.

PHP 7.4 Installation

To install the default PHP version from the Ubuntu software repositories, use the command below:

$ sudo apt install php-fpm

The system will respond like below:

Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages were automatically installed and are no longer required:
libonig5 libzip4
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
libsodium23 php7.4-cli php7.4-common php7.4-fpm php7.4-json php7.4-opcache php7.4-readline
Suggested packages:
php-pear
The following NEW packages will be installed:
libsodium23 php-fpm php7.4-cli php7.4-common php7.4-fpm php7.4-json php7.4-opcache php7.4-readline
0 upgraded, 8 newly installed, 0 to remove and 0 not upgraded.
Need to get 4236 kB of archives.
After this operation, 18.5 MB of additional disk space will be used.
Do you want to continue? [Y/n] y

Just press "y" followed by "Enter" and the installation process will continue.

When the installation process finished, let's verify our installation by typing this command:

$ php -v

It should respond with something like this:

PHP 7.4.16 (cli) (built: Jul  5 2021 13:04:38) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
with Zend OPcache v7.4.16, Copyright (c), by Zend Technologies

Bonus (Installing Laravel dependencies)

Before we're able to install Laravel application in this machine, we need to install more packages. Here are the packages:

$ sudo apt install unzip mariadb-server php7.4-mbstring php7.4-xml php7.4-gd php7.4-zip php7.4-mysql php-json php-curl

PHP 8 Installation

Before we're able to install PHP 8.0, we need to add PPA repository called ondrej. I've published a detailed article in this post. Once you've finished installing PHP 8, we can choose which PHP version we want to run as default.

Set Default PHP Version

We can set the default PHP version to be used on the system with the update-alternatives command.

$ sudo update-alternatives --set php /usr/bin/php8.0

System will respond with something like this:

update-alternatives: using /usr/bin/php8.0 to provide /usr/bin/php (php) in manual mode

After setting it, check the PHP version to confirm as follows:

$ php -v

We should get something like below:

PHP 8.0.11 (cli) (built: Sep 23 2021 21:26:42) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.11, Copyright (c) Zend Technologies
with Zend OPcache v8.0.11, Copyright (c), by Zend Technologies

To switch back to PHP 7.4, we'll use update-alternatives command again:

$ sudo update-alternatives --set php /usr/bin/php7.4

Bonus: Nginx Configuration

There's chance you need to run multiple PHP versions simultaneously. If you're running Nginx web server, it could be done like below.

Website with PHP 7.4

... 
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
...

Website with PHP 8.0

... 
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;
}
...

Final Words

I hope that you now know how to install different / multiple PHP versions in Ubuntu. If you run into any issues or have any feedback feel free to drop a comment below.

Share:

0 comment

Leave a reply

Your email address will not be published. Required fields are marked *