3 Ways to install MongoDB on Mac

Updated: February 1, 2024 By: Guest Contributor Post a comment

Introduction

Installing MongoDB on a Mac can be done through several methods, each with its own set of advantages and considerations. This guide will cover the most common ways, detailing the steps for installation, code examples where necessary, and additional notes on each method’s relative merits.

Homebrew Package Manager

Installing MongoDB through Homebrew is straightforward and one of the most common methods. Homebrew simplifies the installation process and manages updates automatically.

  1. Install Homebrew, if not already installed (see the detailed guide here). Homebrew is a package manager for macOS that makes it easy to install and manage software.
  2. Run the command brew tap mongodb/brew to add the MongoDB tap.
  3. Install MongoDB by running the brew install [email protected] (as an example, specifying the version you want).
  4. Start MongoDB using brew services start mongodb/brew/mongodb-community.

Example:

$ brew tap mongodb/brew
$ brew install [email protected]
$ brew services start mongodb/brew/mongodb-community

Notes: Using Homebrew allows for easy updates via brew upgrade, and it is recommended for most users due to its simplicity. However, installation options might be limited based on the versions available in Homebrew.

Manual Installation from MongoDB Archive

For users who need more control over the installation, manual installation is suitable. It involves downloading the MongoDB binaries and setting up manually.

  1. Download the desired version of MongoDB from the MongoDB Community Download Center.
  2. Extract the archive to your desired location, for example, /usr/local/mongodb.
  3. Add MongoDB’s bin folder to your path by updating your shell profile file, for example, ~/.bash_profile, ~/.zshenv, etc.
  4. Run MongoDB by executing the mongod command.

Example:

$ tar -zxvf mongodb-macos-x86_64-4.4.4.tgz
$ sudo mv mongodb-macos-x86_64-4.4.4 /usr/local/mongodb
$ echo 'export PATH="/usr/local/mongodb/bin:$PATH"' >> ~/.bash_profile
$ source ~/.bash_profile
$ mongod

Notes: This method provides more control over the installation process and location. However, it requires a bit more technical know-how and manual updates for the MongoDB version.

Docker Container

For those familiar with Docker, running MongoDB inside a Docker container is an isolation-focused option that avoids tampering with the system’s environment.

  1. Install Docker Desktop for Mac from the official Docker website.
  2. Open a terminal window and run the MongoDB container with a command like docker run --name some-mongo -d mongo:tag, where “tag” specifies the version of MongoDB you want to use.
  3. Access the MongoDB shell using docker exec -it some-mongo bash.

Commands:

$ docker run --name some-mongo -d mongo:4.4.4
$ docker exec -it some-mongo bash
# mongo

Notes: Docker is ideal for development environments, ensuring that the MongoDB setup does not affect the host system. The downside is that it requires understanding Docker concepts and may have slight performance overhead due to containerization.

Conclusion

These are the principal ways to install MongoDB on a Mac. Each approach has its particular benefit, whether it’s the simplicity of a package manager, the control of a manual setup, or the isolation provided by Docker. Your choice should depend on your comfort level with these technologies and your specific requirements for MongoDB use.