Programming Languages Bug Report

Ubuntu runs the wrong php or python3 after installing a new version: update-alternatives

Installing PHP 8.4 from a PPA silently moved our CLI to it while FPM stayed on 8.3, and an attempt to make python3 point at 3.13 broke apt. How Debian alternatives decide which binary wins, and the safe way to run several versions.

Hannah Okafor Hannah Okafor @hannahokafor · November 23, 2026 · 8 min read · 139 Views
611-ubuntu-multiple-php-python-update-alternatives

Two separate tickets landed on the platform team in the same week, both from Ubuntu 24.04 application servers.

The first came from the PHP team. After a colleague installed PHP 8.4 from the Ondrej Sury PPA to test an upgrade, scheduled artisan commands on a production box started failing, while web requests worked fine:

$ php -v
PHP 8.4.2 (cli) (NTS)
$ php artisan schedule:run
PHP Fatal error:  Uncaught Error: Class "Redis" not found in /var/www/app/vendor/laravel/framework/src/Illuminate/Redis/Connectors/PhpRedisConnector.php

The 8.4 CLI had none of the extensions the app needed, because only the base package had been installed for it.

The second came from a data engineer who wanted python3 to mean 3.13 on a worker. They installed it from the deadsnakes PPA and ran update-alternatives to repoint /usr/bin/python3. Afterwards apt itself started printing tracebacks:

Traceback (most recent call last):
  File "/usr/lib/cnf-update-db", line 3, in <module>
    import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'
Reading package lists... Done
E: Problem executing scripts APT::Update::Post-Invoke-Success 'if /usr/bin/test -w /var/lib/command-not-found/ -a -e /usr/lib/cnf-update-db; then /usr/lib/cnf-update-db > /dev/null; fi'
E: Sub-process returned an error code

Reproducing it

PHP, on a clean Ubuntu 24.04 with php8.3-cli installed:

sudo add-apt-repository ppa:ondrej/php
sudo apt install php8.4-cli
php -v                  # now 8.4

Python, on the same kind of machine (do not do this on a machine you care about):

sudo apt install python3.13
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.13 1
sudo apt update         # traceback from apt hook scripts

What we ruled out

  • PATH order. type -a php and type -a python3 showed only /usr/bin entries; nothing under /usr/local or a version manager.

  • FPM using the wrong version. systemctl status php8.3-fpm was active and Nginx still pointed at /run/php/php8.3-fpm.sock, which is why web traffic was fine.

  • A broken 3.13 build. /usr/bin/python3.13 -c "import ssl, sqlite3" worked. The interpreter was fine; it simply was not the one Ubuntu's own tools were built for.

update-alternatives --display php showed the actual mechanism:

php - auto mode
  link best version is /usr/bin/php8.4
  link currently points to /usr/bin/php8.4
  link php is /usr/bin/php
  slave php.1.gz is /usr/share/man/man1/php.1.gz
/usr/bin/php8.3 - priority 83
  slave php.1.gz: /usr/share/man/man1/php8.3.1.gz
/usr/bin/php8.4 - priority 84
  slave php.1.gz: /usr/share/man/man1/php8.4.1.gz

Root cause

Both problems come from the Debian alternatives system. For PHP, each phpX.Y-cli package registers itself as an alternative for /usr/bin/php with a priority derived from its version. The php group was in auto mode, so installing 8.4 made it the "best" alternative and the CLI switched immediately. FPM is a separate per-version service and did not change, so CLI and web ran different PHP versions with different extension sets.

For Python, /usr/bin/python3 on Ubuntu is not an alternative at all. It is a plain symlink owned by the python3-minimal package, pointing at the distribution's default interpreter (3.12 on 24.04). Ubuntu's own tools, including apt hooks, command-not-found, software-properties and netplan, import compiled modules such as apt_pkg built only for that version. Turning /usr/bin/python3 into an alternative pointing at 3.13 made those tools run on an interpreter that cannot load them.

What is happening under the hood

update-alternatives manages a symlink chain: /usr/bin/php points to /etc/alternatives/php, which points to the real binary such as /usr/bin/php8.3. A group can also carry "slave" links (man pages, related tools) that move together with the master link. Each candidate has a priority. In auto mode the highest priority wins and the choice changes whenever packages are installed or removed. In manual mode the admin's choice sticks until changed by hand.

PHP packages register several groups: php, phar, phar.phar, and with the -dev packages php-config and phpize. These need to agree, or pecl builds an extension against one version and installs it for another.

Python on Ubuntu deliberately does not use alternatives for python3. Versioned interpreters such as /usr/bin/python3.13 live alongside the default, and you call them by name.

Environment

Component

Version / value

OS

Ubuntu 24.04 LTS

PHP (distro default)

8.3 (php8.3-cli, php8.3-fpm)

PHP (PPA)

8.4 from ppa:ondrej/php, CLI only

Python (distro default)

3.12, /usr/bin/python3 from python3-minimal

Python (PPA)

3.13 from deadsnakes

Web stack

Nginx + php8.3-fpm socket

Minimal reproduction

readlink -f /usr/bin/php          # /usr/bin/php8.4 after installing 8.4
readlink -f /usr/bin/python3      # /usr/bin/python3.13 after the bad alternative
/usr/bin/python3 -c "import apt_pkg"

The last line raises ModuleNotFoundError: No module named 'apt_pkg'.

Who is affected

Any Debian or Ubuntu host where a second PHP version is installed from a PPA or Sury's repository, and any host where someone has turned /usr/bin/python3 into an alternative, a pattern that circulates widely in old tutorials. Hosts that run PHP in containers, or use pyenv, uv or mise for Python, are not affected because they never touch /usr/bin.

The fix

PHP: choose the CLI version explicitly and lock it.

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
php -v

--set puts the group into manual mode, so future PHP installs will not move it again. To pick interactively, sudo update-alternatives --config php lists candidates with numbers. To return to automatic selection, sudo update-alternatives --auto php.

When we really did want 8.4, we upgraded CLI and FPM together and installed the same extensions for both:

sudo apt install php8.4-fpm php8.4-mbstring php8.4-redis php8.4-mysql php8.4-xml php8.4-curl
sudo sed -i 's#php8.3-fpm.sock#php8.4-fpm.sock#' /etc/nginx/sites-available/app.conf
sudo nginx -t && sudo systemctl reload nginx
sudo update-alternatives --set php /usr/bin/php8.4

Cron jobs and supervisors now call a version explicitly (/usr/bin/php8.4 artisan ...), so a future alternatives change cannot move them without a config change.

Python: put the system python3 back and call 3.13 by name.

sudo update-alternatives --remove-all python3
sudo apt install --reinstall python3-minimal
readlink -f /usr/bin/python3      # /usr/bin/python3.12 again
sudo apt update                   # no tracebacks

Removing the group deletes the /usr/bin/python3 link the alternative created; reinstalling python3-minimal restores the distribution's symlink. Then use 3.13 through a virtual environment:

sudo apt install python3.13-venv
python3.13 -m venv /opt/worker/.venv
/opt/worker/.venv/bin/pip install -r requirements.txt

The worker's systemd unit runs /opt/worker/.venv/bin/python, never bare python3.

Alternatives and trade-offs

  • Containers per app. Each image carries its own PHP or Python; the host only runs Docker or Podman. Cleanest isolation, more moving parts.

  • uv, pyenv or mise for Python. Interpreters live in user directories and never touch /usr/bin. Good for workers and developer machines.

  • Wrapper scripts. A /usr/local/bin/app-php that execs /usr/bin/php8.4 documents intent, but is one more thing to keep in sync.

  • python-is-python3. Installs a python command pointing at the system python3. Fine, because it only adds a name for the default interpreter.

How to test the fix

update-alternatives --query php | grep -E "Status|Value"
php -v && php -m | grep -E "redis|mbstring"
php -r 'echo PHP_VERSION, PHP_EOL;'
curl -s localhost/health-php-version
/usr/bin/python3 -c "import apt_pkg, sys; print(sys.version)"

Our app exposes the FPM PHP version on an internal health endpoint, which made checking CLI and FPM side by side a single command.

Confirming in production

After applying the fix to the affected hosts, schedule:run succeeded on the next minute and the job logs showed no Unable to load dynamic library lines for 24 hours. On the worker, sudo apt update && sudo apt upgrade ran cleanly and unattended-upgrades logs showed successful runs again.

Regression guard

  • Our configuration management now sets php alternatives with --set explicitly on every PHP host, so auto mode is never relied on.

  • A monitoring check compares the CLI version (php -r 'echo PHP_MAJOR_VERSION.".".PHP_MINOR_VERSION;') with the FPM version from the health endpoint and alerts on mismatch.

  • A host check fails if readlink -f /usr/bin/python3 differs from the distribution default or if update-alternatives --query python3 returns a group at all.

Security implications

The Python breakage stopped unattended-upgrades on that worker for nine days without anyone noticing, so it missed security updates; the alerting we added watches for failed unattended runs as well. Third-party PPAs are trusted with root during installs, so we restrict them to the two maintainers above, pin them in configuration management and review them annually. Running CLI and FPM on different PHP versions also meant two sets of extension versions to patch; keeping them in step halves that work.

Prevention checklist

  • Set PHP alternatives explicitly with update-alternatives --set; avoid auto mode on servers.

  • Upgrade PHP CLI, FPM and extensions together, and call versioned binaries in cron and supervisors.

  • Never repoint /usr/bin/python3 on Debian or Ubuntu.

  • Use virtual environments or a user-level manager for non-default Python versions.

  • Alert on failed unattended-upgrades runs.

References

Add a label

#php #python #ubuntu #update-alternatives #apt
Written by
Hannah Okafor @hannahokafor

I am a frontend engineer who has worked on dashboards, booking flows and design systems, mostly in React and TypeScript. I care about the parts of the web platform that frameworks hide: forms, cookies, rendering cost and what the browser actually does with our code. The bugs I write about usually passed every review and only appeared with production data: a map with a million points, a name with an accent in a PDF, a cookie that one subdomain never sees. Each post shows a minimal reproduction, why the browser or framework behaves that way, and a fix with a test.

Advertisement

Comments

0

No comments yet. Be the first to comment!

Sign In to post a comment.