Website Vidya
Linux & ServersStep-by-Step Guide

update-alternatives Command on Ubuntu and Debian

Master the update-alternatives command on ubuntu with step-by-step examples for managing multiple Java, Python, and editor software versions.

RR
Rajesh RayLead Systems Educator
Published:
5 min read
PDF Cheat Sheet
update-alternatives Command on Ubuntu and Debian
AI Executive SummaryWebsiteVidya AI

5 min read

Master the update-alternatives command on ubuntu with step-by-step examples for managing multiple Java, Python, and editor software versions.

Key Highlights
  • Prerequisites and Supported Releases
  • Step 1: Understanding the Alternatives System Architecture
  • Step 2: Displaying Information with --display and --query
Grounded in full engineering runbookWebsiteVidya AI Engine

In modern Linux computing, multiple software packages frequently deliver the exact same functional role or provide competing versions of a shared executable.

For example, a developer workstation might have OpenJDK 17, OpenJDK 21, and Oracle Java installed simultaneously, or multiple Python interpreters (Python 3.11 and 3.12) alongside multiple terminal text editors like Nano, Vim, and Emacs.

Rather than hardcoding absolute binary paths or breaking dependent system scripts, Debian and Ubuntu rely on the update-alternatives system.

In this guide, we master the update-alternatives command on ubuntu with practical, reproducible terminal commands for managing multiple runtime versions, querying symlink states, and registering custom binaries.

Advertisement

Prerequisites and Supported Releases

This guide is thoroughly verified on Ubuntu 24.04 LTS (Noble Numbat) & Ubuntu 22.04 LTS. The instructions apply cleanly to cloud instances (AWS EC2, Google Compute Engine, DigitalOcean, Hetzner), bare-metal servers, and local virtual machines.

Before proceeding, ensure you have the following requirements satisfied:

  • A computer or server running Ubuntu 24.04 LTS, Ubuntu 22.04 LTS, or any Debian-based distribution.
  • A user account with sudo administrative privileges for modifying system alternatives.
  • Basic familiarity with the terminal command line.

Step 1: Understanding the Alternatives System Architecture

The Debian alternatives system operates using a two-tier symbolic link design that decouples generic program names from specific binary implementations.

Advertisement

When you execute a generic command like /usr/bin/editor or /usr/bin/java, that command is not a physical binary; it is a symlink pointing to an entry in /etc/alternatives/.

The file in /etc/alternatives/ is, in turn, a symlink pointing to the actual binary (for example, /usr/bin/nano or /usr/lib/jvm/java-21-openjdk-amd64/bin/java).

This modular indirection allows administrators to switch the system-wide default software with a single command without modifying application code or shell path variables.

Step 2: Displaying Information with --display and --query

Before switching or modifying alternatives, inspect the current state and registered candidates for a specific generic name.

Advertisement

Use the --display flag to view all available candidates, their assigned priority values, and whether the group is operating in auto or manual mode:

Run the display command for a common alternative like editor:

# Display all registered candidates for the 'editor' alternative:
update-alternatives --display editor

# Expected output confirms:
# editor - auto mode
#   link best version is /bin/nano
#   link currently points to /bin/nano
#   link editor is /usr/bin/editor
# /bin/ed - priority -100
# /bin/nano - priority 40
# /usr/bin/vim.basic - priority 30

# Machine-parseable query format:
update-alternatives --query editor

Step 3: Interactively Selecting Default Programs with --config

The most common use of update-alternatives is interactively choosing the default software for a generic command.

Pass the --config flag followed by the generic master name:

A numbered interactive menu lists each installed candidate; simply type the selection number and press Enter:

# Interactively select the default text editor:
sudo update-alternatives --config editor

# Terminal interactive menu snippet:
# There are 3 choices for the alternative editor (providing /usr/bin/editor).
#
#   Selection    Path                Priority   Status
# ------------------------------------------------------------
# * 0            /bin/nano            40        auto mode
#   1            /bin/ed             -100       manual mode
#   2            /bin/nano            40        manual mode
#   3            /usr/bin/vim.basic   30        manual mode
#
# Press <enter> to keep the current choice[*], or type selection number: 3

Step 4: Registering Custom Program Alternatives with --install

When you install custom software manually or compile newer runtimes from source, they are not registered in the alternatives system automatically.

Use the --install option to register a new candidate into an existing or new alternative group.

The syntax requires four arguments: link (generic path), name (group name), path (real binary), and priority (higher integer wins auto mode):

Here is an example registering multiple Python versions:

# Syntax: update-alternatives --install <link> <name> <path> <priority>

# Register Python 3.12 with priority 2:
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.12 2

# Register Python 3.11 with priority 1:
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.11 1

# Verify registered python3 alternative:
update-alternatives --display python3

Many applications have associated secondary files—such as manpages, compiler tools, or documentation—that must change synchronously whenever the master binary changes.

These secondary associations are called slave links.

When registering a master binary, attach slave links using the --slave flag:

When the master alternative is switched, systemd updates all associated slave links automatically:

# Register a custom editor with an associated slave manpage:
sudo update-alternatives --install /usr/bin/editor editor /usr/local/bin/myeditor 50 \
  --slave /usr/share/man/man1/editor.1.gz editor.1.gz /usr/local/share/man/man1/myeditor.1.gz

Step 6: Removing Alternative Registrations with --remove

If you uninstall or delete a custom binary, remove its entry from the alternatives database to prevent broken dangling symlinks.

Use --remove to unregister a specific binary path from a group:

To remove an entire alternative group completely, use --remove-all:

# Remove a specific binary candidate from the python3 group:
sudo update-alternatives --remove python3 /usr/bin/python3.11

# Remove all candidates for an entire alternative group:
sudo update-alternatives --remove-all my_custom_tool

# Return an alternative to automatic selection mode:
sudo update-alternatives --auto editor

Troubleshooting and Common Errors

When configuring Ubuntu servers, unexpected permission errors, network timeouts, or lock contention can occur. Review these common issues and verified resolutions:

update-alternatives: error: alternative [name] can't be master: it is a slave of [other_name]

Cause: Attempting to manage a slave link directly as a top-level alternative.

Resolution: Configure the master alternative that owns the slave link instead.

update-alternatives --display [master_name]

update-alternatives: error: alternative path [path] doesn't exist

Cause: The target binary specified in the --install command does not exist on disk.

Resolution: Verify the absolute path to the binary using ls -l [path] or which [binary] before registering.

ls -la /usr/bin/python*

Shell still executes old version after update-alternatives --config

Cause: The active bash shell has cached the binary location in its hash table.

Resolution: Clear the shell binary lookup hash cache using hash -r.

hash -r

Security Best Practices and Hardening

Never register untrusted or world-writable binaries in update-alternatives, as root cron jobs and system scripts executing generic commands could be compromised.

Verify that slave links point to legitimate documentation to prevent manpage spoofing.

Exercise caution when changing the default python3 alternative on Ubuntu, as core system utilities like APT depend on specific library versions.

Deepen your Linux system administration skills with our related production-tested guides:

Authoritative References and Official Documentation

Frequently Asked Questions

Quick answers to common questions and configurations

4 FAQs
In auto mode, the system automatically selects the candidate with the highest numerical priority value. In manual mode, the administrator explicitly chooses a specific candidate (via --config or --set), and package updates will not override the administrator's choice.
Run sudo update-alternatives --auto [name] (e.g., sudo update-alternatives --auto editor). The system will immediately select the candidate with the highest priority.
The administrative state database resides in /var/lib/dpkg/alternatives/, while the symbolic links reside in /etc/alternatives/.
Bash caches the locations of previously executed commands in an internal memory hash table. Running hash -r clears this cache, ensuring the shell resolves the newly symlinked binary immediately.
RR
Written byVerified Author

Rajesh Ray

I’m Rajesh Rai, a full-stack developer, Linux administrator, and technology enthusiast passionate about making complex technology easier to understand and use.

SPAM-FREE MODERATED DISCOURSE

Join the Conversation & Perspective

Have thoughts, counterpoints, or reflections on this story? Engage directly with fellow readers and editorial contributors.

AT
MP
JV
+2

No comments on this guide yet. Be the first to share your thoughts or ask a question!

Recommended Reading10 Guides

Related Technical Guides

Tested runbooks, server administration walkthroughs, and troubleshooting guides

Featured Infrastructure PartnerUp to 75% OFF + 3 Months Free

Supercharge Your Linux & Cloud Workloads with Hostinger

Deploy high-speed NVMe KVM VPS, Docker containers, and LiteSpeed-accelerated WordPress servers in under 60 seconds with full root access, dedicated IPv4, and 24/7 technical support.

Pure NVMe SSD StorageFull Root Access & SSHFree Domain & Unlimited SSL99.9% Uptime SLA
Plans from$2.99/mo
Claim Hostinger DealAffiliate link • Special discount applied at checkout