148

I have installed php 5.6 and and php 7.1 on my Ubuntu 16.04

I know with Apache as my web server, I can do

a2enmod php5.6 #to enable php5
a2enmod php7.1 #to enable php7

When I disable php7.1 in Apache modules and enable php 5.6, Apache recognizes the change and uses php 5.6 interpreter as expected.

But when I run internal php web server from the commandline:

php -S localhost:8888

php handles requests using php 7. So how do I switch between php 5.6 and php 7.1 in the command line ?

Akshay
  • 147
  • 13
salimsaid
  • 1,886
  • 2
  • 11
  • 17

18 Answers18

323

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

PiTheNumber
  • 20,216
  • 13
  • 96
  • 165
Stevie G
  • 3,528
  • 1
  • 8
  • 15
  • 3
    sudo update-alternatives --set php /usr/bin/php5.6 this commands saved my time.. thanks @StevieG – Ask Bytes Apr 29 '18 at 10:25
  • You're the best bro... Thanks a lot. – Fendi Septiawan Jun 12 '18 at 08:23
  • I had segmentation fault on apache2 restart. I then found that I had more than one PHP version enabled at a time. I had to disable both 7.1 and 7.2 before enabling 5.6. – donquixote Jul 24 '18 at 15:19
  • Indeed, I just have used this solution to change `php -v` from 7.2 to 7.1! It is great and simple. – SaidbakR Nov 27 '18 at 18:19
  • Great piece of advice is here also - https://askubuntu.com/questions/761713/how-can-i-downgrade-from-php-7-to-php-5-6-on-ubuntu-16-04 . By the way, you can install several php modules - 5.6, 7.0, 7.1, 7.2, for example, and then juggle them, if you get certain errors for a given version of php (for example, `create_function` is no longer supported starting from php 7.2.) – Vadim Anisimov Feb 01 '19 at 16:26
  • In Ubuntu 18.04 I had to do some extra steps to disable other versions or processes will continue to run. # systemctl stop php5.6-fpm | # systemctl disable php5.6-fpm | to get the list of php processes do # ps -ax | grep php – Braconnot_P Feb 27 '20 at 15:08
  • sudo update-alternatives --set php /usr/bin/php7.4 worked for me (for switching from 7.0 to 7.4). Thanks – Ahmad Aug 17 '20 at 06:58
163

type this in your command line, should work for all ubuntu between 16.04, 18.04 and 20.04.

$ sudo update-alternatives --config php

and this is what you will get

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

GiorgosK
  • 4,369
  • 2
  • 25
  • 21
20

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
Kamal Kumar
  • 2,495
  • 1
  • 16
  • 15
15

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.

FatBoyXPC
  • 733
  • 4
  • 14
15

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
Rupinder Sohal
  • 151
  • 1
  • 3
  • 1
    Give enough explanation for your answers to understand why this code works and what it means.So that the answer will be useful for the users having the same issue. See the link to know how to write good answers. – Jino Shaji Sep 19 '17 at 09:47
10

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!

chepe263
  • 2,566
  • 19
  • 37
ihakoz
  • 101
  • 1
  • 3
  • Looks promising but what does 'n' flag does in ln command as you use "ln -sfn ..." . I have not been able to find that flag ("n") anywhere in the documentation. – Fakhar Anwar Jul 15 '20 at 12:05
  • @FakharAnwar `man ln`: `-n, --no-dereference : treat LINK_NAME as a normal file if it is a symbolic link to a directory` – Flame Jan 10 '21 at 12:56
8

You can use below command lines to switch between two PHP version.

E.g.

I want to switch PHP Version from 7.1 to 7.2 we can use below command

sudo a2dismod php7.1 &&  sudo update-alternatives --set php /usr/bin/php7.2 && sudo a2enmod php7.2 && sudo service apache2 restart

a2dismod is use to disable the current php version and a2enmod is use to enable the version

Ashish Viradiya
  • 369
  • 6
  • 15
8

May be you might have an old PHP version like PHP 5.6 in your system and you installed PHP 7.2 too so thats multiple PHP in your machine. There are some applications which were developed when older PHP 5.6 was latest version, they are still live and you working on those applications, You might be working on Laravel simultaneously but Laravel requires PHP 7+ to get started. Getting the picture ?

In that case you can switch between the PHP versions to suit your requirements.

Switch From PHP 5.6 => PHP 7.2

Apache:-

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

Command Line:-

sudo update-alternatives --set php /usr/bin/php7.2
sudo update-alternatives --set phar /usr/bin/phar7.2
sudo update-alternatives --set phar.phar /usr/bin/phar.phar7.2
sudo update-alternatives --set phpize /usr/bin/phpize7.2
sudo update-alternatives --set php-config /usr/bin/php-config7.2

And vice-versa, Switch From PHP 7.2 => PHP 5.6

Apache:-

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

Command Line:-

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
sudo update-alternatives --set phpize /usr/bin/phpize5.6
sudo update-alternatives --set php-config /usr/bin/php-config5.6
Martijn Pieters
  • 889,049
  • 245
  • 3,507
  • 2,997
Dinesh Suthar
  • 565
  • 5
  • 26
3

Type given command in your terminal..

For disable the selected PHP version...

    • sudo a2dismod php5
    • sudo service apache2 restart
  1. For enable other PHP version....

    • sudo a2enmod php5.6
    • sudo service apache2 restart

It will upgrade Php version, same thing reverse if you want version downgrade, you can see it by PHP_INFO();

3

Switch from PHP 5.6 to PHP 7.2 using:

sudo a2dismod php5.6 && sudo a2enmod php7.2 && sudo service apache2 restart

Switch from PHP 7.2 to PHP 5.6 using:

sudo a2dismod php7.2 && sudo a2enmod php5.6 && sudo service apache2 restart
Omar Einea
  • 2,410
  • 6
  • 21
  • 34
2

You could use these open source PHP Switch Scripts, which were designed specifically for use in Ubuntu 16.04 LTS.

https://github.com/rapidwebltd/php-switch-scripts

There is a setup.sh script which installs all required dependencies for PHP 5.6, 7.0, 7.1 & 7.2. Once this is complete, you can just run one of the following switch scripts to change the PHP CLI and Apache 2 module version.

./switch-to-php-5.6.sh
./switch-to-php-7.0.sh
./switch-to-php-7.1.sh
./switch-to-php-7.2.sh
DivineOmega
  • 349
  • 1
  • 4
  • 11
2

I made a bash script to switch between different PHP versions on Ubuntu.

Hope it helps someone.

Here's the script: (save it in /usr/local/bin/sphp.sh, don't forget to add +x flag with command: sudo chmod +x /usr/local/bin/sphp.sh)

#!/bin/bash

# Usage
if [ $# -ne 1 ]; then
  echo "Usage: sphp [phpversion]"
  echo "Example: sphp 7.2"
  exit 1
fi

currentversion="`php -r \"error_reporting(0); echo str_replace('.', '', substr(phpversion(), 0, 3));\"`"
newversion="$1"

majorOld=${currentversion:0:1}
minorOld=${currentversion:1:1}
majorNew=${newversion:0:1}
minorNew=${newversion:2:1}

if [ $? -eq 0 ]; then
  if [ "${newversion}" == "${currentversion}" ]; then
    echo "PHP version [${newversion}] is already being used"
    exit 1
  fi

  echo "PHP version [$newversion] found"
  echo "Switching from [php${currentversion}] to [php${newversion}] ... "

  printf "a2dismod php$majorOld.$minorOld ... "
  sudo a2dismod "php${majorOld}.${minorOld}"
  printf "[OK] and "

  printf "a2enmod php${newversion} ... "
  sudo a2enmod "php${majorNew}.${minorNew}"
  printf "[OK]\n"

  printf "update-alternatives ... "
  sudo update-alternatives --set php "/usr/bin/php${majorNew}.${minorNew}"
  printf "[OK]\n"

  sudo service apache2 restart
  printf "[OK] apache2 restarted\n"
else
  echo "PHP version $majorNew.$minorNew was not found."
  echo "Try \`sudo apt install php@${newversion}\` first."
  exit 1
fi

echo "DONE!"
slawkens
  • 99
  • 6
2

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)
Pejman Kheyri
  • 1,389
  • 6
  • 13
  • 22
1

From PHP 5.6 => PHP 7.1

$ sudo a2dismod php5.6
$ sudo a2enmod php7.1

for old linux versions

 $ sudo service apache2 restart

for more recent version

$ systemctl restart apache2
1

please follow the steps :

i.e : your current version is : current_version = 7.3 , and you want to change it to : new_version = 7.2

1) sudo a2dismod php(current_version) 
2) sudo a2enmod php(new_version)
3) sudo update-alternatives --config php (here you need to select php version number) 
4) restart apache through : 
  sudo /etc/init.d/apache2 restart OR
  sudo service apache2 restart
Yash
  • 51
  • 4
1

this worked for me:-

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

just change the PHP version to whichever version you need I have changed it to php7.4

Aahad
  • 132
  • 1
  • 8
0

You can use the below script to switch between PHP version easily I have included phpize configuration too.

https://github.com/anilkumararumulla/switch-php-version

Download the script file and run

sh switch.sh
Anil Kumar
  • 626
  • 5
  • 7
0

When installing laravel on Ubuntu 18.04, be default PHP 7.3.0RC3 install selected, but laravel and symfony will not install properly complaining about missin php-xml and php-zip, even though they are installed. You need to switch to php 7.1, using the instructions above or,

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

now, running laravel new blog, will proceed correctly

pingle60
  • 500
  • 5
  • 9