DEV Community

Ibrahim
Ibrahim

Posted on

How to Switch Between Installed PHP Versions on Linux

On a Linux system, it's possible to have multiple PHP versions installed. To see the list of installed PHP versions, use the sudo update-alternatives --list php command. For example:

sudo update-alternatives --list php
# /usr/bin/php5.6
# /usr/bin/php7.4
# /usr/bin/php8.2
# /usr/bin/php8.3
Enter fullscreen mode Exit fullscreen mode

As shown in the output, there are 4 installed PHP versions.

To check which PHP version is currently used by default, use the php --version command. For example:

php --version
# PHP 8.3.14 (cli) (built: Nov 25 2024 18:07:16) (NTS)
# Copyright (c) The PHP Group
# Zend Engine v4.3.14, Copyright (c) Zend Technologies
#     with Zend OPcache v8.3.14, Copyright (c), by Zend Technologies
Enter fullscreen mode Exit fullscreen mode

To switch the default PHP version, use the sudo update-alternatives --config php command. For example:

sudo update-alternatives --config php

# There are 4 choices for the alternative php (providing /usr/bin/php).

#   Selection    Path             Priority   Status
# ------------------------------------------------------------
#   0            /usr/bin/php8.3   83        auto mode
#   1            /usr/bin/php5.6   56        manual mode
#   2            /usr/bin/php7.4   74        manual mode
#   3            /usr/bin/php8.2   82        manual mode
# * 4            /usr/bin/php8.3   83        manual mode

# Press <enter> to keep the current choice[*], or type selection number: 
Enter fullscreen mode Exit fullscreen mode

The output will be a list of installed PHP versions along with their selection numbers.

There is also an input prompt below the list to type the selection number of the PHP version you want to set as the default.

For example, I will select number 2 to make PHP 7.4 the default version.

Press <enter> to keep the current choice[*], or type selection number: 2
Enter fullscreen mode Exit fullscreen mode

After successfully running the command, check the current default PHP version:

php --version
# PHP 7.4.33 (cli) (built: Nov 24 2024 08:40:54) ( NTS )
# Copyright (c) The PHP Group
# Zend Engine v3.4.0, Copyright (c) Zend Technologies
#     with Zend OPcache v7.4.33, Copyright (c), by Zend Technologies
Enter fullscreen mode Exit fullscreen mode

Now, the default PHP version has been changed to 7.4.

Top comments (2)

Collapse
 
ravavyr profile image
Ravavyr

"update-alternatives" is only on Debian-based Operating Systems (e.g. Ubuntu, Debian).

It's available by default on: Ubuntu, Debian, Linux Mint, etc.
but it's not available on: RHEL, CentOS, Fedora, Arch, etc.

if it's not available, look up your operating system and how to swap php versions for that OS

Collapse
 
ibrahimalanshor profile image
Ibrahim

Yeah, I forgot to mention it’s for Debian-based Linux. Thanks