How to Install and Manage mongosh (MongoDB Shell) Independently with m
TLDR: Use m mongosh ls to browse available versions, m mongosh 2.3.7 to install and activate a specific release, and m mongosh rm <version> to clean up old binaries, with m handling downloads, extraction to isolated directories, and symlink management in ~/.local/bin.
The m tool by Aaron Heckmann is a lightweight Bash-based version manager for the MongoDB ecosystem. Unlike monolithic installers, m treats mongosh (MongoDB Shell) as a first-class citizen, allowing you to install and manage mongosh independently from the database server itself. This separation ensures your shell tooling remains reproducible and version-specific without cluttering your system PATH.
Architecture of mongosh Management in m
All mongosh-related functionality resides in bin/m and revolves around environment variables that enforce strict isolation between components.
Configuration and Directory Structure
At the top of bin/m (lines 8-25), the script defines critical paths that keep mongosh separate from server binaries. The $SHELL_DIR variable (defaulting to $HOME/.local/m/shell/versions) stores downloaded mongosh archives, while $M_BIN_DIR (defaulting to $HOME/.local/bin) holds the active executable symlinks. The $M_PREFIX and $M_DIR variables determine the root installation context, ensuring that mongosh binaries never mix with the legacy mongo shell or server tools.
Command Dispatch via handle_mongosh
When you invoke m mongosh, the handle_mongosh function (lines 78-92 in bin/m) parses subcommands and delegates to specialized handlers. This dispatcher routes ls to list_shell_versions, version strings to install_shell, and rm to remove_shell_versions, providing a consistent CLI interface while keeping implementation details modular.
Installing mongosh Versions
The installation pipeline uses install_shell as a wrapper that checks the current active version before triggering install_shell_bin to handle the actual download and extraction.
Listing Available Releases
To view all published mongosh versions without downloading them, use the list command. The list_shell_versions function (lines 73-82) queries the MongoDB GitHub API, sorts semantic versions, and prints a human-readable list to stdout.
m mongosh ls
Installing Specific Versions
Install any version by passing it as an argument. The install_shell function (lines 124-149) checks if the requested version is already active, then calls install_shell_bin (lines 84-110) to construct the download URL. This helper detects your platform (linux or darwin), appends the x64 architecture identifier, and fetches the archive from https://downloads.mongodb.com/compass/. It extracts the binary to $SHELL_DIR/<version> and creates a symlink at $M_BIN_DIR/mongosh, instantly activating the new version.
m mongosh 2.3.7
Installing the Latest Stable Release
To automatically fetch and activate the highest stable version, use the stable keyword. The function queries GitHub tags, identifies the latest release, and executes the standard installation pipeline.
m mongosh stable
Managing Active mongosh Installations
Once installed, versions persist in $SHELL_DIR until explicitly removed, allowing instant switching via symlink updates.
Displaying Installed Versions
Running m mongosh without arguments invokes display_shell_versions (lines 101-119), which scans $SHELL_DIR for installed versions and marks the active one with a checkmark (✔).
m mongosh
Removing Old Versions
Clean up disk space by deleting specific versions with the rm subcommand. The remove_shell_versions function (lines 150-172) deletes the directory under $SHELL_DIR for each specified version. If you remove the currently active version, the function clears the symlink in $M_BIN_DIR and warns you to select a new default.
m mongosh rm 2.2.4 2.2.5
Running One-Off Commands Without Switching Versions
For CI/CD pipelines or testing, you can execute a specific mongosh version without altering the global symlink. The execute_shell_with_version function (lines 70-80) locates the binary directly in its version directory ($SHELL_DIR/<version>/bin/mongosh) and runs it with your provided arguments. If the requested version is missing, m falls back to the legacy mongo shell to ensure scripts remain compatible.
m shell 2.3.6 --eval "db.version()"
Summary
- Isolation: mongosh binaries live in $SHELL_DIR, completely separate from MongoDB Server and Database Tools installations.
- Symlink activation: The active version is controlled via a symlink in $M_BIN_DIR, enabling instant switching without modifying your shell profile.
- Platform detection: The
install_shell_binfunction automatically selects Linux or macOS packages but currently targets x64 architectures exclusively. - Flexible workflows: Use
m mongosh lsto browse,m mongosh <version>to install, andm mongosh rmto clean up specific releases. - Fallback safety: The
m shellcommand provides one-off execution with automatic fallback to the legacymongoshell if the specified mongosh version is absent.
Frequently Asked Questions
How does m isolate mongosh from MongoDB Server binaries?
According to the bin/m source code, m uses distinct directory variables: $SHELL_DIR stores mongosh versions while separate path logic handles server binaries in different subdirectories of $M_PREFIX. This architecture ensures that installing mongosh 2.3.7 never interferes with your MongoDB Server 7.0 installation, and both can be activated independently via symlinks in $M_BIN_DIR.
Can I run a specific mongosh version without changing the global default?
Yes. The execute_shell_with_version function in bin/m supports the m shell <version> syntax, which executes the binary directly from its versioned directory under $SHELL_DIR without updating the $M_BIN_DIR/mongosh symlink. This allows you to test scripts against multiple shell versions in the same terminal session.
What happens if the requested mongosh version is missing?
As implemented in execute_shell_with_version (lines 70-80), m first checks for the requested version in $SHELL_DIR. If the binary is not found, the function automatically falls back to the legacy mongo shell. This ensures that automation scripts continue to run on systems where mongosh has not been explicitly installed via m.
Does m support ARM64 architectures for mongosh?
Currently, the install_shell_bin function hardcodes the x64 architecture identifier when constructing download URLs for both Linux and macOS packages. While the underlying MongoDB Compass downloads may offer ARM64 builds, m specifically requests the x64 variants as implemented in the URL building logic within bin/m.
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:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →