How to Switch PHP versions using Command Line in Ubuntu

  1927 views   6 months ago PHP

You might need multiple PHP version for different projects in your system. So to use different versions, you need to disable old version and enable new version that you want to use.

Suppose your system PHP 7.4 and 8.0 both versions installed and you want to switch PHP version 7.4 to 8.0.

If you want to change php version for apache, run the below command:

sudo a2dismod php7.4
sudo a2enmod php8.0
sudo service apache2 restart

If you want to change php version for command line, run the command:

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

Now check the PHP version with php --version command. If you want to switch PHP 8.0 to 7.4 version, you can also switch php version for apache and command line.

Apache version:

sudo a2dismod php8.0
sudo a2enmod php7.4
sudo service apache2 restart

Command line verison:

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

This way you can switch between different php versions in single Ubuntu system.

Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]