Jubayed's Blog

Ruby on Rails learner

Switch Between Multiple PHP Versions

| Comments

To check the default installed version of PHP, run:

1
 $ php -v
1
2
$ sudo add-apt-repository -y ppa:ondrej/php
$ sudo apt-get  update
1
$ sudo apt-get  install php5.6  php7.0  php7.1  php7.2

Switch from PHP7.x to PHP5.x

First disable PHP7.2 module using command:

1
sudo a2dismod php7.2

Module php7.2 disabled. To activate the new configuration, you need to run: systemctl restart apache2

Next, enable PHP5.6 module:

1
$ sudo a2enmod php5.6

Set PHP5.6 as default version:

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

Alternatively, you can run the following command to set which system wide version of PHP you want to use by default.

1
$ sudo update-alternatives --config php

Enter the selection number to set it as default version or simply press ENTER to keep the current choice.

In case, you have installed other PHP extensions, set them as default as well.

1
$ sudo update-alternatives --set phar /usr/bin/phar5.6

Finally, restart your Apache web server:

1
$ sudo systemctl restart apache2

Now, check if PHP5.6 is the default version or not:

1
$  php -v

shell script for change php version add this line on your .bashrc file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
function set-php {
 if [ ! $2 ] ; then
    if [[ "$1" == "5.6" ]]; then
      print -P '%B%F{red}Change you PHP -v $1 %f%b\n'
      sudo update-alternatives --set php /usr/bin/php5.6
      sudo a2dismod php7.0
      sudo a2dismod php7.1
      sudo a2dismod php7.2
      sudo a2enmod php5.6
      sudo systemctl restart apache2
      print -P '%B%F{red}successfully completed %f%b\n'
    elif [[ "$1" == "7.0" ]]; then
      print -P '%B%F{red}Change you PHP -v $1 %f%b\n'
      sudo update-alternatives --set php /usr/bin/php7.0
      sudo a2dismod php5.6
      sudo a2dismod php7.1
      sudo a2dismod php7.2
      sudo a2enmod php7.0
      sudo systemctl restart apache2
      print -P '%B%F{red}successfully completed %f%b\n'
    elif [[ "$1" == "7.1" ]]; then
      print -P '%B%F{red}Change you PHP -v $1 %f%b\n'
      sudo update-alternatives --set php /usr/bin/php7.1
      sudo a2dismod php5.6
      sudo a2dismod php7.0
      sudo a2dismod php7.2
      sudo a2enmod php7.1
      sudo systemctl restart apache2
      print -P '%B%F{red}successfully completed %f%b\n'
    elif [[ "$1" == "7.2" ]]; then
      print -P '%B%F{red}Change you PHP -version to php$1 %f%b\n'
      sudo update-alternatives --set php /usr/bin/php7.2
      sudo a2dismod php5.6
      sudo a2dismod php7.0
      sudo a2dismod php7.1
      sudo a2enmod php7.2
      sudo systemctl restart apache2
      print -P '%B%F{red}successfully completed %f%b\n'
    else
      print "This php$1 version not installed."
    fi
    else
    print "Formate error!!"
  fi
}

to run

1
 php-set 7.1

Comments