1. Introduction

PHP requirements may vary from one project to another. As a result, we sometimes have to install varying PHP versions on our machine. But then, the php command can only refer to one of the many installed PHP versions at a time. So, to ensure we’re using the right one whenever we run a script, we may have to switch between them.

In this tutorial, we discuss how to switch between PHP versions on the Linux command line interface.

2. Using update-alternatives

The update-alternatives command is a seamless way to switch between similar programs in Linux. True to its function, we can use it to switch between PHP versions on Linux CLI.

We can use update-alternatives in two ways: interactively and without interaction.

Say we have PHP 8.1 and PHP 8.2 installed on our Linux machine. 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

Running php -v confirms the PHP version is now 8.2:

$ php -v
PHP 8.2.12 (cli) (built: Nov 10 2023 09:45:28) (NTS)
...truncated...

In the update-alternatives –set command above, update-alternatives changes the php‘s binary to the file at the absolute path passed to it – /usr/bin/php8.2. So, if we want to switch back to PHP 8.1, we can follow a similar syntax, replacing the php8.2 absolute path with php8.1‘s path:

$ sudo update-alternatives --set php /usr/bin/php8.1
update-alternatives: using /usr/bin/php8.1 to provide /usr/bin/php (php) in manual mode

Then, we can confirm the PHP version again:

$ php -v
PHP 8.1.11 (cli) (built: Sep 29 2022 22:17:15) (NTS)
...truncated...

If we choose to switch the PHP versions interactively, we’ll use the –config subcommand instead of –set:

$ update-alternatives --config php
There are 2 choices for the alternative php (providing /usr/bin/php).

  Selection    Path             Priority   Status
------------------------------------------------------------
  0            /usr/bin/php8.2   82        auto mode
* 1            /usr/bin/php8.1   81        manual mode
  2            /usr/bin/php8.2   82        manual mode

Press <enter> to keep the current choice[*], or type selection number:

As seen above, we’re prompted to choose between PHP 8.1 and PHP 8.2 by entering a selection number. In responding to this prompt, if we want to switch to PHP 8.2, we’ll respond to the prompt with or 2.

Responding with returns php to auto mode. In auto mode, the binary with the highest priority is set as the program’s binary by default. But in manual mode, we control the program’s selected binary.

Besides switching between PHP versions, we can also get a list of PHP installations using update-alternatives‘s –list subcommand:

$ update-alternatives --list php
/usr/bin/php8.1
/usr/bin/php8.2

3. Using alias

Instead of switching between PHP versions multiple times while using update-alternatives, we can create an alias for each PHP version. This way, we never have to switch; we only have to use the corresponding alias for each one.

We can create an alias named php8.1 for our PHP 8.1 binary:

$ alias php8.1=/usr/bin/php8.1

Of course, we can do the same for the PHP 8.2 binary:

$ alias php8.2=/usr/bin/php8.2

With these aliases, any time we need to run a PHP script with version 8.1, we’ll use php8.1 as our base command. Then, when we need version 8.2, we’ll use php8.2 as our base command.

We’ll test this out with a PHP script that outputs Hello World:

$ cat baeldung.php
<?php
echo "Hello World\n";
?>

If we want to run our script, baeldung.php with PHP 8.1, all we have to do is use the php8.1 alias as our base command:

$ php8.1 baeldung.php
Hello World

The same thing applies when we want to run the script with PHP 8.2:

$ php8.2 baeldung.php
Hello World

To ensure our aliases persist across terminal sessions, we can add it to a Bash configuration file, .bashrc.

4. Conclusion

In this article, we discussed how to switch PHP versions using update-alternatives and alias. Then, we reviewed how to use update-alternatives in interactive mode while touching on the differences between auto mode and manual mode.

If we ever need to run multiple PHP versions without switching, alias might be more practical. But if we only need one PHP version for the most part, update-alternatives would be fitting.

Comments are closed on this article!