Skip to Content

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

How to install multiple PHP versions in Ubuntu?

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

[email protected] ~]# apt update ; apt install software-properties-common -y ; add-apt-repository ppa:ondrej/php

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

[email protected] ~]# apt install php5.6 -y

[email protected] ~]# apt install php7.0 -y

[email protected] ~]# apt install php7.4 -y

[email protected] ~]# apt install php8.0 -y

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

[email protected] ~]# 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

[email protected] ~]# echo "<?php phpinfo()?>" >> /var/www/html/phpinfo.php

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

[email protected] ~]# dpkg-query -l | grep -i php

[email protected] ~]# 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.

[email protected] ~]# 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

[email protected] ~]# 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

[email protected] ~]# 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.

[email protected]u ~]# 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

[email protected] ~]# echo "<?php phpinfo()?>" >> /var/www/html/phpinfo.php

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

[email protected] ~]# a2dismod php5.6

[email protected] ~]# a2enmod php7.4

[email protected] ~]# systemctl restart apache2

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