Installing Multiple PHP Versions on Linux / Linux / CentOS / Installing Multiple PHP Versions on Linux

You can run different versions of PHP on a Linux host at the same time. In this article, we’ll show you how to install multiple versions of PHP using Ubuntu/Debian as an example.

Checks which version of PHP is available in the default Ubuntu repository:

$ apt search --names-only '^php[.0-9]{3}$'

This is php8.1 in our example.

check available PHP version in linux repo

If the php package is not installed, you can install it with the command

$ sudo apt install php

If you need a version of PHP that is not included in the default repository, you will need to install an additional repo.

The latest PHP packages can be installed from the Ondřej Surý repository.

Add a new repo:

$ sudo apt install software-properties-common
$ sudo add-apt-repository ppa:ondrej/php
$ sudo apt update
If you get an error during installation “FileNotFoundError: [Errno 2] No such file or directory: gpg”, install gnupg-agent:

$ sudo apt install gnupg-agent

List the available PHP versions in the repository:

$ sudo apt-cache search php*

You can now install the PHP package versions you need:

$ sudo apt-get install php7.4
$ sudo apt-get install php8.2

List installed PHP versions:

$ dpkg --get-selections | grep -i php

List installed multiple PHP versions

In order to install multiple versions of PHP on rpm-based Linux distros (CentOS, RHEL, Rocky, and Oracle Linux), you can use the following commands:

# dnf -y install epel-release
# dnf install
# dnf install
# dnf module list php

Check which version of PHP is used by default:

$ php –v

PHP 8.2.5 (cli) (built: Apr 14 2023 04:27:02) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.5, Copyright (c) Zend Technologies
with Zend OPcache v8.2.5, Copyright (c), by Zend Technologies

Check the default phph version on Linux

To change the default version of PHP, run the command

$ sudo update-alternatives --config php

There are two versions of PHP available. From the list, select the version that you would like to use as the default.

Or specify the version directly:

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

Switch between multiple PHP versions in Ubuntu Linux

The settings for the different versions of PHP are located in different files. For example, the settings for PHP 8.2:

  • CLI: /etc/php/8.2/cli/php.ini
  • Apache: /etc/php/8.2/apache2/php.ini
  • FPM: /etc/php/8.2/fpm/php.ini
Leave a Reply

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