You can do this with update-alternatives. If you would like to do this interactively, you can do this:

sudo update-alternatives --config php

If you like to specifically choose the PHP version (via an alias or whatnot), you can do this:

sudo update-alternatives --set php /usr/bin/php7.4

Of course, be sure to change php7.4 to the actual version you want to switch between.

Answer from matigo on askubuntu.com
Discussions

Upgrading PHP version
I need to update my php version for an app. Here is the version I am running: PHP 8.1.2-1ubuntu2.21 (cli) (built: Mar 24 2025 19:04:23) (NTS). I am running Ubuntu 22.04.5. My system says it is up-to-date. How do I get a minimum of 8.2, or preferably, the latest stable version? More on discourse.ubuntu.com
🌐 discourse.ubuntu.com
0
June 6, 2025
Change PHP version (use multiple versions at once)
If you don’t know what you’re doing, then first thing I’d do is find someone who does know what they’re doing. With that being said, the general gist would be not to use the PHP version switcher in some sort of cPanel or WHM as that will change the PHP version for the entire server. If you were setting up the sites from scratch, then you’d have Apache/nginx configs for each site, and each sites’ config would specify the php-fpm binary to use. That way, you could use different binary versions (PHP versions) per site. This is getting into the nitty-gritty of server administration and configuration, so if you don’t know what you’re doing that please have someone more knowledgable do this for you, otherwise you can end up bricking a site—and possibly the entire server—if you run some wrong commands or configure something incorrectly. More on reddit.com
🌐 r/PHPhelp
12
4
December 30, 2023
apache httpd - Switch PHP versions in the console, Ubuntu - Unix & Linux Stack Exchange
Trying to switch PHP versions at the shell in Ubuntu: I'd like to switch from version 8.1 to 8.3. I complete the steps above and restart apache but my server remains using version 8.1? Am I missi... More on unix.stackexchange.com
🌐 unix.stackexchange.com
March 17, 2024
How to make new PHP version a system default?
SYSTEM INFORMATION OS type and version AlmaLinux 9.2 Webmin version 2.101 Virtualmin version 7.8.2 This question has been asked here couple of times, but I can’t find a decent answer. I got PHP 8.0 with AlmaLinux 9.2 package. I’m running Laravel 10, which requires PHP 8.2. I installed it ... More on forum.virtualmin.com
🌐 forum.virtualmin.com
0
November 1, 2023
🌐
OneUptime
oneuptime.com › home › blog › how to switch between php versions on ubuntu
How to Switch Between PHP Versions on Ubuntu
2 weeks ago - If you are using mod_php (not PHP-FPM) with Apache, switching versions requires disabling one module and enabling another: # Disable current PHP module (e.g., PHP 7.4) sudo a2dismod php7.4 # Enable new PHP module (e.g., PHP 8.2) sudo a2enmod ...
🌐
DEV Community
dev.to › vanwildemeerschbrent › changing-php-versions-on-ubuntu-2204-if5
How to change your PHP version on Ubuntu 22.04 - DEV Community
November 10, 2023 - Info This is a guide to change your PHP version through the command line on Ubuntu...
Top answer
1 of 5
108

apt-get update updates only the package lists. To actually upgrade the packages you have to run:

sudo apt-get upgrade

or

sudo apt-get dist-upgrade

More info here.


If you ran the above commands but the packages still were not upgraded, probably you didn't add Ondřej Surý's PHP PPA to your system.

Run the following to add the PPA:

  • For PHP 5.5, PHP 5.6 and PHP 7.0:

    sudo apt-add-repository ppa:ondrej/php
    
  • For PHP 5.4 (Deprecated, upgrade at least to PHP 5.5):

    sudo apt-add-repository ppa:ondrej/php5-oldstable
    
  • For PHP 5.5 (Deprecated, use ppa:ondrej/php):

    sudo apt-add-repository ppa:ondrej/php5
    
  • For PHP 5.6 (Deprecated, use ppa:ondrej/php):

    sudo apt-add-repository ppa:ondrej/php5-5.6
    

Then update the package lists and perform the upgrade:

sudo apt-get update

For PHP 5.5:

sudo apt-get install php5.5

For PHP 5.6:

sudo apt-get install php5.6

For PHP 7:

sudo apt-get install php7.0

Release upgrade

If you want to install the latest PHP (for example PHP7) but you want to stick to the release's packages intead of hacking the sources around; and you want to do a full-release upgrade (for example you are in Ubuntu 14.04 -which does not have php7- and you want to go to Ubuntu 16.04 -which does- then you can do:

do-release-upgrade

Now you should have the latest PHP version.

2 of 5
13

For newest updating process use the following command

sudo LC_ALL=C.UTF-8 add-apt-repository ppa:ondrej/php

Then run the following command in terminal

sudo apt-get update 

This is worked well when installing php 5.5,5.6 and 7.0

If there some errors this command will resolve the errors

sudo dpkg --configure -a

Update 2020-10-14: For versions upto 7.*

sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt update

Then

sudo apt install php7.2

Here also you can use this for php 7.0,7.1,7.3 to 7.4

After updating process you can use below command to install extensions

sudo apt update 
sudo apt install php-xml

or you can use like below

sudo apt install php7.2 php-xml  

Update 2021-01-31: For versions upto 8.*

sudo apt-add-repository ppa:ondrej/php
sudo apt update

Then

sudo apt install -y php8.0 php8.0-cli php8.0-common
🌐
Ubuntu Community Hub
discourse.ubuntu.com › support and help
Upgrading PHP version - Support and Help - Ubuntu Community Hub
June 6, 2025 - I need to update my php version for an app. Here is the version I am running: PHP 8.1.2-1ubuntu2.21 (cli) (built: Mar 24 2025 19:04:23) (NTS). I am running Ubuntu 22.04.5. My system says it is up-to-date. How do I ge…
Find elsewhere
🌐
Jonathan Bossenger
jonathanbossenger.com › running-different-php-versions-with-apache-on-ubuntu
Running Different PHP Versions with Apache on Ubuntu – Jonathan Bossenger
February 4, 2024 - To switch back to PHP 8.1, simply run the update-alternatives commands, but change the version number to 8.1.
🌐
OSTechNix
ostechnix.com › home › programming › php › how to switch between multiple php versions in ubuntu
Switch Between Multiple PHP Versions In Ubuntu - OSTechNix
May 2, 2025 - And, that's all for now. This is how we install multiple PHP versions in Ubuntu. With the help of the update-alternatives command, we can quickly change from one PHP version to another in Ubuntu and set default PHP version.
🌐
ServerPilot
serverpilot.io › docs › php
PHP versions and settings | ServerPilot Documentation
The available PHP versions are determined by the server’s Ubuntu release. ... Go to the Apps page in the dashboard and click on the name of the app. Click on Settings. For the PHP Version, click the edit icon and select the desired PHP version.
🌐
PHP.Watch
php.watch › articles › php-84-install-upgrade-guide-debian-ubuntu
PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian • PHP.Watch
November 21, 2024 - However, using the update-alternatives command, this can be changed to any other PHP version: ... There are 2 choices for the alternative php (providing /usr/bin/php). Selection Path Priority Status -----------------------------------------...
🌐
Reddit
reddit.com › r/phphelp › change php version (use multiple versions at once)
r/PHPhelp on Reddit: Change PHP version (use multiple versions at once)
December 30, 2023 -

So, I have a VPS, Im running several web applications (built on PHP ofc).

I want to upgrade PHP for some of the web applications.

Today Im running this:

Web application A - PHP 7.3
Web application B - PHP 7.3
Web application C - PHP 7.3
Web application D - PHP 7.3

I would like to do this:

Web application A - PHP 7.3
Web application B - PHP 8.1
Web application C - PHP 7.3
Web application D - PHP 8.1

Can I simply change the PHP version here (in cPanel): https://ibb.co/9tpQdTr

Since I don't know anything about web servers, PHP Apache and so on, I don't really know what I'm doing.

🌐
Stack Exchange
unix.stackexchange.com › questions › 772554 › switch-php-versions-in-the-console-ubuntu
apache httpd - Switch PHP versions in the console, Ubuntu - Unix & Linux Stack Exchange
March 17, 2024 - Trying to switch PHP versions at the shell in Ubuntu: I'd like to switch from version 8.1 to 8.3. I complete the steps above and restart apache but my server remains using version 8.1? Am I missi...
🌐
TecAdmin
tecadmin.net › switch-between-multiple-php-version-on-ubuntu
How to Change Default PHP Version on Ubuntu – TecAdmin
April 26, 2025 - By executing a simple series of commands, you can easily configure your system to use your preferred PHP version. For those interested in expanding their PHP setup, you might find our guide on installing PHP 8 and PHP 7 on Ubuntu 22.04 particularly useful. Before changing to PHP 8.3, it is necessary to disable any previously enabled Apache modules.
🌐
ezeelogin
ezeelogin.com › kb › article › how-to-install-and-switch-different-versions-of-php-in-ubuntu-506.html
How to install and switch different versions of PHP in Ubuntu?
March 21, 2025 - root@ubuntu ~]# echo "<?php phpinfo()?>" >> /var/www/html/phpinfo.php · Step 2. Run the below commands to disable and enable multi PHP versions. ... Step 3. Now refresh GUI and confirm the PHP version has changed.
🌐
PHP.Watch
php.watch › articles › php-8.3-install-upgrade-on-debian-ubuntu
How to install or upgrade to PHP 8.3 on Ubuntu and Debian • PHP.Watch
November 12, 2023 - A complete guide to install or upgrade to PHP 8.3 on Ubuntu 22.04 (Jammy), Ubuntu 20.04 (Focal), Debian 10 (Buster), 11 (Bullseye), and Debian 12 (Bookworm).
🌐
Stapps
blog.stapps.io › easy-php-version-switching-on-ubuntu
Easy PHP version switching on Ubuntu (allows for running multiple versions at the same time)
January 3, 2025 - Using PHP FPM you can switch PHP version easily by switching which /var/run/php/php7.4-fpm.sock (this may also be /run/php/php7.4-fpm.sock) you reference in either the Apache or Nginx config (or whichever system you use to host.
🌐
Virtualmin
forum.virtualmin.com › help! (home for newbies)
How to make new PHP version a system default? - Help! (Home for newbies) - Virtualmin Community
November 1, 2023 - SYSTEM INFORMATION OS type and version AlmaLinux 9.2 Webmin version 2.101 Virtualmin version 7.8.2 This question has been asked here couple of times, but I can’t find a decent answer. I got PHP 8.0 with AlmaLinux 9.2 package. I’m running Laravel 10, which requires PHP 8.2. I installed it following this instructions: Multiple PHP Versions – Virtualmin.