Sling Academy
Home/MongoDB/3 Ways to install MongoDB on Mac

3 Ways to install MongoDB on Mac

Last updated: February 01, 2024

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.

Next Article: 3 ways to self-host MongoDB on Ubuntu

Previous Article: 4 Ways to install MongoDB on Windows

Series: MongoDB Tutorials

MongoDB

You May Also Like

  • MongoDB: How to combine data from 2 collections into one
  • Hashed Indexes in MongoDB: A Practical Guide
  • Partitioning and Sharding in MongoDB: A Practical Guide (with Examples)
  • Geospatial Indexes in MongoDB: How to Speed Up Geospatial Queries
  • Understanding Partial Indexes in MongoDB
  • Exploring Sparse Indexes in MongoDB (with Examples)
  • Using Wildcard Indexes in MongoDB: An In-Depth Guide
  • Matching binary values in MongoDB: A practical guide (with examples)
  • Understanding $slice operator in MongoDB (with examples)
  • Caching in MongoDB: A practical guide (with examples)
  • CannotReuseObject Error: Attempted illegal reuse of a Mongo object in the same process space
  • How to perform cascade deletion in MongoDB (with examples)
  • MongoDB: Using $not and $nor operators to negate a query
  • MongoDB: Find SUM/MIN/MAX/AVG of each group in a collection
  • References (Manual Linking) in MongoDB: A Developer’s Guide (with Examples)
  • MongoDB: How to see all fields in a collection (with examples)
  • Type checking in MongoDB: A practical guide (with examples)
  • How to query an array of subdocuments in MongoDB (with examples)
  • MongoDB: How to compare 2 documents (with examples)