How to Install Multiple PHP versions in Ubuntu?

At times every developer needs to work on multiple projects which are working on different PHP versions. So It is very difficult to work with a different version installed in your system if the code is written for a different version of PHP. It will be very convenient if we can install multiple versions of PHP in our local system and change them as we need.

Currently, we have three major supported versions of PHP, i.e PHP 5.*, 7.*, and 8.*. So here are the steps you can use to install multiple versions of PHP.

Check the current version of PHP

sudo apt show php
OR
sudo apt show php -a

Add ppa-repository

To install multiple PHP versions you need to first install the ondrej/php by using the below-mentioned steps.

$ sudo apt install python-software-properties
$ sudo add-apt-repository ppa:ondrej/php

Update the system

$ sudo apt-get update

Install the different PHP versions for Apache

$ sudo apt install php5.6
$ sudo apt install php7.4
$ sudo apt install php8.0 

You can change the PHP version to php7.1 or php7.2 or any other available version as per your requirement.

Install the different PHP versions for Nginx

$ sudo apt install php5.6-fpm
$ sudo apt install php7.4-fpm
$ sudo apt install php8.0-fpm

Change the PHP version to php7.1-fpm or php7.2-fpm or any other as per your requirement.

And it’s all done. You can install other PHP modules as per your requirement by specifying the correct version with the module. Below are some examples

$ sudo apt install php7.3-cli
$ sudo apt install php5.6-cli
$ sudo apt install php7.4-xml

Finally check the default version of PHP by using the below-mentioned command

$ php -v 

Change PHP version

Now we have installed the required version of PHP in our system. We can use the below-mentioned commands to change the working version of the PHP.

Disable the current version of PHP

$ sudo a2dismod php7.4

Enable the required PHP versions

$ sudo a2enmod php8.0

A common confusion created by the default version of PHP

Normally when we check the current PHP version using the above-mentioned command. It can show you a different version from what you will get from phpinfo() function. The current working version will be what you are getting from phpinfo() function.

To fix this different versions issue you need to set the same PHP version as the default version after changing the working PHP version. You can set the default PHP version by using the below mention command.

change the version as per your requirement.

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

Conclusion

We can set up multiple PHP versions in our system using the ppa repository. We can change enable and disable the active version of the PHP from the terminal. Sometimes we got the different versions from terminal and phpinfo(). To fix that we need to set up the default version from the terminal.

Thank you for reading. Hopefully, you have learned something new with the post. Please share your thoughts in the comments or send us an email at support@expandabletechnologies.com

You may also like...