Table of Contents
Introduction: In web development, different projects may require different versions of PHP due to compatibility or specific feature requirements. Ubuntu, a popular Linux distribution, offers a simple way to manage multiple PHP versions. This guide will walk you through the process of switching PHP versions on the command line in Ubuntu.
Prerequisites:
- Ubuntu Linux installed on your system.
- Multiple PHP versions installed using a package manager like
apt
.
Steps to Switch PHP Versions on the Command Line:
- Check Installed PHP Versions: Before switching PHP versions, it’s essential to know which versions are currently installed on your system. You can list installed PHP versions using the following command:
ls /usr/bin/php*
This command will display a list of PHP binaries available on your system.
- Choose PHP Version: Identify the PHP version you want to switch to based on your project requirements.
- Create Symbolic Link: Ubuntu uses symbolic links to manage default PHP version. To switch to a different PHP version, you need to update the symbolic link that points to the default PHP binary.
First, remove the existing symbolic link by running:
sudo rm /usr/bin/php
Then, create a new symbolic link pointing to the desired PHP version. Replace [version]
with the version number you want to switch to. For example:
sudo ln -s /usr/bin/php[version] /usr/bin/php
For instance, if you want to switch to PHP 7.4, the command will be:
sudo ln -s /usr/bin/php7.4 /usr/bin/php
Verify PHP Version: To confirm that you have successfully switched PHP versions, run:
php -v
This command will display the currently active PHP version along with other information.
- Optional: Restart Web Server: If you are using PHP with a web server like Apache or Nginx, you may need to restart the server for the changes to take effect. Use the following commands to restart Apache or Nginx:
For Apache:
sudo systemctl restart apache2
For Nginx:
sudo systemctl restart nginx
sudo systemctl restart nginx
Another Way To Switch PHP
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
Conclusion
Managing multiple PHP versions on Ubuntu is straightforward, thanks to the flexibility of symbolic links. By following the steps outlined in this guide, you can seamlessly switch between PHP versions to meet the requirements of your various projects. This flexibility ensures compatibility and enables you to utilize the features offered by different PHP versions effortlessly.
Remember to choose the PHP version carefully based on your project’s needs and test thoroughly after switching to ensure everything functions as expected.