Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch php versions on commandline ubuntu 16.04

People also ask

Can I install 2 PHP versions Ubuntu?

By default, Ubuntu 20.04 ships with the PHP version 7.4. So you'll need to add the PHP repository in your system to install the multiple PHP versions. Once the repository is up-to-date, you can proceed to install multiple PHP versions.

How do I find my default PHP version Ubuntu?

Open a bash shell terminal and use the command “php –version” or “php -v” to get the version of PHP installed on the system. As you can see from both the command output above, the system has PHP 5.4.


Interactive switching mode

sudo update-alternatives --config php
sudo update-alternatives --config phar
sudo update-alternatives --config phar.phar

Manual Switching

From PHP 5.6 => PHP 7.1

Default PHP 5.6 is set on your system and you need to switch to PHP 7.1.

Apache:

$ sudo a2dismod php5.6
$ sudo a2enmod php7.1
$ sudo service apache2 restart

Command Line:

$ sudo update-alternatives --set php /usr/bin/php7.1
$ sudo update-alternatives --set phar /usr/bin/phar7.1
$ sudo update-alternatives --set phar.phar /usr/bin/phar.phar7.1

From PHP 7.1 => PHP 5.6

Default PHP 7.1 is set on your system and you need to switch to PHP 5.6.

Apache:

$ sudo a2dismod php7.1
$ sudo a2enmod php5.6
$ sudo service apache2 restart

Command Line:

$ sudo update-alternatives --set php /usr/bin/php5.6

Source


$ sudo update-alternatives --config php

should work for all ubuntu versions after 16.04 (18.04 and 20.04)

This is what you should see as a response

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

  Selection    Path             Priority   Status
------------------------------------------------------------
* 0            /usr/bin/php7.2   72        auto mode
  1            /usr/bin/php5.6   56        manual mode
  2            /usr/bin/php7.0   70        manual mode
  3            /usr/bin/php7.1   71        manual mode
  4            /usr/bin/php7.2   72        manual mode
Press <enter> to keep the current choice[*], or type selection number:

Choose the appropriate version


To list all available versions and choose from them :

sudo update-alternatives --config php

Or do manually

sudo a2dismod php7.1 // disable
sudo a2enmod php5.6  // enable

I think you should try this

From php5.6 to php7.1

sudo a2dismod php5.6
sudo a2enmod php7.1
sudo service apache2 restart

sudo update-alternatives --set php /usr/bin/php7.1
sudo update-alternatives --set phar /usr/bin/phar7.1
sudo update-alternatives --set phar.phar /usr/bin/phar.phar7.1

From php7.1 to php5.6

sudo a2dismod php7.1
sudo a2enmod php5.6
sudo service apache2 restart

sudo update-alternatives --set php /usr/bin/php5.6
sudo update-alternatives --set phar /usr/bin/phar5.6
sudo update-alternatives --set phar.phar /usr/bin/phar.phar5.6

I actually wouldn't recommend using a2enmod for php 5 or 7. I would use update-alternatives. You can do sudo update-alternatives --config php to set which system wide version of PHP you want to use. This makes your command line and apache versions work the same. You can read more about update-alternatives on the man page.


in ubuntu 20.04 switching between PHP 8.0 and PHP 7.4 version:

DOWNGRADE PHP 8.0 to PHP 7.4

sudo a2dismod php8.0
sudo a2enmod php7.4
sudo service apache2 restart
sudo update-alternatives --config php
sudo update-alternatives --config phar
sudo update-alternatives --config phar.phar
sudo service apache2 restart

UPGRADE PHP 7.4 to PHP 8.0

sudo a2dismod php7.4
sudo a2enmod php8.0
sudo service apache2 restart
sudo update-alternatives --config php
sudo update-alternatives --config phar
sudo update-alternatives --config phar.phar
sudo service apache2 restart

check the changes:

  1. run php -v in the console and you will got:

PHP 8.0.3 (cli) (built: Mar 5 2021 07:54:13) ( NTS ) Copyright (c) The PHP Group Zend Engine v4.0.3, Copyright (c) Zend Technologies with Zend OPcache v8.0.3, Copyright (c), by Zend Technologies

OR

PHP 7.4.16 (cli) (built: Mar 5 2021 07:54:38) ( NTS ) Copyright (c) The PHP Group Zend Engine v3.4.0, Copyright (c) Zend Technologies with Zend OPcache v7.4.16, Copyright (c), by Zend Technologies

  1. add a PHP file in your runnable local environment like /var/www/html/ path by adding phpinfo(); and get PHP info in the browser (in top of the page the version of PHP is available to see)

You can create a script to switch from versions: sudo nano switch_php then type this:

#!/bin/sh
#!/bin/bash
echo "Switching to PHP$1..."
case $1 in
    "7")
        sudo a2dismod php5.6
        sudo a2enmod php7.0
        sudo service apache2 restart
        sudo ln -sfn /usr/bin/php7.0 /etc/alternatives/php;;
    "5.6")
        sudo a2dismod php7.0
        sudo a2enmod php5.6
        sudo service apache2 restart
        sudo ln -sfn /usr/bin/php5.6 /etc/alternatives/php;;
esac
echo "Current version: $( php -v | head -n 1 | cut -c-7 )"

exit and save make it executable: sudo chmod +x switch_php

To execute the script just type ./switch_php [VERSION_NUMBER] where the parameter is 7 or 5.6

That's it you can now easily switch form PHP7 to PHP 5.6!