How to Customize the Binary Symlink Directory in m (MongoDB Version Manager)
Set the M_BIN_DIR environment variable before running m to change where MongoDB binary symlinks are created, or use M_PREFIX to relocate the entire m installation hierarchy.
The m version manager for MongoDB stores downloaded binaries in a versioned cache while exposing them through symlinks placed in a dedicated binary directory. By default, these symlinks land in $HOME/.local/bin, but the aheckmann/m repository provides flexible environment variables to customize this behavior. Whether you need to avoid permission conflicts or integrate with system-wide PATH directories, customizing the binary symlink directory ensures seamless access to your managed MongoDB versions.
Understanding the Default Symlink Directory
In the main script bin/m, the configuration block at lines 14–16 defines three critical path variables:
# Parent directory
M_PREFIX=${M_PREFIX-$HOME/.local}
# Top level directory for files managed by `m`
M_DIR=$M_PREFIX/m
# Directory to symlink binaries into
M_BIN_DIR=${M_BIN_DIR-$M_PREFIX/bin}
M_PREFIX serves as the root for all m data, defaulting to $HOME/.local. M_BIN_DIR determines where symlinks are created, falling back to $M_PREFIX/bin (typically $HOME/.local/bin) when not explicitly set. This design separates the heavy versioned binaries stored in $M_DIR from the lightweight symlinks that need to reside on your executable path.
How m Creates Binary Symlinks
When you activate or install a MongoDB version, the install_mongo function delegates to install_bin, which executes the symlink creation at lines 991–996:
cd $dir \
&& ln -fs $dir/bin/!($tools_glob) $M_BIN_DIR \
&& post change
This command creates symbolic links for all binaries in the downloaded version's bin directory—except those matching the internal tools_glob pattern—pointing them into $M_BIN_DIR. The -f flag forces overwriting existing symlinks, ensuring seamless version switching without manual cleanup.
Methods to Customize the Binary Symlink Directory
You can override the default symlink location using two complementary environment variables.
Using M_BIN_DIR for Direct Control
Set M_BIN_DIR to specify exactly where symlinks should appear. This variable takes precedence over all other path calculations:
# Create symlinks in a custom location
export M_BIN_DIR=$HOME/custom-m-bin
mkdir -p "$M_BIN_DIR"
m 7.0.14
After activation, executables like mongod and mongosh appear as symlinks in $HOME/custom-m-bin rather than the default location.
Using M_PREFIX for Global Relocation
Set M_PREFIX to move the entire m ecosystem—both the versioned binary cache and the symlink directory—to a new root:
# Relocate everything to /opt/m
export M_PREFIX=/opt/m
mkdir -p "$M_PREFIX/bin"
m stable
When M_BIN_DIR is unset, this configuration places symlinks in $M_PREFIX/bin (e.g., /opt/m/bin). You can combine both variables to store data in one location while placing symlinks elsewhere:
export M_PREFIX=/srv/m-cache
export M_BIN_DIR=/usr/local/bin
m 8.0.6
Practical Configuration Examples
One-Off Activation Without Environment Changes
Run a specific version with a custom binary directory without modifying your shell profile:
M_BIN_DIR=/tmp/m-test m 6.0.15
This temporary override affects only the current invocation, creating symlinks in /tmp/m-test that disappear when you delete the directory.
System-Wide Installation with User Symlinks
For multi-user systems where administrators manage versions but users control their PATH:
# As admin: install to shared location
sudo M_PREFIX=/opt/m m 7.0.14
# As user: point to personal bin directory
export M_BIN_DIR=$HOME/bin
m 7.0.14
Integration with Existing PATH Directories
If your system already sources /usr/local/bin or another standard directory:
sudo mkdir -p /usr/local/m-bin
sudo chown "$(whoami)" /usr/local/m-bin
export M_BIN_DIR=/usr/local/m-bin
m latest
Now MongoDB binaries are immediately available system-wide without modifying PATH variables.
Why Customize the Binary Symlink Directory?
Separate storage from execution: Keep large versioned binaries in a private cache ($M_PREFIX/m) while exposing only lightweight symlinks in a standard system path like /usr/local/bin or /opt/bin.
Avoid permission conflicts: If you lack write access to the default $HOME/.local/bin or it doesn't exist on your system's PATH, redirect M_BIN_DIR to a directory you control and ensure it's added to your shell configuration.
Support multi-user deployments: System administrators can maintain a shared $M_PREFIX (e.g., /opt/m) containing all MongoDB versions, while individual users set personal M_BIN_DIR values in their home directories to activate specific versions without affecting other users.
Summary
M_BIN_DIRdirectly controls where binary symlinks are created, overriding all other path settings.M_PREFIXrelocates the entiremhierarchy; whenM_BIN_DIRis unset, symlinks default to$M_PREFIX/bin.- Symlinks are created via
ln -fsin theinstall_binfunction at lines 991–996 ofbin/m. - Environment variables can be exported permanently in shell profiles or prepended to single commands for temporary overrides.
- Customization enables separation of version storage from executable access, resolves permission issues, and supports complex multi-user workflows.
Frequently Asked Questions
What is the default binary symlink directory in m?
By default, m creates binary symlinks in $HOME/.local/bin. This is determined by the default M_PREFIX value of $HOME/.local and the fallback logic M_BIN_DIR=${M_BIN_DIR-$M_PREFIX/bin} defined at lines 14–16 of bin/m.
Can I change the symlink directory without moving the downloaded binaries?
Yes. Set only the M_BIN_DIR environment variable while leaving M_PREFIX unchanged. This places symlinks in your specified directory while keeping the actual MongoDB versions stored in the default $HOME/.local/m location (or wherever M_PREFIX points).
How do I make the custom binary directory permanent?
Add the export statement to your shell configuration file (e.g., ~/.bashrc, ~/.zshrc, or ~/.bash_profile):
export M_BIN_DIR=$HOME/bin
export PATH="$M_BIN_DIR:$PATH"
After reloading your shell or sourcing the file, all future m commands will use this directory, and the binaries will remain available in your PATH across sessions.
Why are some MongoDB tools not symlinked to M_BIN_DIR?
The symlink creation command at lines 991–996 uses the exclusion pattern !($tools_glob) to skip certain internal utility binaries. This prevents cluttering your PATH with tools intended for internal m operations while ensuring primary executables like mongod, mongos, and mongosh are readily accessible.
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 →