How to switch between different PHP versions on Linux ???

Viraj Madhushan
3 min readOct 4, 2022

--

Image by OpenClipart-Vectors from Pixabay

You can easily switch between 2 different PHP versions in your Linux pc. There are 2 different ways we can do it.

1) Interactive switching mode in Terminal

If you wanted to do some changes or install new package which required specific PHP version, you can use this method. Specially if you wanted to change PHP version that use in composer, this method can really helpful.

Check current php version

php -v
Output of php -v command

Change php version

sudo update-alternatives --config php

You can see all installed php versions in your pc in the chart. In here, I only have php 7.4 and php 8.1 and my default php version is 8.1 that's the currently selected php version. This may be changed in yours.
After entering this command, the terminal is waiting for some input.

Press <enter> to keep the current choice[*], or type selection number:

In here, you need to give the selection number of required php version. So I need to change php version to the 7.4, I entered number 2. If you don’t want to change any version, you can simply press enter without giving any input. After successfully changing the version, you can see the message like this.

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

Then check again your php version using php -v command. You should be able to see changed php version in your pc.

In most cases, only above steps are working fine. But still you got some errors, you need to try change phar version as well. Same as the sudo update-alternatives — config php you need to run below commands.

sudo update-alternatives --config phar
sudo update-alternatives --config phar.phar

2) Manual mode in Terminal

In manual mode, you can switch between php versions using a single command. It does the same thing in Interactive mode, but it does not show what are the php versions we have. Then you need to know exactly what php version you are going to switch.
Let's consider switching to php7.4

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

That's all. Now your php version is 7.4 you can confirm it using php -v command. Same as Interactive mode if you need to change phar versions use…

sudo update-alternatives --set phar /usr/bin/phar7.4
sudo update-alternatives --set phar.phar /usr/bin/phar.phar7.4

3) Switching the php module that will be used by apache2 -Manual mode

Let's consider switching from php8.1 to php7.4 in apache2.
First, you need to disable current php version. a2dismod will do that.

sudo a2dismod php8.1

Then we need to enable required php version. We can do it using a2enmod.

sudo a2enmod php7.4

Finally, need to restart the apache2 server.

sudo service apache2 restart

Conclusion

we can switch between any php versions which is already installed in our computer. You can use, either Interactive or Manual mode to accomplish that task.

--

--

Viraj Madhushan

Passionate learner of AI and Robotics. Always exploring new technologies to stay ahead of emerging trends. Hiking and reading enthusiast.