Manage multiple PHP versions in Linux: 2025 edition
Do you also have the below question in your mind?
How to manage multiple PHP versions in your local development environment?
If you are having trouble managing multiple PHP versions in your local development environment, then you're at the right place to get the best solution for your issue.
If you are over Linux like me, it is possible to install multiple PHP versions to run one version at a time with your web server.
You may need to switch between them as per your requirement with your project that supports the particular PHP version.
Assume that you have installed multiple PHP versions (let's take a few, PHP 5.6, 7.0, 7.2, 7.4, 8.0, and 8.1).
Also, keep in mind that you can only set a single version as a default one to run with your web server (Nginx or Apache).
The script shows you some PHP versions that are already at the end of their life. However, it is just a demo, and you can always update it to match the versions you are currently using.
It is a challenge to become a modern web developer, as one should stay updated with new technologies and have to play with them every day.
Besides that, if a developer handles packages, frameworks, and libraries and their client's projects, they need to have a supported setup to apply new updates and fixes for their works.
Let's dive into the actual workflow on how you can manage multiple PHP versions in your machine.
Open up your favorite code editor and copy and paste the snippet below.
phpswap () {
if [[ -z "$1" ]]; then
echo "You did not specify a version (5.6, 7.0, 8.0, 8.1)"
elif [[ "$1" == "5.6" ]]; then
echo "Switching to PHP 5.6"
sudo ln -sf /usr/bin/php5.6 /etc/alternatives/php
sudo a2dismod php7.0
sudo a2dismod php8.0
sudo a2dismod php8.1
sudo a2enmod php5.6
sudo systemctl restart apache2
elif [[ "$1" == "7.0" ]]; then
echo "Switching to PHP 7.0"
sudo ln -sf /usr/bin/php7.0 /etc/alternatives/php
sudo a2dismod php5.6
sudo a2dismod php8.0
sudo a2dismod php8.1
sudo a2enmod php7.0
sudo systemctl restart apache2
elif [[ "$1" == "8.0" ]]; then
echo "Switching to PHP 8.0"
sudo ln -sf /usr/bin/php8.0 /etc/alternatives/php
sudo a2dismod php5.6
sudo a2dismod php7.0
sudo a2dismod php8.1
sudo a2enmod php8.0
sudo systemctl restart apache2
elif [[ "$1" == "8.1" ]]; then
echo "Switching to PHP 8.1"
sudo ln -sf /usr/bin/php8.1 /etc/alternatives/php
sudo a2dismod php5.6
sudo a2dismod php7.0
sudo a2dismod php8.0
sudo a2enmod php8.1
sudo systemctl restart apache2
else
echo "You did not specify a version (5.6, 7.0, 8.0, 8.1)"
fi
}
The script has support for multiple versions of PHP and has the way to run it with a single command and easy to remember bash function that you can run on your terminal whenever you need it.
Apply the relevant changes to the actual version you want to use in your environment.
After all, you need to apply it to the run time of your system somewhere.
Open up your terminal and hit the below command.
nano ~/.bashrc
Once the edit mode appears on your screen, paste the script somewhere in the file and save it.
Log off your system and login in again, or reboot if necessary, then you are ready to go with it.
Usage
Open up your terminal and check your current php version with php -v
After showing the current version, you can hit phpswap, and it alerts you with options you have set up previously with the bash function.
In my case, it shows like this below.
➜ phpswap
You did not specify a version (7.0, 8.0, 8.1)
Then, hit phpswap 8.1, and it does everything else for you, and after it finishes running, it's ready with another version activated.
Easy right?
Conclusion
Thanks for reading this post up to the end.
If the article was helpful to you, share it with your circle to help spread the good content.
If you have any feedback feel free to submit the contact us form, and I will get your message right into my inbox.
Happy Coding!
Last updated: Dec, 17, 2024