If your environment is configured with multiple PHP installations, such as PHP 8.3, PHP 8.2, PHP 7.4, and PHP 5.6, with PHP 7.4 currently serving as the default for both Apache and the command line interface (CLI), you might find yourself in need of switching the default PHP version. It’s common to have various PHP versions for different projects, but only one can be designated as the default at any given time.
This guide is designed to assist you in altering the default PHP version utilized by the CLI and Apache web server. By executing a simple series of commands, you can easily configure your system to use your preferred PHP version.
For those interested in expanding their PHP setup, you might find our guide on installing PHP 8 and PHP 7 on Ubuntu 22.04 particularly useful.
Switching to PHP 8.3
Before changing to PHP 8.3, it is necessary to disable any previously enabled Apache modules. Use the commands below to deactivate the current Apache module and switch to the PHP 8.3 module. Following that, the listed commands will configure PHP 8.3 as the default version for the command line interface (CLI).
- Apache:
sudo a2dismod php*
sudo a2enmod php8.3
sudo systemctl restart apache2
- Command line interface:
sudo update-alternatives --set php /usr/bin/php8.3
sudo update-alternatives --set phar /usr/bin/phar8.3
sudo update-alternatives --set phar.phar /usr/bin/phar.phar8.3
sudo update-alternatives --set phpize /usr/bin/phpize8.3
sudo update-alternatives --set php-config /usr/bin/php-config8.3
Note: The phpize8.3 and php-config8.3 command is available in php8.2-dev package. This is more useful for compiling PHP modules using pecl.
Switching to PHP 8.2
Before switching to PHP 8.2, you have to deactivate the previously activated Apache module. The following commands will deactivate the already activated Apache module and then activate PHP 8.2 Apache module. Then the next commands will set PHP 8.2 as the default version for the command line interface (CLI).
- Apache:
sudo a2dismod php*
sudo a2enmod php8.2
sudo systemctl restart apache2
- Command line interface:
sudo update-alternatives --set php /usr/bin/php8.2
sudo update-alternatives --set phar /usr/bin/phar8.2
sudo update-alternatives --set phar.phar /usr/bin/phar.phar8.2
sudo update-alternatives --set phpize /usr/bin/phpize8.2
sudo update-alternatives --set php-config /usr/bin/php-config8.2
Note: The phpize8.2 and php-config8.2 command is available in php8.2-dev package. This is more useful for compiling PHP modules using pecl.
If you want a different version (eg: PHP 8.1 or PHP 8.0) to be configured as default, just replace 8.2 with the required version.
Switch to PHP 7.4
Similarly, if you need to configure PHP 7.4 as the default version in your system. The following set of commands will change the default PHP to 7.4 for the Apache web server and command line interface.
- Apache:
sudo a2dismod php*
sudo a2enmod php7.4
sudo systemctl restart apache2
- Command line interface:
sudo update-alternatives --set php /usr/bin/php7.4
sudo update-alternatives --set phar /usr/bin/phar7.4
sudo update-alternatives --set phar.phar /usr/bin/phar.phar7.4
sudo update-alternatives --set phpize /usr/bin/phpize7.4
sudo update-alternatives --set php-config /usr/bin/php-config7.4
Note – The phpize7.4 and php-config7.4 command is available in php7.4-dev package. This is more useful for compiling PHP modules using pecl.
Switch to PHP 5.6
PHP 5.6 is an outdated version. So if you have an application that required PHP 5.6, consider them to upgrade to last PHP version. The following set of commands will change the default PHP to 5.6 for the Apache web server and command line interface.
- Apache:
sudo a2dismod php*
sudo a2enmod php5.6
sudo systemctl restart apache2
- Command line interface:
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
Note: The phpize5.6 and php-config5.6 command is available in php5.6-dev package. This is more useful for compiling PHP modules using pecl.
Conclusion
Switching between different PHP versions for your CLI and Apache web server doesn’t have to be a daunting task. By following the steps outlined in this tutorial, you can efficiently manage multiple PHP installations and ensure that your development environment is aligned with your project requirements. This flexibility allows you to work seamlessly across various projects, each with its own set of PHP version dependencies, thereby optimizing your development workflow.
37 Comments
I have read your blog on the differences between PHP 5 and PHP 7. It was very interesting and helpful but I can add some extra points in your article. Here some extra points:
1.PHP 7.0 added scalar type declarations for strings, integers, floating-point numbers, and booleans.
2.PHP 7.0 added support for return type declarations.
3.PHP 7.1 added support for nullable parameter types and return types.
4.void is a valid return type as of PHP 7.1.
These are some extra points to add to your article. Readers if you are confused about your web and App development , you can get a free consultation at Alakmalak technologies.Visit our site for more information.
I have Switched from 8.1 => 7.4.
After following the above steps Switched successfully and I can see a Switched version of PHP in the terminal and info.php.
But My phpmyadmin is not working after doing this.
All the latest versions of phpMyAdmin support PHP 7.4. Might be that PHP 7.4 have some missing extensions. Please check the Apache error log files to find out the actual error.
Error is returning like this as below.
Error: [Mon Jan 30 17:11:16.156924 2023] [php7:error] [pid 26344] [client 127.0.0.1:55872] PHP Parse error: syntax error, unexpected ‘static’ (T_STATIC) in /usr/share/php/Symfony/Component/DependencyInjection/ContainerBuilder.php on line 272
This is a topic which is close to myy heart… Cheers!
Exactly where are your contact details though?
i am so satisfacted.my english is poor, sorry :). thx for approving my user greetings wally
Would you expect the output of my test info.php to show PHP 5.6?
#php -i | grep “Loaded Configuration File”
Loaded Configuration File => /etc/php/5.6/cli/php.ini
But the info.php still shows
Configuration File (php.ini) Path /etc/php/7.4/fpm
Loaded Configuration File /etc/php/7.4/fpm/php.ini
i am so satisfacted. greetings wally
thanks….
I made a bash function for this process. i hope it be useful:
#disable current php and enable php with passed version
function switch_php {
sudo a2dismod php$(php -v | grep -o -E ‘[0-9.]+’ -m 1 | cut -c 1-3 | head -n 1);
sudo a2enmod php$1;
sudo service apache2 restart;
sudo update-alternatives –set php /usr/bin/php$1;
sudo update-alternatives –set phar /usr/bin/phar$1;
sudo update-alternatives –set phar.phar /usr/bin/phar.phar$1;
sudo update-alternatives –set phpize /usr/bin/phpize$1;
sudo update-alternatives –set php-config /usr/bin/php-config$1;
}
//sample:
$ switch_php 7.1
or
$ switch_php 5.6
thank you
Thanks for the tutorial, I was struggling trying to use PHP 7.3 in Ubuntu 20 and this helped me to configure things as needed.
BTW, do you know of any way besides pecl install –force to build an extension for different PHP version?
thanks, it helped much!
Not touching the tutorial itself, but it’s time to abandon the 5.6 version
How can I check which modules have each php versions without having to switch them and run “php -m” ?
Thank you
Use the command with PHP version like:
php7.2 -m
php5.6 -m
For enable/disable modules read: https://tecadmin.net/enable-disable-php-modules-ubuntu/
thanks…
I hope this
Also I hope
I’m using Centos 7 and I want to switch default version from ” PHP 5.4.16 (cli) ” to ” PHP 7.0.27 ” in shall so for that what would be the command?
Thanks, Rahul. This is exactly what I was looking for.
Great post Rahul,
Even I was on the same issue few months back. Similar article I found on a tech blog which saved my day.
Great explanation and detailing. Keep up the good work.
Thank you my friend.:)
Excelente, Buen aporte, de php 7.2 a 7.1,
I’m hosting 5 sites using nginx virtual sites .. I want to use PHP 7.1 for just one site only how can I do this ?
How abot switching from php7.1 to php7.2 ?
phpize and php-config throw error messages (no alternatives)
Hi, What error are you getting?
just install php7.2 and check php version
thanks, you save my time)
thanxx
Good Work! Thanks for the blog
Ne fonctionne pas du tout sur un ubuntu 16.04
Pour se la raconter dans la signature y a du monde, mais pour filer des informations pertinentes la y a plus personne
Nice, thank you very much, I could make the change easily with your tutorial
Also Consider:
From PHP 5.6 => PHP 7.1
update-alternatives –set phar /usr/bin/phar7.1 && update-alternatives –set phar.phar /usr/bin/phar.phar7.1 && systemctl restart apache2
From PHP 7.1 => PHP 5.6
update-alternatives –set phar /usr/bin/phar5.6 && update-alternatives –set phar.phar /usr/bin/phar.phar5.6 && systemctl restart apache2
Thanks Eduardo,
Article updated
Great post you have here, Rahul. Did you know that if you manage your server with RunCloud, you can just change the PHP version just a click. We developed RunCloud which will configure your server to the best configuration so you can focus only on things that matters. Check out http://runcloud.io to get your 15 days free trial.
what is your selling point over something like ISPConfig3 which is provided free of charge?