How to Get the Binary Path for an Installed MongoDB Version Using m
Use m bin <version> or its alias m which <version> to print the absolute file system path to the MongoDB binaries for any version installed via the m version manager.
The m tool by Aaron Heckmann (aheckmann/m) is a lightweight MongoDB version manager that installs server binaries under a structured directory hierarchy. When automating deployments or scripting database operations, you often need the full path to a specific version's mongod or mongo executable rather than relying on the active shell session's PATH.
How m bin Resolves the Binary Path
The m bin command delegates to the internal bash function display_bin_path_for_version defined in bin/m (source lines 78-87). This function performs four distinct operations to locate and validate the requested binaries.
Argument Validation and Version Resolution
First, the function ensures a version argument was supplied, aborting with "version required" if the argument is empty (lines 78-80). It then normalizes the input by stripping a leading v and passing the remainder to get_latest_installed_version (lines 80-81, 94-122). This helper scans the $M_PREFIX/m/versions directory to resolve partial series identifiers—such as 4.4 or 6.0—into the latest installed patch release (e.g., 4.4.29 or 6.0.12).
Path Construction and Existence Verification
With the exact version string determined, the function constructs the binary directory path (lines 81-82):
local bin=$VERSIONS_DIR/$version/bin
Where VERSIONS_DIR expands to $M_PREFIX/m/versions by default. Before returning the path, the script verifies that $bin/mongod exists as a regular file. If the executable is missing, it invokes abort_not_installed with the original argument (lines 83-87).
CLI Usage Examples
Display Binary Path for a Specific Version
# Exact version
$ m bin 5.0.12
/home/user/.local/m/versions/5.0.12/bin
# Using the alias
$ m which 5.0.12
/home/user/.local/m/versions/5.0.12/bin
Resolve Partial Version Series
When you specify a release series rather than a full semver string, m automatically selects the newest installed patch:
$ m bin 4.4
/home/user/.local/m/versions/4.4.29/bin
Scripting with the Binary Path
Capture the output to invoke a specific MongoDB version directly without modifying your shell environment:
#!/usr/bin/env bash
VERSION="6.0"
BIN_PATH=$(m bin "$VERSION")
# Start mongod using the resolved path
"$BIN_PATH/mongod" --config /etc/mongod.conf --dbpath /data/db
Handling Missing Installations
If the requested version is not present in the versions directory, the command exits with an error:
$ m bin 4.2.0
Error: requested MongoDB version is not installed
Try 'm 4.2.0' to download and activate.
Summary
- Primary command:
m bin <version>(orm which <version>) returns the absolute path to thebindirectory for any installed MongoDB version managed bym. - Implementation: The logic resides in
display_bin_path_for_versioninsidebin/m(lines 78-87), which leveragesget_latest_installed_version(lines 94-122) to handle fuzzy version matching. - Storage convention: Binaries are stored under
$M_PREFIX/m/versions/<version>/bin, whereM_PREFIXdefaults to your user's local directory. - Validation: The command verifies that
mongodexists within the calculated directory before printing the path, preventing errors from stale or incomplete installations.
Frequently Asked Questions
What is the difference between m bin and m which?
There is no functional difference; which is simply an alias for bin. Both commands invoke the same display_bin_path_for_version function in bin/m and produce identical output. Use whichever is more intuitive for your workflow.
Can I use partial version numbers with m bin?
Yes. The get_latest_installed_version helper accepts series identifiers like 5.0 or 6.0 and resolves them to the highest installed patch release within that series. This allows scripts to target "the latest 5.0 I have installed" without hardcoding specific minor versions.
What happens if the MongoDB version is not installed?
The command checks for the existence of mongod within the calculated path. If the file is absent, it calls abort_not_installed and exits with the message "Error: requested MongoDB version is not installed", followed by a suggestion to run m <version> to download it.
How do I use the binary path in a shell script?
Use command substitution to capture the path into a variable, then reference the specific executable you need: BIN_PATH=$(m bin 6.0) followed by "$BIN_PATH/mongod" [options]. This technique is useful for CI/CD pipelines or init scripts that must launch a specific MongoDB version without relying on the global PATH.
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 →