Interactive switching mode

sudo update-alternatives --config php
sudo update-alternatives --config phar
sudo update-alternatives --config phar.phar

Manual Switching

From PHP 5.6 => PHP 7.1

Default PHP 5.6 is set on your system and you need to switch to PHP 7.1.

Apache:

$ sudo a2dismod php5.6
$ sudo a2enmod php7.1
$ sudo service apache2 restart

Command Line:

$ sudo update-alternatives --set php /usr/bin/php7.1
$ sudo update-alternatives --set phar /usr/bin/phar7.1
$ sudo update-alternatives --set phar.phar /usr/bin/phar.phar7.1

From PHP 7.1 => PHP 5.6

Default PHP 7.1 is set on your system and you need to switch to PHP 5.6.

Apache:

$ sudo a2dismod php7.1
$ sudo a2enmod php5.6
$ sudo service apache2 restart

Command Line:

$ sudo update-alternatives --set php /usr/bin/php5.6

Source

Answer from Stevie G on Stack Overflow
🌐
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 - To list all the installed PHP versions on your local device, use the following command : sudo update-alternatives --config php · To change your currently used PHP version to a different version (that is installed), you can use the following ...
🌐
Baeldung
baeldung.com › home › administration › switch between php versions on linux cli
Switch Between PHP Versions on Linux CLI | Baeldung on Linux
March 18, 2024 - We can switch to PHP 8.2 with a single command using the –set subcommand: $ sudo update-alternatives --set php /usr/bin/php8.2 update-alternatives: using /usr/bin/php8.2 to provide /usr/bin/php (php) in manual mode
🌐
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 - 1.1.4. Alternatively, you can run ... update-alternatives --config php · 1.1.5. Enter the selection number to set it as default version or simply press ENTER to keep the current choice....
🌐
TecAdmin
tecadmin.net › switch-between-multiple-php-version-on-ubuntu
How to Change Default PHP Version on Ubuntu – TecAdmin
April 26, 2025 - Following that, the listed commands will configure PHP 8.3 as the default version for the command line interface (CLI). ... sudo update-alternatives --set php /usr/bin/php8.3 sudo update-alternatives --set phar /usr/bin/phar8.3 sudo update-alternatives --set phar.phar /usr/bin/phar.phar8.3 sudo update-alternatives --set phpize /usr/bin/phpize8.3 sudo update-alternatives --set php-config /usr/bin/php-config8.3
🌐
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?
Selection Path Priority Status ... /usr/bin/php8.2 82 manual mode · Press <enter> to keep the current choice[*], or type selection number: 1 update-alternatives: using /usr/bin/phar7.4 to provide /usr/bin/phar (phar) in manual mode · root@ubuntu ~]# update-alternatives --config ...
Find elsewhere
Top answer
1 of 2
3

EDIT:

As pointed out by ᴍᴇʜᴏᴠ, Debian does support update-alternatives for the socket, but it does it a little differently, and there are two requirements:

  • You must be running a PHP-FPM package from Feb 1 2020 or later (which hopefully you are)
  • You must be running systemd versus sysvinit

The generic socket, located at /run/php/php-fpm.sock, is created by the systemd unit file by calling /usr/lib/php/php-fpm-socket-helper via the ExecStartPost and ExecStopPost directives. If you wish to use this socket, you still need to ensure your web server config is pointed to it versus the version-specific file.

The original answer is below if you don't meet those requirements.


It appears you are running a Debian/Ubuntu based distribution, which intentionally installs php-fpm in version specific locations (it's the whole reason you can use update-alternatives to switch the default version on the fly). However, there is no alternatives config for the socket files of php-fpm (your call to update-alternatives is only changing the CLI PHP version at /usr/bin/php).

The socket file to use is defined in /etc/php/8.3/fpm/pool.d/www.conf.

; The address on which to accept FastCGI requests.
; Valid syntaxes are:
;   'ip.add.re.ss:port'    - to listen on a TCP socket to a specific IPv4 address on
;                            a specific port;
;   '[ip:6:addr:ess]:port' - to listen on a TCP socket to a specific IPv6 address on
;                            a specific port;
;   'port'                 - to listen on a TCP socket to all addresses
;                            (IPv6 and IPv4-mapped) on a specific port;
;   '/path/to/unix/socket' - to listen on a unix socket.
; Note: This value is mandatory.
listen = /run/php/php8.3-fpm.sock

If you really want to have a version agnostic socket file, you can change that setting to just be listen = /run/php/php-fpm.sock and update your nginx config accordingly. You'll need to do this for every version of PHP you have installed, and it also means you cannot have two different php-fpm versions running simultaneously.

You might be able to play around with creating your own update-alternative config for /run/php/php-fpm.sock that allows you to switch between versions, but I'm not sure how well that will work trying to manage files in /run that way.

2 of 2
1

This does not seem to be a problem on Debian 11, and I can use both /run/php/php-fpm.sock as well as /etc/alternatives/php-fpm.sock.

# ls -la /run/php
total 20
drwxr-xr-x  2 www-data www-data 260 Mar 29 19:24 .
drwxr-xr-x 20 root     root     600 Mar 29 19:24 ..
-rw-r--r--  1 root     root       3 Mar 29 19:24 php5.6-fpm.pid
srw-rw----  1 www-data www-data   0 Mar 29 19:24 php5.6-fpm.sock
-rw-r--r--  1 root     root       3 Mar 29 19:24 php7.3-fpm.pid
srw-rw----  1 www-data www-data   0 Mar 29 19:24 php7.3-fpm.sock
-rw-r--r--  1 root     root       3 Mar 29 19:24 php7.4-fpm.pid
srw-rw----  1 www-data www-data   0 Mar 29 19:24 php7.4-fpm.sock
-rw-r--r--  1 root     root       3 Mar 29 19:24 php8.2-fpm.pid
srw-rw----  1 www-data www-data   0 Mar 29 19:24 php8.2-fpm.sock
-rw-r--r--  1 root     root       3 Mar 29 19:24 php8.3-fpm.pid
srw-rw----  1 www-data www-data   0 Mar 29 19:24 php8.3-fpm.sock
lrwxrwxrwx  1 root     root      30 Mar 29 19:24 php-fpm.sock -> /etc/alternatives/php-fpm.sock

See where /etc/alternatives/php-fpm.sock points to:

# stat /etc/alternatives/php-fpm.sock
  File: /etc/alternatives/php-fpm.sock -> /run/php/php8.3-fpm.sock

Let's reconfigure PHP to 5.6 using the below command. It will present you with available versions and prompt for a number.

update-alternatives --config php-fpm.sock

Check again:

# stat /etc/alternatives/php-fpm.sock
  File: /etc/alternatives/php-fpm.sock -> /run/php/php5.6-fpm.sock
🌐
GitHub
gist.github.com › claudiosanches › e8b252a8418225a088370725abc08ea6
Changing PHP version on Ubuntu · GitHub
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 sudo update-alternatives --set phpize /usr/bin/phpize7.4 sudo update-alternatives --set php-config /usr/bin/php-config7.4
🌐
DEV Community
dev.to › ibrahimalanshor › how-to-switch-between-installed-php-versions-on-linux-jga
How to Switch Between Installed PHP Versions on Linux - DEV Community
July 8, 2025 - sudo update-alternatives --config php # There are 4 choices for the alternative php (providing /usr/bin/php).
🌐
GitHub
yellowduck.be › posts › changing-the-default-php-version-in-ubuntu
🐥 Changing the default PHP version in Ubuntu
sudo update-alternatives --set php-config /usr/bin/php-config8.1 · You might also like: Fixing GPG errors when installing apt packages · Managing firewall rules with UFW on Linux · TIL: Removing old PHP versions after an upgrade · If this ...
🌐
Tutorials24x7
php.tutorials24x7.com › blog › how-to-switch-php-version-on-ubuntu-20-04-lts
How To Switch PHP Version on Ubuntu 20.04 LTS | Tutorials24x7
December 5, 2021 - We can configure multiple versions of PHP installed on Ubuntu 20.04 LTS using the commands as shown below. # PHP 7.0 sudo update-alternatives --set php /usr/bin/php7.0 sudo update-alternatives --set phar /usr/bin/phar7.0 sudo update-alternatives --set phar.phar /usr/bin/phar.phar7.0 # PHP 8.0 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
🌐
Medium
tamrakar-shreyaa.medium.com › how-to-switch-between-multiple-php-version-on-ubuntu-fdf6ea06c698
How to Switch between Multiple PHP Version on Ubuntu | by Tamrakar Shreyaa | Medium
August 21, 2021 - sudo update-alternatives --set php /usr/bin/php7.2 sudo update-alternatives --set phar /usr/bin/phar7.2 sudo update-alternatives --set phar.phar /usr/bin/phar.phar7.2 sudo update-alternatives --set phpize /usr/bin/phpize7.2 sudo update-alternatives --set php-config /usr/bin/php-config7.2
🌐
Jcarpizo
jcarpizo.github.io › multiple-php.html
Switch between Multiple PHP Version on Ubuntu — Jasper Carpizo
sudo a2dismod php5.6 sudo a2enmod php7.1 sudo service apache2 restart · sudo update-alternatives --set php /usr/bin/php7.1 sudo update-alternatives --set phar /usr/bin/phar7.1 sudo update-alternatives --set phar.phar /usr/bin/phar.phar7.1
🌐
GitHub
gist.github.com › akhileshdarjee › 5c9185dd0210ac96cdb279fc8d68a778
Change PHP version system wide · GitHub
Change PHP version system wide. GitHub Gist: instantly share code, notes, and snippets.