# How to Change the Default Installation Directory for MongoDB Versions in m

> Easily change the default MongoDB installation directory in m by setting the M_PREFIX environment variable. Learn how to customize your MongoDB install location now.

- Repository: [Aaron Heckmann/m](https://github.com/aheckmann/m)
- Tags: how-to-guide
- Published: 2026-02-23

---

**Set the `M_PREFIX` environment variable to your desired parent directory before running `m` to relocate MongoDB binaries from the default `$HOME/.local/m` to any custom location.**

The `m` tool by aheckmann simplifies MongoDB version management, but by default it installs all binaries under your home directory. If you need to change the default installation directory to a different partition, shared network location, or system-wide path, you can override the behavior using environment variables defined in the source code.

## Understanding the Default Path Structure

According to `bin/m` (lines 9-15), `m` determines its root storage location using the `M_PREFIX` environment variable. When unset, `m` falls back to `$HOME/.local` and creates a subdirectory named `m` to house all files.

Under this prefix, the tool organizes downloaded artifacts as follows:

- `<M_PREFIX>/m/versions/` – MongoDB server binaries (`mongod`, `mongos`)
- `<M_PREFIX>/m/tools/` – Database Tools (`mongodump`, `mongorestore`, `mongoimport`)
- `<M_PREFIX>/m/shell/` – MongoDB Shell (`mongosh`)

The symbolic links to active binaries are placed in `<M_PREFIX>/bin` by default, though this destination is separately configurable.

## Change the Installation Directory with M_PREFIX

To change where MongoDB versions are installed, export `M_PREFIX` with the path to your preferred parent directory before invoking any `m` commands. The tool automatically appends `/m` and creates the required subdirectories.

### Temporary Change (Per Command)

For a one-off installation to a custom location, prefix the command:

```bash
M_PREFIX=/opt/mongodb m stable

```

This installs the latest stable MongoDB version under `/opt/mongodb/m/versions/` instead of the default home directory.

### Persistent Change (Shell Configuration)

To permanently change the installation directory, add the export to your shell startup file:

```bash

# ~/.bashrc, ~/.zshrc, or ~/.bash_profile

export M_PREFIX=/opt/mongodb
export M_BIN_DIR=/opt/mongodb/bin  # optional: customize symlink location

```

After reloading your shell (`source ~/.bashrc`), all subsequent `m` commands will use the new paths.

## Customize the Binary Symlink Location (M_BIN_DIR)

As implemented in `bin/m` (lines 14-16), the directory where `m` creates symbolic links to the active MongoDB binaries is controlled by the `M_BIN_DIR` environment variable. This defaults to `$M_PREFIX/bin` but can be overridden independently if you need executables accessible from a specific location in your `$PATH`.

```bash
export M_PREFIX=/data/mongodb
export M_BIN_DIR=/usr/local/bin

```

With this configuration, `m` stores large binary files under `/data/mongodb/m/` while placing lightweight symlinks in `/usr/local/bin`.

## Verify Active Directories

After configuring these variables, confirm the active paths using built-in `m` commands:

```bash
m ls          # Lists installed versions from $M_PREFIX/m/versions

m bin 6.0.2   # Prints the full path to the mongod binary for version 6.0.2

```

## Relocate an Existing Installation

If you have already installed MongoDB versions under the default `$HOME/.local/m` and want to move them to a new location:

1. Copy the existing directory structure to the new parent directory.
2. Update the `M_PREFIX` environment variable.
3. Rebuild the symbolic links to point to the new location.

```bash

# 1. Copy existing installations (preserve permissions)

cp -a $HOME/.local/m /opt/mongodb/m

# 2. Update environment variable

export M_PREFIX=/opt/mongodb

# 3. Recreate symlinks in the new bin directory

m reinstall 6.0.2

```

The `m reinstall` command forces `m` to recreate the symlink in the new `M_BIN_DIR` without re-downloading the binaries.

## Summary

- **Default behavior**: `m` stores binaries under `$HOME/.local/m`, using `M_PREFIX` which defaults to `$HOME/.local` as defined in `bin/m`.
- **Primary control**: Set the `M_PREFIX` environment variable to change the parent directory of the `m` folder.
- **Symlink control**: Use `M_BIN_DIR` to customize where executable links are placed; it defaults to `$M_PREFIX/bin`.
- **Migration path**: Copy the existing `m` directory to the new location, update `M_PREFIX`, and run `m reinstall` for each version to refresh symlinks.

## Frequently Asked Questions

### How do I check the current installation directory used by m?

Run the command `m bin <version>` (e.g., `m bin 6.0.2`) to print the full path to the `mongod` binary. This output follows the pattern `$M_PREFIX/m/versions/<version>/bin/mongod`, revealing the active prefix location.

### Can I use different installation directories for different terminal sessions?

Yes. Because `m` reads the `M_PREFIX` environment variable at runtime, you can set different values in different terminal sessions or shell scripts. Each session operates independently, allowing isolated MongoDB installations for different projects.

### What happens if I change M_PREFIX after installing versions?

If you change `M_PREFIX` without moving the files, `m` will not find your previously installed versions and will treat the new location as empty. To migrate, copy the `$HOME/.local/m` directory to the new `$M_PREFIX/m` location and run `m reinstall <version>` to recreate the symlinks.

### Is M_BIN_DIR required to be inside M_PREFIX?

No. While `M_BIN_DIR` defaults to `$M_PREFIX/bin`, you can set it to any directory such as `/usr/local/bin` or `$HOME/bin`. This is useful when storing large binaries on a separate partition (via `M_PREFIX`) while keeping executable links in a standard location already in your `$PATH`.