103

I am new to using Linux and I broke some php settings while tampering.

If I execute a php script containing: phpinfo(); it shows the php versions as 5.6, but via the command line, if I run php -v it returns a version of 7.0.

I want to have both versions match.
How can i fix my command line PATH to also use php 5.6?

2
  • check both ini files Mar 27, 2017 at 8:02
  • Clearer wording on the problem without changing or adding any details. English isn't likely the author's first language. Hope this helps. Mar 29, 2017 at 17:41

8 Answers 8

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

Please see: Source

2
  • What to do if I have only one version and need to install more?
    – Čamo
    Nov 5, 2021 at 14:45
  • 2
    This changed the CLI Version of PHP but the browser one is still the same.
    – Black
    Jun 7, 2022 at 11:03
77

Here is more interactive way:

update-alternatives --config php

You'll see the list of all available versions and select one of them

3
  • 9
    Should be the new answer, much better this way and less error prone and time consuming
    – Neo
    Aug 5, 2019 at 18:22
  • What if I dont have more alternatives? This command shows the notice: There is only one alternative in link group php (providing /usr/bin/php): /usr/bin/php7.2 Nothing to configure.
    – Čamo
    Nov 5, 2021 at 14:26
  • @Čamo, update-alternatives allows you to set default php version from the list of installed in your system, so... have you installed something other than php7.2?
    – Dmitry
    Nov 7, 2021 at 11:47
45

From PHP 7.1 => PHP 5.6

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

From PHP 5.6 => PHP 7.1

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

You can change the versions as you want.

You can check this tutorial

Regards!!

1
  • Thank you so much! And I mean SOOOOOOOOO MUCH! Jun 21, 2022 at 14:53
41

This explanation is based on Ubuntu 16.04 but is expected to work for other versions too

Most answers here manipulate the php-version by using the command

sudo update-alternatives --set ...

While the command is quite useful it's never explained what it does exactly. Here comes the explanation, including backup and some options:

Get the Information

  • The command update-alternatives is displaying or changing symlinks which reside in an alternative direction which is in Ubuntu usually defined as /etc/alternatives but which could be changed too.
  • a full list of options related to update-alternatives can be shown with the command update-alternatives --help, a deeper explanation can be shown with man update-alternatives.
  • As the command update-alternatives is primary changing symlinks in a special folder, the content of that folder can be shown with common commands too, following all items starting with php are shown:
 $ ls -al /etc/alternatives/php*  
lrwxrwxrwx 1 root root 15 Jan 19 02:58 /etc/alternatives/php -> /usr/bin/php7.2  
lrwxrwxrwx 1 root root 31 Jan 19 02:58 /etc/alternatives/php.1.gz -> /usr/share/man/man1/php7.2.1.gz  
lrwxrwxrwx 1 root root 19 Jan 19 03:00 /etc/alternatives/php-cgi -> /usr/bin/php-cgi7.2  
lrwxrwxrwx 1 root root 35 Jan 19 03:00 /etc/alternatives/php-cgi.1.gz -> /usr/share/man/man1/php-cgi7.2.1.gz 
lrwxrwxrwx 1 root root 23 Jan 19 03:00 /etc/alternatives/php-cgi-bin -> /usr/lib/cgi-bin/php7.2
  • to display the items with the command update-alternatives use this command:
$ update-alternatives --list php  
/usr/bin/php7.0  
/usr/bin/php7.2
  • to show more details you can execute this:
$ update-alternatives --display php
php - auto mode
  link best version is /usr/bin/php7.2
  link currently points to /usr/bin/php7.2
  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.2 - priority 72
  slave php.1.gz: /usr/share/man/man1/php7.2.1.gz

Finally we still want to know which php-versions we can link. The following command shows a list of the currently available php-versions in /usr/bin/:

$ ls -al /usr/bin/php*
lrwxrwxrwx 1 root root      21 Jan  1 19:47 /usr/bin/php -> /etc/alternatives/php
-rwxr-xr-x 1 root root 4385840 Apr  5 18:13 /usr/bin/php7.0
-rwxr-xr-x 1 root root 4875488 Apr  5 18:10 /usr/bin/php7.2
lrwxrwxrwx 1 root root      25 Jan  1 19:47 /usr/bin/php-cgi -> /etc/alternatives/php-cgi
-rwxr-xr-x 1 root root 4279672 Apr  5 18:13 /usr/bin/php-cgi7.0
-rwxr-xr-x 1 root root 4769272 Apr  5 18:10 /usr/bin/php-cgi7.2
-rwxr-xr-x 1 root root     663 Feb 29  2016 /usr/bin/phpunit

Backup the information

  • To make an informational backup of these symlinks just save the list in a file and save it on your desktop with this command (adjust the filename alternatives-php.txt to your needs):
$ echo "command: update-alternatives --list php" > ~/Desktop/alternatives-php.txt
$ update-alternatives --display php >> ~/Desktop/alternatives-php.txt 
$ echo "command: update-alternatives --display php" >> ~/Desktop/alternatives-php.txt
$ update-alternatives --display php >> ~/Desktop/alternatives-php.txt  

if you like you still can add the result of the command ls like shown above:

$ echo "command: ls -al /etc/alternatives/php*" >> ~/Desktop/alternatives-php.txt
$ ls -al /etc/alternatives/php* >> ~/Desktop/alternatives-php.txt

... and the available PHP-versions:

$ echo "command: ls -al /usr/bin/php*" >> ~/Desktop/alternatives-php.txt
$ ls -al /usr/bin/php* >> ~/Desktop/alternatives-php.txt

Change the PHP-version for commandline

  • If we filter first the man-documents the list in /etc/alternatives/php*/ includes 3 lines:
 $ ls -al /etc/alternatives/php*  
lrwxrwxrwx 1 root root 15 Jan 19 02:58 /etc/alternatives/php -> /usr/bin/php7.2  
lrwxrwxrwx 1 root root 19 Jan 19 03:00 /etc/alternatives/php-cgi -> /usr/bin/php-cgi7.2  
lrwxrwxrwx 1 root root 23 Jan 19 03:00 /etc/alternatives/php-cgi-bin -> /usr/lib/cgi-bin/php7.2
  • While on the one hand it might be useful to have consistent links for the whole system on the other hand the fact that several php-versions exist already on the system implies that it's used for development and php for cli, web and cgi could be configured differently.
    Which symlink has to be changed?
    PHP for web is usually not configured by usage of the symlinks in /etc/alternatives and cli(commandline) is usually not using the cgi-version or cgi-configuration. So it can be assumed that changing only the symlink for php will work for commandline, change of php-cgi and php-cgi-bin is probably not required. Usually the most recent version is linked by default, so the command below is using an older version taken from the list in /usr/bin/:
$ sudo update-alternatives --set php /usr/bin/php7.0
update-alternatives: using /usr/bin/php7.0 to provide /usr/bin/php (php) in manual mode

Control

Checking the change:

$: php -v
PHP 7.0.29-1+ubuntu16.04.1+deb.sury.org+1 (cli) (built: Apr  5 2018 08:34:50) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.0.29-1+ubuntu16.04.1+deb.sury.org+1, Copyright (c) 1999-2017, by Zend Technologies

The change was accepted and the used version is an older one now.

  • Now we check the version the man-page is written for:
$: man php
... [SCROLL DOWN]
VERSION INFORMATION
   This manpage describes php, version 7.0.29-1+ubuntu16.04.1+deb.sury.org+1.
COPYRIGHT
...

This means the PHP-version is consistent with the man-page and the command man php returns the right descriptions for the current php-version and no manual adjustments concerning the man-page have been required.

7
  • This is very good. I only have PHP 7.0 so gonna install the others.
    – kJamesy
    May 3, 2018 at 9:48
  • A true MVP makes detailed answers like these
    – Sunhat
    May 29, 2020 at 4:19
  • @Sunhat thanks, up-vote of the answer is appreciated :)
    – David
    Jun 1, 2020 at 14:12
  • What it I have only one version and need to add other newer version? Dont know how to do it. What should I copy from where to where?
    – Čamo
    Nov 5, 2021 at 14:43
  • @Čamo you can list all versions for you ubuntu version with sudo apt list php*.
    – David
    Nov 9, 2021 at 15:40
4

check what you have before changing

 php -v

change everything to php 7.2

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
3

You can try setting the path to the correct php version on the terminal command line:

set PATH="/usr/bin/php5.6/bin:$PATH"

Modify the path to match your own path to PHP 5.6 (ie. if it was installed first, it might be something like "/usr/bin/php").

The "$PATH" at the end appends the current path value, so don't forget to use it.

0

Plesk

Just to say, if your server is managed using Plesk (like mine is at the time of searching through this question) then you may not have later PHP versions in /usr/bin

In this case, your PHP versions will be located in /opt/plesk/php so you can instead use the following command as a super user (run su first if you're not a super user) to set your PHP CLI version (to 7.3 in this example):

echo "alias php='/opt/plesk/php/7.3/bin/php'" >> ~/.bashrc

Log out and log back in and.... voila!

Notes

  1. You must be have super user privileges to run this
  2. If you want to set the CLI to a different version then simply replace the 7.3 in the command to whichever version you would like (in the OP's case this would be 5.6
  3. If you want to check which versions Plesk has available for you to use, simply run: ls /opt/plesk/php

Source: Plesk Support

2
  • $ which php /usr/bin/php $ mv /usr/bin/php /usr/bin/php-backup $ ln -s /opt/plesk/php/7.3/bin/php /usr/bin/php $ php -v Jun 14 at 21:48
  • When you look into Plesk installer maybe you find more PHP versions that can be installed than the ones that are already on your system.
    – Thomas
    Jun 16 at 15:12
0

For PLESK do like that:

$ which php
/usr/bin/php
$ mv /usr/bin/php /usr/bin/php-backup
$ ln -s /opt/plesk/php/7.3/bin/php /usr/bin/php
$ php -v

and to see what is avaiable to choose from do:

/usr/local/psa/admin/bin/php_handlers_control --list

How to Change PHP Cli Version

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.