Recently, while juggling a Laravel project and an older legacy app, I ran into version conflicts with PHP on my Ubuntu setup. It reminded me how important it is to know how to quickly switch PHP versions on ubuntu from the terminal without breaking your local dev environment. If you’re in the same boat, here’s a clean guide that works every time.
Step 1: List All Installed PHP Versions
To change the version used by default (e.g., in CLI or scripts), use
update-alternatives --list php
BashIf you don’t see all versions listed, try listing all PHP binaries in /usr/bin/
:
ls /usr/bin/php*
BashThis helps confirm which versions are actually installed and where they’re located.
Step 2: Switch the Default PHP Version
To change the default PHP version, use:
sudo update-alternatives --config php
BashYou’ll see a list of available versions. Just type the number for the one you want to set as default and hit Enter. Ubuntu will instantly update the symlink for /usr/bin/php
.

For Apache Users: Restart and Switch PHP Modules
If you’re running Apache as your web server, just restart the apache server or you can disable the old PHP module and enable the one you’re switching to make it default:
sudo systemctl restart apache2
BashFor disabling the old PHP specific module and enable the one you’re switching to make it default:
sudo a2dismod php7.4
sudo a2enmod php8.2
sudo systemctl restart apache2
BashThis setup has saved me tons of time when working across different projects. No more breaking apps due to the wrong PHP version. If you’re working in Laravel, Symfony, or WordPress development on Ubuntu, I definitely recommend mastering this terminal trick to list and switch PHP versions on Ubuntu terminal.
Let me know in the comments if you hit any issues or if you’ve found a smoother method!