Switch php versions on commandline ubuntu 16.04
PhpApacheCommand Line-InterfacePhp 5.6Php 7.1Php Problem Overview
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 ?
Php Solutions
Solution 1 - Php
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
Solution 2 - Php
$ 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
Solution 3 - Php
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
Solution 4 - Php
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
Solution 5 - Php
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:
- 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
- add a PHP file in your runnable local environment like
/var/www/html/
path by addingphpinfo();
and get PHP info in the browser (in top of the page the version of PHP is available to see)
Solution 6 - Php
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.
Solution 7 - Php
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!
Solution 8 - Php
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
Solution 9 - Php
> 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
Solution 10 - Php
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
Solution 11 - Php
Type given command in your terminal..
For disable the selected PHP version...
-
- sudo a2dismod php5
- sudo service apache2 restart
-
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();
Solution 12 - Php
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
Solution 13 - Php
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
Solution 14 - Php
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!"
Solution 15 - Php
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
Solution 16 - Php
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</br></br>
Download the script file and run
sh switch.sh
Solution 17 - Php
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
Solution 18 - Php
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