How to Install Multiple Versions of PHP on Ubuntu 22.04, 20.04, 18.04

This tutorial shows you how to install multiple versions of PHP on Ubuntu and make a particular Apache virtual host or Nginx server block use one of them. This is useful when you have multiple web applications on your server but one or two of them isn’t compatible with the PHP version in the default Ubuntu repository. Ubuntu 20.04 repository includes PHP7.4 and Ubuntu 22.04 repository includes PHP8.1. We will see how to install PHP7.4, PHP8.0, and PHP8.1 on a single Ubuntu server.

Install Multiple Versions of PHP on Ubuntu via PPA

The easiest way to install multiple versions of PHP is by using the PPA from Ondřej Surý, who is a Debian developer. To add this PPA, run the following commands in the terminal. The software-properties-common package is needed if you want to install software from PPA. It’s installed automatically on Ubuntu desktop, but might be missing on your Ubuntu server.

sudo apt install software-properties-common

sudo add-apt-repository ppa:ondrej/php

sudo apt update

Now you can install PHP8.0 on Ubuntu by executing the following command.

sudo apt install php8.0 php8.0-fpm

And install some common PHP8.0 extensions.

sudo apt install php8.0-mysql php8.0-mbstring php8.0-xml php8.0-gd php8.0-curl

You can view all available PHP8.0 extensions by typing in sudo apt install php8.0 and pressing Tab key twice.

ubuntu-install-multiple-php-versions

To install PHP7.4 on Ubuntu , run

sudo apt install php7.4 php7.4-fpm

Install some common PHP7.4 extensions.

sudo apt install php7.4-mysql php7.4-mbstring php7.4-xml php7.4-gd php7.4-curl

You can install PHP8.1 in the same way. Simply replace the version number in these commands.

Hint: PHP5.6, PHP7.0 and PHP7.2 reached end-of-life, but you can still get security fixes from this PPA.

Switching PHP Version in Apache Virtual Host

By default, Apache uses one PHP version across all virtual hosts. If you want to use a different PHP version in a particular virtual host, you will need to disable Apache PHP module and run PHP code via PHP-FPM. Check if mod_php is installed.

dpkg -l | grep libapache2-mod-php

If it’s installed, you need to disable it. For example, I have libapache2-mod-php7.4 installed on my Ubuntu 20.04 server,

multiple-php-versions-apache-ubuntu-server-20.04

I disable it by running:

sudo a2dismod php7.4

You also need to disable the prefork MPM module.

sudo a2dismod mpm_prefork

Now you need to run the following command to enable three modules in order to use PHP-FPM, regardless of whether mod_php is installed on your server.

sudo a2enmod mpm_event proxy_fcgi setenvif

The PHP-FPM configuration snippet is located at /etc/apache2/conf-available/.

run-multiple-php-versions-ubuntu-server-20.04

Let’s say you want to use PHP8.0 in an Apache virtual host, then open the virtual host configuration file and add the following line in the <VirtualHost> tags.

Include /etc/apache2/conf-available/php8.0-fpm.conf

Like this:

switch php version ubuntu 20.04 apache

Save and close the file. Then check syntax.

sudo apachectl -t

Restart Apache for the change to take effect.

sudo systemctl restart apache2

Now let’s say you want to use PHP7.4 in Mautic virtual host because Mautic isn’t compatible with PHP8.0, then add the following line in your Mautic virtual host between <VirtualHost> tags and then restart Apache.

Include /etc/apache2/conf-available/php7.4-fpm.conf

To test which PHP version is used by your virtual host, you can create a info.php file in your document root (let’s say /var/www/html).

sudo nano /var/www/html/info.php

Paste the following PHP code into the file.

<?php phpinfo(); ?>

Save and close the file. Then in your browser address bar, type in

your-domain.com/info.php

ubuntu 20.04 install php8.0

Switching PHP Version in Nginx Server Block

It’s very easy to switch PHP version in Nginx server block. As you probably know, Nginx runs PHP code via PHP-FPM, which listens on a Unix socket. The socket file is located in /run/php/ directory.

php-fpm change php version ubuntu 20.04

As you can see from the screenshot, there are several PHP-FPM socket files (.sock) on my server corresponding to different PHP versions. Your Nginx server block file typically has the following snippet to connect to PHP-FPM server.

location ~ \.php$ {
    try_files $uri =404;
    fastcgi_pass unix:/run/php/php7.4-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
  }

nginx multiple php versions ubuntu 18.04

The PHP version being used by Nginx is determined by the third line in the snippet. To use PHP8.0 in a particular Nginx server block, change that line to:

fastcgi_pass unix:/run/php/php8.0-fpm.sock;

To use PHP8.1 in Nginx server block, change that line to

fastcgi_pass unix:/run/php/php8.1-fpm.sock;

I’m sure you get the idea now. Save and close the file. Then reload Nginx for the changes to take effect.

sudo systemctl reload nginx

To test which PHP version is used by your server block, you can create a info.php file in your document root (let’s say /var/www/html).

sudo nano /var/www/html/info.php

Paste the following PHP code into the file.

<?php phpinfo(); ?>

Save and close the file. Then in your browser address bar, type in

your-domain.com/info.php

ubuntu 20.04 install php8.0

How To Set the Default PHP Version for the Command Line

If you install a newer version of PHP on Ubuntu, then it will become the default version for the command line. I installed PHP8.0, but I still want PHP7.4 as the default version for the command line.

To set the default command line PHP version, run the following command.

sudo update-alternatives --config php

set the default command line PHP version ubuntu

To check the current version of command-line PHP, run

php --version

php --version ubuntu

Troubleshooting PHP Errors

Unable to load dynamic library

If you encounter the following error,

PHP Warning:  PHP Startup: Unable to load dynamic library 'pdo_mysql.so' (tried: /usr/lib/php/20190902/pdo_mysql.so (/usr/lib/php/20190902/pdo_mysql.so: undefined symbol: mysqlnd_allocator), /usr/lib/php/20190902/pdo_mysql.so.so (/usr/lib/php/20190902/pdo_mysql.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0

Then you need to install PHP MySQL,

sudo apt install php8.0-mysql

Enable the PHP MySQL extensions.

sudo phpenmod mysqlnd mysqli

Wrapping Up

That’s it! I hope this tutorial helped you run multiple PHP versions on Ubuntu 18.04, 20.04, 22.04 with Apache and Nginx. You may also want to set up the ModSecurity web application firewall to protect your PHP web applications from hacking. If you use Apache web server on Debian/Ubuntu, then read the following tutorial.

If you use Nginx web server on Debian/Ubuntu, then read the following tutorial:

Want to develop PHP web applications on Ubuntu Desktop? You can use PhpStorm.

As always, if you found this post useful, then subscribe to our free newsletter to get more useful tutorials 🙂

Rate this tutorial
[Total: 16 Average: 4.9]

22 Responses to “How to Install Multiple Versions of PHP on Ubuntu 22.04, 20.04, 18.04

  • Stephen
    1 month ago

    Thanks, this works fine. I’m a novice but got it working simple enough. Cheers.

  • Would the process be the same for 18.04?

  • jajcek2006
    4 years ago

    Works great in Ubuntu 18.04.

  • Vladimir
    3 years ago

    Thanks for the tutorial. I setup a webpage with 8.0.3 PHP and owncloud with 7.2 PHP. The onlyproblem I have when I access my owncloud with HTTPS it gets an error as over HTTPS it is using the 8.0.3 PHP. Anyway to get around that ?

  • I tried it with fresh linux 20.04 on WSL2. I try change php versions, restart apache2, but php -v always shows php8 version. And php do not work

    • Xiao Guoan (Admin)
      2 years ago

      Unless noted in the article, all articles on this website are meant for standard Linux distributions. WSL are not covered.

  • Works great… turns out I need 5.4 and this doesn’t have that package.

    Anyway, your tutorials are always so very helpful and accurate, thank you.

    Mike

  • That’s strange, when I do a copy of your code line in the black window, the paste gives :
    Include/etc/apache2/conf-avatlable/php8.0-fpm.conf

    One space is missing after Include, and available is changed by avatlable

    How can you explain that ?

    • Xiao Guoan (Admin)
      2 years ago

      It’s likely has something to do with the keyboard layout and locale on your computer. But I’m not sure how to fix it as I don’t use a French keyboard.

  • hi Xiao Guoan,

    First of all, amazing post.
    Im trying to config multiple php version on my ubuntu 20.04 server too.
    I have followed all of your steps, but when I try to access my website I got the error Service Unavailable
    The server is temporarily unable to service your request due to maintenance downtime or capacity problems. Please try again later.
    Can you please help me.

  • Duffman
    2 years ago

    Thank you LinuxBabe!

  • I think there’s a typo in this sentence: “You can view all available PHP7.4 extensions by typing in sudo apt install php8.0 and pressing Tab key twice.” “PHP7.4” should be “PHP8.0”, I think.

    • Xiao Guoan (Admin)
      2 years ago

      You are right. I just corrected the typo. Thanks.

  • Lillie Enderby
    2 years ago

    Fantastic goodѕ from you, man. I haѵe understand your stuff pгevious to
    and yoս’гe just extremely ցreat. I actually
    like what you have acquired here, really like what үou’re ѕaying ɑnd the way in ԝhich yyou
    ѕay it. You maкe it entertaining annd yyou
    stiⅼl take care ⲟf tօ kеep it wise. I cаn’t wait tо readd far morе from you.

    Tһіѕ is actually a wonderful site.

  • cortezthyer
    2 years ago

    thank you a good deal this fabulous website
    can be official and also everyday

  • jolenenewton
    2 years ago

    Everyone loves it when individuals get together and share thoughts.
    Great blog, stick with it!

  • lamontcone
    2 years ago

    Nice post. I learn something new and challenging on sites I stumbleupon on a daily basis.
    It’s always helpful to read content from other writers and use something from other sites.

  • I like the helpful info you supply to your articles. I’ll
    bookmark your blog and test again right here frequently.
    I’m fairly certain I will be told many new stuff proper here!
    Best of luck for the next!

  • Michael Milette
    5 months ago

    Hi Xiao,

    Great article. Very helpful. I have some old websites that are not compatible with PHP 8.x and this is just what I needed.

    Thank you!

    Michael Milette

  • Hi, this worked for me in Ubuntu 22.04, but I had to run one more command at the end to make PHP evaluate with Apache:
    a2enconf php7.4-fpm

Leave a Comment

  • Comments with links are moderated by admin before published.
  • Your email address will not be published.
  • Use <pre> ... </pre> HTML tag to quote the output from your terminal/console.
  • Please use the community (https://community.linuxbabe.com) for questions unrelated to this article.
  • I don't have time to answer every question. Making a donation would incentivize me to spend more time answering questions.

The maximum upload file size: 2 MB. You can upload: image. Links to YouTube, Facebook, Twitter and other services inserted in the comment text will be automatically embedded. Drop file here