How to Install Multiple Versions of PHP on Ubuntu?

There are many tools like Docker, Vagrant, VirtualBox, etc. that gives you the power to achieve the multiple versions of PHP in your system. But I want to stay away as much as possible from third-party tools and try to leverage the native as much as I can.

So want to learn how to install multiple versions of PHP on the Ubuntu system? Okay, that’s great.

Let’s assume, you have

  • OS: Ubuntu 16.04
  • PHP: 7.1 (PHP-FPM)
  • Nginx: 1.10

And want to install PHP 7.2.

Install PHP 7.2

Run the following commands to update the PHP repository & the Ubuntu system

sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt update

Now you are able to install the new version of PHP

sudo apt install -y php7.2-fpm
sudo apt install -y php7.2-dev
php -v

If php -v still pointing to the old PHP version, then run the following command

sudo update-alternatives --set php /usr/bin/php7.2

Install PHP 7.2 Modules

You may need some additional PHP modules in order for PHP to work with your applications.
You can install the most commonly needed modules especially required for Magento 2 with:

sudo apt install php7.2-mysql php7.2-bcmath php7.2-curl php7.2-xml php7.2-gd php7.2-intl php7.2-mcrypt php7.2-soap php7.2-zip

To check installed PHP modules:

php -m | grep -E 'bcmath|ctype|curl|dom|gd|hash|iconv|intl|json|mbstring|mcrypt|openssl|pdo|soap|spl|xml|xsl|zip'

To search the PHP module:

sudo apt-cache search php7.2

Now, you have a new PHP 7.2 version and required modules installed. Similarly, you can install PHP 7.3 and it’s related modules.

Now a big question is: How to use a specific PHP version as per application?

Before that, we should know how PHP is used.
PHP can be used as a CLI or web server module (mod_php, CGI, FPM).

Switching PHP CLI Version

PHP’s php command is linked to /usr/bin/php, which is symlinked to /etc/alternatives/php, which again is symlinked to actual PHP, example /usr/bin/php7.2.
So here lies the logic of switching PHP CLI versions.

By default, the PHP installation changes the CLI version to the new one.
In case if you want to switch to a specific version, say PHP 7.2, you can use the update-alternatives command

sudo update-alternatives --set php /usr/bin/php7.2
php -v

You can find the list of PHP executables

ll /usr/bin | grep php

You might need to run update-alternatives to set the required PHP version if you are running composer commands or any CLI PHP script.

Switching PHP Web Server Version

As you probably know, Nginx runs PHP code via PHP-FPM, which listens on a Unix socket.
And that makes it easier to switch the PHP version in the Nginx’s server block.

If you want to point any application, say Magento 2 running on Nginx with PHP 7.2, you can simply change the value of fastcgi_pass directive as:

fastcgi_pass unix:/run/php/php7.2-fpm.sock

To find the list of Unix sockets for PHP-FPM

ll /run/php | grep sock

In order to verify the PHP version for Web SAPI, you can use phpinfo() in your .php file.

Conclusion

Some people might want to use Docker, Vagrant based solution for this. It’s Okay.

For me, I always prefer a native system without depending on third-party solutions. This minimizes the overhead and latency problem and hence makes the application development easier and faster.

Above you read, how easily you can change the PHP CLI and Web SAPI version. For CLI based you might have to run the update-alternatives command on demand. But for Web application, once the required PHP version is set, they will work independently on specific PHP versions.

Please comment below if you have any better solution for handling multiple PHP versions.