Skip to Content

How to install and switch different versions of PHP in Ubuntu?

How to install multiple PHP versions in Ubuntu?

Installing and switching different versions of PHP is important because different applications and websites require different versions of PHP to function properly. For example, an older website may require an older version of PHP to work, while a newer application may require a newer version of PHP.

In addition, some developers may prefer to work with a specific version of PHP for their projects. Being able to switch between different versions of PHP allows developers to work with the version that is best suited for their project.


1. Run the below command to install PPA and enable the PHP repository.

root@ubuntu ~]# apt update ; apt install software-properties-common -y ; add-apt-repository ppa:ondrej/php

2. Install different versions of PHP by the below commands.

root@ubuntu ~]# apt install php5.6 -y

root@ubuntu ~]# apt install php7.0 -y

root@ubuntu ~]# apt install php7.4 -y

root@ubuntu ~]# apt install php8.0 -y

3. You can check the PHP version from CLI using the below command.

root@ubuntu ~]# php -v

PHP 8.0.18 (cli) (built: Apr 21 2022 10:14:55) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.18, Copyright (c) Zend Technologies
with Zend OPcache v8.0.18, Copyright (c), by Zend Technologies

4. You can check the PHP version for GUI by adding phpinfo.php and accessing with <server-ip>/phpinfo.php

root@ubuntu ~]# echo "<?php phpinfo()?>" >> /var/www/html/phpinfo.php

5. Check all PHP versions installed on ubuntu by running the below commands.

root@ubuntu ~]# dpkg-query -l | grep -i php

root@ubuntu ~]# apt list --installed | grep -i php

How to switch PHP version for CLI?

Run the below command and select the PHP version you want to switch to. In the below example I'm switching the PHP version to 7.4.

root@ubuntu ~]# update-alternatives --config php

There are 4 choices for the alternative php (providing /usr/bin/php).

  Selection  Path                     Priority      Status
------------------------------------------------------------
* 0          /usr/bin/php8.0          80            auto mode
  1          /usr/bin/php5.6          56            manual mode
  2          /usr/bin/php7.0          70            manual mode
  3          /usr/bin/php7.4          74            manual mode
  4          /usr/bin/php8.0          80            manual mode

Press <enter> to keep the current choice[*], or type selection number: 3
update-alternatives: using /usr/bin/php7.4 to provide /usr/bin/php (php) in manual mode

root@ubuntu ~]# update-alternatives --config phar

There are 4 choices for the alternative phar (providing /usr/bin/phar).

  Selection  Path                     Priority      Status
------------------------------------------------------------
* 0          /usr/bin/phar8.0        80            auto mode
  1          /usr/bin/phar5.6        56            manual mode
  2          /usr/bin/phar7.0        70            manual mode
  3          /usr/bin/phar7.4        74            manual mode
  4          /usr/bin/phar8.0        80            manual mode

Press <enter> to keep the current choice[*], or type selection number: 3
update-alternatives: using /usr/bin/phar7.4 to provide /usr/bin/phar (phar) in manual mode

root@ubuntu ~]# update-alternatives --config phar.phar

There are 4 choices for the alternative phar.phar (providing /usr/bin/phar.phar).

  Selection  Path                     Priority      Status
------------------------------------------------------------
* 0          /usr/bin/phar.phar8.0    80            auto mode
  1          /usr/bin/phar.phar5.6    56            manual mode
  2          /usr/bin/phar.phar7.0    70            manual mode
  3          /usr/bin/phar.phar7.4    74            manual mode
  4          /usr/bin/phar.phar8.0    80            manual mode

Press <enter> to keep the current choice[*], or type selection number: 3
update-alternatives: using /usr/bin/phar.phar7.4 to provide /usr/bin/phar.phar (phar.phar) in manual mode

After completing above three steps, check php version by running the below command.

root@ubuntu ~]# php -v

PHP 7.4.29 (cli) (built: Apr 21 2022 10:16:36) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
with Zend OPcache v7.4.29, Copyright (c), by Zend Technologies

How to switch PHP version for GUI?

1. View current version loaded in GUI by viewing the PHP configuration file. Run below command and view the configuration in browser with <server_ip>/phpinfo.php

root@ubuntu ~]# echo "<?php phpinfo()?>" >> /var/www/html/phpinfo.php

2. Run the below commands to disable and enable multi PHP versions.

root@ubuntu ~]# a2dismod php5.6

root@ubuntu ~]# a2enmod php7.4

root@ubuntu ~]# systemctl restart apache2

3. Now refresh GUI and confirm the PHP version has changed.

 

FAQ

1. Can we install multiple PHP versions?

        Yes, it is possible to install multiple versions of PHP on the same Ubuntu system. This allows you to switch between different versions of PHP depending on the requirements of your projects or applications.

2. How to install multiple PHP versions Ubuntu 20?

   There are different ways to install multiple PHP versions, but one common method is to use a package manager like apt-get to install each version of PHP as a separate package. Refer the article How to install multiple PHP versions in Ubuntu?

3. How to switch between multiple PHP versions In Ubuntu?

   Refer the article How to install multiple PHP versions in Ubuntu?