DEV Community

Cover image for How to Install Multiple Versions of PHP on Ubuntu?
Raj KB
Raj KB

Posted on • Updated on • Originally published at blog.magepsycho.com

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

sudo update-alternatives --set php /usr/bin/php7.2
Enter fullscreen mode Exit fullscreen mode

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-mbstring php7.2-soap php7.2-zip
Enter fullscreen mode Exit fullscreen mode

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'
Enter fullscreen mode Exit fullscreen mode

To search the PHP module:

sudo apt-cache search php7.2
Enter fullscreen mode Exit fullscreen mode

Now, you have a new PHP 7.2 version and required modules installed. Similarly, you can install PHP 7.3, 7.4, 8.0 & so on 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
Enter fullscreen mode Exit fullscreen mode

You can find the list of PHP executables with

ll /usr/bin | grep php
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

To find the list of Unix sockets for PHP-FPM

ll /run/php | grep sock
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (2)

Collapse
 
vishalraj82 profile image
Vishal Raj

@magepsycho I would definitely use docker or vagarant. But another alternative to have multiple version of PHP can be to compile them locally and put them in seprate directories. Haven't tried that, but I hope the system does not runs into any kind of conflict.

Collapse
 
magepsycho profile image
Raj KB

No doubt Docker is the current de-facto for development. But if just your PHP version varies for your PHP projects then this approach is quite handy (feel right at home)