MongoDB: Install on MacOS

In this tutorial, we present a step by step tutorial on how to install MongoDB on Mac OS.

The operating system considered for this tutorial is macOS 10.14 (Mojave).

1. Download the Community Edition of MongoDB from here: https://www.mongodb.com/download-center/community. It will be a TGZ file, something like mongodb-osx-ssl-x86_64-4.0.11.tgz

mongodb community server

2. After completion of download, navigate to the /Downloads directory.

				
					$ cd ~/Downloads
				
			

3. Extract the downloaded .tar file.

				
					$ tar -zxvf mongodb-osx-ssl-x86_64-4.0.11.tar
				
			

4. Now move the extracted directory to /usr/local/mongo

				
					$ sudo mv mongodb-osx-x86_64-4.0.11 /usr/local/mongodb
				
			

5. Navigate to /usr/local/mongo

				
					$ cd /usr/local/mongodb
				
			

6. Now MongoDB needs a data directory to store data. By default, it stores at /data/db but you need to create it.

				
					$ sudo mkdir -p /data/db  
				
			

7. With the data directory created, you need to set the correct permissions. You first check and get your current username and set permissions to it.

				
					$ whoami
					$ sudo chown [username] /data/db
				
			

9. Get back to the root directory.

				
					$ cd
				
			

10. List all files, including those starting with .

				
					$ ls -al
				
			

11. If you already find the .bash_profile file listed, open it. If not, create and open it.

				
					$ touch .bash_profile
					$ open .bash_profile
				
			

12. Copy the following two lines of code and append at the end of the file.

				
					export MONGO_PATH=/usr/local/mongodb
					export PATH=$PATH:$MONGO_PATH/bin
				
			

13. Save the changes, press CTRL + S.

14. Restart the terminal, or read and execute the .bash_profile file again

				
					$ source .bash_profile
				
			

15. If the installation is successful, the following command will give the version of MongoDB just installed on your system.

				
					$ mongo --version
				
			
mongodb version

16. Now we get to work with MongoDB. The first is to run the Mongo Daemon mongod

				
					$ mongod	
				
			

17. Next, open a new terminal window and start the Mongo shell

				
					$ mongo
				
			

This is where you will be typing your commands.

mongod and mongo shell