Switch between PHP versions on LINUX:

Scenario:

Let’s say there is a new development which requires a different version of PHP. Assume that we have installed and are working with the PHP 7.0 version. According to new development, We need PHP 7.3 and its libraries. We can install the same using ” apt-get install ” command in UBUNTU. Alternatively you can use “ yum install ” in CENTOS versions.

Following describes the installation and switching procedures for Ubuntu OS.

Tested in Ubuntu Linux Mint

Distributor ID : LinuxMint

Description : Linux Mint 18.2 Sonya

Release : 18.2

Codename : sonya

Install php 7.3

$ sudo apt-get install php7.3 php7.3-bcmath php7.3-bz2 php7.3-cgi php7.3-cli php7.3-common php7.3-curl php7.3-enchant php7.3-fpm php7.3-gd php7.3-gmp php7.3-imap php7.3-intl php7.3-json php7.3-mbstring php7.3-mysql php7.3-odbc php7.3-opcache php7.3-readline php7.3-soap php7.3-tidy php7.3-xml php7.3-xmlrpc php7.3-xsl php7.3-zip -y

Check your PHP-FPM status

$ service php7.3-fpm status

Check your PHP version

$ php -v

PHP 7.0.33-10+ubuntu16.04.1+deb.sury.org+1 (cli) (built: Aug 7 2019 09:50:44) ( NTS )

Post installation of PHP 7.3 version, the PHP version of the system still remains the same as 7.0 version. This is because the system hasn’t pointed the PHP executable to the latest. We need to do it manually as we have now 2 or more versions of PHP. This can be done using following command in two methods

What is this ?

This command is used to connect as a symbolic link between desired versions to default commands.

Command: update-alternatives –command

Many command are present, of which commonly used as below

Arguments:

Depends on the commands, Below are used for commands such as set, config, display, query, list

Name: Name of the original executable, in our case this is php

Path: Path of New version to be linked to the executable(name)

–version

Informs the version on the update-alternatives command

–set name path

Sets the library/executable’s path as an alternative for the existing name.

–config name

Allow users to configure manually from the list of available choices/versions of the name.

Numbered Indexes are used to choose the desired version.

–display name

display complete information about the link name. Mode of the link manual/auto

–list name | –query name

Lists the available alternatives for the same packages/library

List of available alternatives and its details

Other commands are –-remove, –remove-all, –help, –get-selections, –set-selections, –install, –auto.

Refer: http://manpages.ubuntu.com/manpages/trusty/en/man8/update-alternatives.8.html#commands

Let’s Switch using 2 methods:

Method 1: Update the configuration permanently by selecting from available PHP versions.

$ sudo update-alternatives –config php

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

Method 2: Directly change the symlink to desired PHP version

$ sudo update-alternatives –set php /usr/bin/php7.3

update-alternatives: using /usr/bin/php7.3 to provide /usr/bin/php (php) in manual mode

Verify the PHP version

$ php -v

PHP 7.3.16-1+ubuntu16.04.1+deb.sury.org+1 (cli) (built: Mar 20 2020 13:51:21) ( NTS )

Verify PHP links after update

$ ls -l /etc/alternatives/ | grep “php”

lrwxrwxrwx 1 root root 15 Apr 1 16:38 php -> /usr/bin/php7.3

lrwxrwxrwx 1 root root 31 Apr 1 16:38 php.1.gz -> /usr/share/man/man1/php7.3.1.gz

lrwxrwxrwx 1 root root 19 Apr 1 16:08 php-cgi -> /usr/bin/php-cgi7.3

lrwxrwxrwx 1 root root 35 Apr 1 16:08 php-cgi.1.gz -> /usr/share/man/man1/php-cgi7.3.1.gz

lrwxrwxrwx 1 root root 23 Apr 1 16:08 php-cgi-bin -> /usr/lib/cgi-bin/php7.3

Commonly used Update Alternatives commands examples to verify packages/libraries

$ update-alternatives –version

Debian update-alternatives version 1.18.4.

This is free software; see the GNU General Public License version 2 or

later for copying conditions. There is NO warranty.

$ update-alternatives –display php

php – manual mode

link best version is /usr/bin/php7.3

link currently points to /usr/bin/php7.3

link php is /usr/bin/php

slave php.1.gz is /usr/share/man/man1/php.1.gz

/usr/bin/php7.0 – priority 70

slave php.1.gz: /usr/share/man/man1/php7.0.1.gz

/usr/bin/php7.1 – priority 71

slave php.1.gz: /usr/share/man/man1/php7.1.1.gz

/usr/bin/php7.3 – priority 73

slave php.1.gz: /usr/share/man/man1/php7.3.1.gz

$ update-alternatives –list php

/usr/bin/php7.0

/usr/bin/php7.1

/usr/bin/php7.3

$ update-alternatives –query php

Name: php

Link: /usr/bin/php

Slaves:

php.1.gz /usr/share/man/man1/php.1.gz

Status: manual

Best: /usr/bin/php7.3

Value: /usr/bin/php7.3

Alternative: /usr/bin/php7.0

Priority: 70

Slaves:

php.1.gz /usr/share/man/man1/php7.0.1.gz

Alternative: /usr/bin/php7.1

Priority: 71

Slaves:

php.1.gz /usr/share/man/man1/php7.1.1.gz

Alternative: /usr/bin/php7.3

Priority: 73

Slaves:

php.1.gz /usr/share/man/man1/php7.3.1.gz

Author : Mohandass

——————————————————————–

Smoking Causes Lung CANCER !!!

Stay Home Stay Safe

Leave a Reply