Apache2 | How To Switch Between PHP Version In Ubuntu ?
In many cases, you might want to switch between PHP versions according to the project specifications. Some projects solely depend on some specific versions, some products produce error with certain versions and some projects need to be checked for version compatibilities.
Considering you have installed several PHP versions in your system, the following commands can be used to switch between PHP versions in Linux.
How To Switch Between PHP Version In Ubuntu
Interactive Way
The first technique is interactive way of switching. You can utilize the update-alternatives
command with --config
flag accordingly.
sudo update-alternatives --config php
sudo update-alternatives --config phar
sudo update-alternatives --config phar.phar
Manual Way
In manual way of switching, you have to first disable the current version, enable required version and restart the server. Lets assume that we are switching from php7.0
to php8.0
sudo a2dismod php7.0
sudo a2enmod php8.0
sudo service apache2 restart
You can perform the same using update-alternatives
command as well.
sudo update-alternatives --set php /usr/bin/php8.0
sudo update-alternatives --set phar /usr/bin/phar8.0
sudo update-alternatives --set phar.phar /usr/bin/phar.phar8.0
If you prefer the single line command, use the following:
sudo a2dismod php7.0 && sudo a2enmod php8.0 && sudo service apache2 restart