PHP Version change in Ubuntu

If multiple PHP versions are installed on Ubuntu, you can choose which version is used by default from the command line.

Run:

sudo update-alternatives --config php

Ubuntu will display the installed PHP versions, for example:

Selection Path Priority Status
------------------------------------------------
* 0 /usr/bin/php8.4 84 auto mode
1 /usr/bin/php8.2 82 manual mode
2 /usr/bin/php8.3 83 manual mode
3 /usr/bin/php8.4 84 manual mode

Enter the number for the PHP version you want to use.

Then confirm the active version:

php -v

Important

update-alternatives changes the PHP version used by the terminal/CLI.

It does not necessarily change the PHP version used by a website.

If the server uses PHP-FPM with Nginx, the site's Nginx configuration may still point to a specific PHP-FPM socket, such as:

/run/php/php8.4-fpm.sock

To see the installed PHP-FPM versions:

ls /run/php/

After changing a website to another PHP-FPM version, restart the required services, for example:

sudo systemctl restart php8.4-fpm
sudo systemctl restart nginx

Use php -v for the CLI version and check the web server/PHP-FPM configuration separately when changing the PHP version used by a Laravel website.

Contact

Want to get in touch?

If you want to talk about a project, a software or IT role, something you have built, or a system you want to improve, send me a message.

I read the messages myself.

Thank you. Your message has been sent.