How to Change the PHP Version on Ubuntu for CLI and Apache

Learn how to switch between PHP versions on Ubuntu for both CLI and Apache. Ideal for developers maintaining multiple projects with different PHP version requirements.

How to Change the PHP Version on Ubuntu for CLI and Apache
Photo by Nathan da Silva / Unsplash

Managing multiple PHP projects often requires switching between PHP versions. For example, you might be developing a new application using PHP 8.0 while maintaining an older project running on PHP 7.4. Unlike Node.js, which benefits from tools like NVM for managing versions, PHP lacks a built-in version manager—so the process must be done manually.

This guide walks you through switching PHP versions on a Debian-based system like Ubuntu, covering both CLI and Apache configurations.

If PHP isn't installed on your system yet, follow this tutorial to set up a complete web server on WSL 2 first.

Check Your Current PHP Version

CLI Version

To see the current PHP version used in your terminal, run:

php -v

Example output:

PHP 7.4.3 (cli) (built: Jul  5 2021 15:13:35) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies

This confirms that the CLI is using PHP 7.4.

Apache Version

To check the PHP version used by Apache, create a simple PHP file (e.g., info.php) with the following content:

<?php phpinfo(); ?>

Open it in your browser through your local web server to see detailed PHP version and module information.

How to Switch PHP Versions on Ubuntu

Switching PHP versions involves configuring both the web server (Apache) and the terminal (CLI). These settings are managed independently, so both must be updated separately.

Change PHP Version for Apache

To switch the PHP version used by Apache, follow these steps:

sudo a2dismod php7.4
sudo a2enmod php8.0
sudo service apache2 restart
  • a2dismod disables the current PHP module.
  • a2enmod enables the desired PHP module.
  • service apache2 restart restarts the Apache server to apply changes.

After running these commands, Apache will use PHP 8.0 instead of PHP 7.4.

Change PHP Version for CLI

To switch PHP versions for terminal use, you need to update symbolic links using update-alternatives. This ensures commands like php and phar point to the correct version.

Run the following commands:

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

These commands update the symbolic links for php, phar, and phar.phar.

You can verify the new version by running:

php -v

You should now see output similar to:

PHP 8.0.8 (cli) (built: Jul  1 2021 15:26:46) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.8, Copyright (c) Zend Technologies
    with Zend OPcache v8.0.8, Copyright (c), by Zend Technologies

Reverting to a Previous PHP Version

If you need to switch back to an older PHP version (e.g., from PHP 8.0 to 7.4), simply reverse the commands:

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

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 method works for any installed PHP versions, not just 7.4 and 8.0.

Conclusion

Switching between PHP versions on Ubuntu is essential for maintaining compatibility across projects. By updating Apache modules and symbolic links for the terminal, you can easily toggle between PHP versions as needed.

Although PHP lacks a built-in version manager like Node.js, these simple commands give you full control over your development environment.