MongoDB Local Installation on a Mac
By: Hunter Campbell
Tags: Database, Software Development, Web Development, Mongodb, NoSQL

Photo by Jan Antonin Kolar on Unsplash

Recently I ran into some trouble while trying to install MongoDB on my mac and had to bounce around on several sites such as MongoDB, StackOverflow, and a list of other random sites I found through googling. So to help someone else save time, I figured I would try and make a step-by-step guide on how I was able to get this done.

Step One: Figure out which version

The full version of the chart can be found here in the mongoDB docs.

Now my current macOS is Sierra (10.12) so that means I will be installing the 4.2 version of Mongo and as far as I can tell, as long as you are not on macOS Catalina (10.15) then you should be fine following the same steps. For all the Catalina users out there, refer to this link to another article here on Medium. Back to the process…

After you figure out which version you need to install on your mac you need to go to the link MongoDB Community Install Page and select the correct version to install in the upper left hand corner dropdown menu. I’ll paste a picture below to show you.

This page can also be reached here.

Step Two: Install the package manager Homebrew

After you select your version, the document is going to guide you down the page to the install directions. Now in order to run the recommended processes you need to have Homebrew installed on your system. To do so just follow this link to Homebrew’s installation guide here. Copy the command line and paste/run it in your macOS terminal. From there just follow their instructions.

Once you have Homebrew installed we then need to update it with the mongodb package. To do this you want to run in your terminal the command

brew tap mongodb/brew

Step Three: Install mongoDB with Homebrew

Start by running the command

brew install mongodb-community@< your version >

****EXAMPLE**** brew install mongodb-community@4.2

Everything up to this step should have been pretty smooth sailing. The next bit is where I ran into a few speed bumps. If you follow the documentation, it assumes that you already know how to set up your PATH and where your data is being stored, and rightly so. After all, what are you doing installing something via your terminal that you don’t fully understand (uh, learning).

Step Four: Setting your PATH file

To set up your PATH file you need to find the directory in which your installation has gone. The documentation will tell you that it is located in /usr/local/mongodb/bin. This may be correct for you and if it is that’s great! But for me it was in /usr/local/opt/mongodb-community@4.2/bin. By the way the opt folder is where add-on software packages go. I didn’t know this until I had to find where mongo went.

After you find the path to your mongo package you are going to want add it to your .bash_profile. In order to do this, go to your home folder within your terminal. You can get there by running

cd ~/

From there we are going to open our .bash_profile in our textEdit application by typing the following command

open -e .bash_profile

If for some reason you do not have a .bash_profile you can always create one from this directory with the following command

touch .bash_profile

and then opening your new .bash_profile with the before mentioned open command.

From here we are going to add our path with the following line

export PATH=$PATH:/usr/your/directory/goes/here

****EXAMPLE**** export PATH:$PATH:/usr/local/opt/mongodb-community@4.2/bin

To update our paths we then want to run

source .bash_profile

After that save your edits and close out the textEdit application.

Now because we used Homebrew package manager we do not need to setup a data/db directory on account that Homebrew sets up an alternative directory for us and pre routes our mongo server. So that means that we can now spin up our server and run the mongo command!

To start the local server run

brew services start mongodb-community@<your version>

and then run the command

mongo

to dive into the CLI of MongoDB.

When you are ready to shut-down the server run the command

exit

to end the mongo CLI and then run the command

brew services stop mongodb-community@<your version>

And there you have it! A local installation of MongoDB right on your OS. From here you should checkout this crash course by Traversy Media. It’s very well done will have you adding data in no time!

I hope you enjoyed and as always, please comment if you find anything that I should change or any problems you run into along the way!