How m Detects Linux Distribution and Selects the Correct MongoDB Binary

m uses a two-stage detection process involving the get_distro_and_arch function to identify the OS and normalize distribution names, then iterates through a curated list of MongoDB-compatible distro identifiers in install_server to find the first available binary URL.

The m MongoDB version manager by aheckmann automates the complex task of matching your Linux distribution to the correct pre-compiled MongoDB binary. Understanding how m detects Linux distribution and selects the correct MongoDB binary reveals the sophisticated mapping layer that bridges generic OS detection with MongoDB's specific packaging taxonomy. This article examines the source code in bin/m to trace the exact mechanism from system detection to tarball download.

Distribution Detection Architecture in m

OS and Architecture Detection

The detection pipeline begins in the get_distro_and_arch function located in bin/m. This function first captures the base platform using standard Unix utilities.


# From bin/m around line 1160

local os=$(uname -s | tr '[:upper:]' '[:lower:]')
local arch=$(uname -m)

The script normalizes uname output into canonical values: linux for Linux kernels and x86_64 (or aarch64) for architectures. This establishes the foundation variables $os and $arch used throughout the selection process.

Linux Distribution Identification

For Linux hosts, m employs a cascading fallback strategy to identify the specific distribution. The code checks multiple system files in order of reliability:

  • lsb_release -si and lsb_release -sr for LSB-compliant systems
  • /etc/lsb-release as a secondary source
  • /etc/debian_version for Debian derivatives
  • /etc/os-release as the final fallback

This logic appears in the distribution detection block of bin/m (lines 1160-1172), where the script extracts distro_id and distro_version components.

Normalizing Distribution Names

Raw distribution IDs often differ from MongoDB's naming convention. The script maintains a normalization mapping that converts detected IDs to MongoDB-compatible identifiers:


# Mapping examples from bin/m around lines 1264-1270

case "$distro_id" in
  almalinux|rocky|centos|fedora|rhel) distro_id="rhel" ;;
  opensuse|sles) distro_id="suse" ;;
  pop) distro_id="ubuntu" ;;
  linuxmint) distro_id="ubuntu" ;;
esac

After normalization, m concatenates the ID and version into a standardized $distro variable (e.g., ubuntu2004, rhel8, debian11) that matches MongoDB's release taxonomy.

Binary Selection Logic in install_server

Mapping Detected Distros to MongoDB Names

Once detection completes, the install_server function (starting around line 1080 in bin/m) translates the detected $distro into a prioritized list of compatible MongoDB distribution identifiers stored in the $distros variable.

case "$distro" in
  debian-9*)  distros="debian92 debian81" ;;
  debian-10*) distros="debian10 debian92" ;;
  debian-11*) distros="debian11 debian92" ;;
  ubuntu-20*) distros="ubuntu2004" ;;
  ubuntu-22*) distros="ubuntu2204" ;;
  rhel-8*)    distros="rhel80" ;;
  # ... additional mappings

esac

This case statement accounts for MongoDB's specific packaging history, where newer binaries often work on older compatible systems (hence the fallback chains like debian11 debian92).

URL Probing and Fallback Mechanism

The selection algorithm iterates through the $distros list, constructing candidate URLs and testing their availability using the good function (which performs an HTTP HEAD request):

for distro in $distros; do
  if good "http://downloads.mongodb.com/$os/mongodb-$os-$arch-enterprise-$distro-$version.tgz"; then
    dist=$distro
    tarball="mongodb-$os-$arch-enterprise-$distro-$version.tgz"
    break
  fi
done

If no specific distro URL responds successfully, m falls back to generic binaries from fastdl.mongodb.org (lines 1120-1130 in bin/m), ensuring installation proceeds even on unrecognized distributions.

Practical Examples of m's Detection

To observe the detection mechanism in action, you can inspect m's internal variables during an installation:


# Install MongoDB 6.0 on the current system

$ m install 6.0

# With debug output (if M_DEBUG is supported) or by examining the script:

$ bash -x $(which m) install 6.0 2>&1 | grep -E "(distro|arch|os)"

The script will show the progression from raw uname output through normalized distribution names to the final tarball selection. For example, on Ubuntu 20.04, the trace reveals:


+ os=linux
+ arch=x86_64
+ distro_id=ubuntu
+ distro_version=20.04
+ distro=ubuntu2004
+ distros=ubuntu2004
+ tarball=mongodb-linux-x86_64-enterprise-ubuntu2004-6.0.12.tgz

Summary

  • m detects Linux distribution through the get_distro_and_arch function in bin/m, which queries uname, lsb_release, and /etc/os-release to establish system identity.
  • Distribution names are normalized to match MongoDB's taxonomy (e.g., converting pop to ubuntu or almalinux to rhel) before being stored in the $distro variable.
  • Binary selection occurs in install_server, which maps detected distributions to prioritized lists of MongoDB-compatible identifiers and probes each URL until finding a valid tarball.
  • Fallback mechanisms ensure that if no specific distro binary exists, m attempts generic downloads from the fastdl mirror.

Frequently Asked Questions

What files does m check to determine the Linux distribution?

m checks lsb_release first, then falls back to /etc/lsb-release, /etc/debian_version, and finally /etc/os-release to extract the distribution ID and version numbers.

How does m handle distributions that MongoDB doesn't officially support?

For unrecognized or newly released distributions, m normalizes the ID to the closest equivalent (such as mapping almalinux to rhel) and attempts to use compatible binaries. If specific distro URLs fail, it falls back to generic Linux binaries from the fastdl repository.

Where in the source code is the binary URL constructed?

The URL construction occurs in the install_server function within bin/m (around lines 1083-1087), where the script assembles the string http://downloads.mongodb.com/$os/mongodb-$os-$arch-enterprise-$distro-$version.tgz and tests it with the good function.

Can I override m's automatic distribution detection?

While m does not expose a direct command-line flag to force a specific distribution identifier, you can modify the $distro variable in the script or set environment variables that affect the detection logic, though this requires editing bin/m directly.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →