Change from update-alternatives command not taking effect

I tried to change my PHP CLI version with sudo update-alternatives --config php:

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

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

I set it to 0 and checked it with update-alternatives --display php:

php - auto mode
  link best version is /usr/bin/php8.1
  link currently points to /usr/bin/php8.1
  link php is /usr/bin/php
  slave php.1.gz is /usr/share/man/man1/php.1.gz
/usr/bin/php8.0.15 - priority 1
/usr/bin/php8.1 - priority 81
  slave php.1.gz: /usr/share/man/man1/php8.1.1.gz

It looks like it worked, but if I check it with php -v then I get:

PHP 8.0.15 (cli) (built: Jun  7 2022 10:21:21) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.15, Copyright (c) Zend Technologies

command -v php outputs /usr/local/bin/php.

I’m using Ubuntu 22.04 LTS

Asked By: Black


You presumably have /usr/local/bin ahead of /usr/bin in your PATH, and since there’s a php binary in /usr/local/bin, your shell uses that. The php alternative configured by update-alternatives ends up having no effect — at least, on anything based on that PATH.

To fix this, in your case, I recommend removing the PHP installation in /usr/local. A simpler approach would be to re-order your PATH, but that might affect other binaries you rely on in /usr/local/bin.

Answered By: Stephen Kitt
Categories: Answers Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.