MongoDB: Install it on macOS

In this tutorial, we present a step by step guide on how to install MongoDB on macOS.

The operating system considered for this tutorial is macOS 14.2.1 (Sonoma).

1. Download the Community Edition of MongoDB from here: https://www.mongodb.com/download-center/community. As my machine is an M1 chip, the "macOS ARM 64" package shows up in the Platform select field. If yours is run by an Intel processor, the other "macOS x64" will show up. The package will be a TGZ file, something like mongodb-macos-arm64-7.0.5.tgz.

mongodb community server

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

				
					$ cd ~/Downloads
				
			

3. Extract the downloaded .tgz file.

				
					$ tar -zxvf mongodb-macos-arm64-7.0.5.tgz
				
			

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

				
					$ sudo mv mongodb-macos-arm64-7.0.5 /usr/local/mongodb
				
			

5. Navigate to /usr/local/mongodb

				
					$ 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. If you try to create it inside /usr/local/mongodb

				
					$ sudo mkdir -p /data/db  
				
			

it will throw an error mkdir: /data: Read-only file system. Create it inside some directory, say, Documents. So, data/db will be inside Documents now.

7. Get back to the root directory.

				
					$ cd
				
			

8. List all files, including those starting with .

				
					$ ls -al
				
			

9. If you already find the .zshrc file listed, open it. If not, create and open it.

				
					$ touch .zshrc
					$ open .zshrc
				
			

10. 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
				
			

11. Save the changes, press CTRL + S.

12. Restart the terminal, or read and execute the .zshrc file again

				
					$ source .zshrc
				
			

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

				
					$ mongod --version
				
			
mongodb version

14. mongod is the MongoDB daemon process which runs in the background. Now, earlier we have created a data/db directory inside Documents. Create a dbpath to it.

				
					$ mongod --dbpath=Documents/data/db
				
			
mongodb mongod dbpath

Installing Mongosh, The MongoDB Shell

15. We next install mongosh, the MongoDB Shell. Via this shell, you will be able to exeute queries and do interactions with your database in MongoDB. Download the mongosh zip package here. I use Mac Mini (M1), so the downloaded package name contains the -arm64 string.

16. Navigate to your /Downloads directory and extract the downloaded .zip file.

					
						$ tar -zxvf mongosh-2.1.1-darwin-arm64.zip	
					
				

17. Go inside the extracted directory (in my case, mongosh-2.1.1-darwin-arm64) and then into its bin directory. Copy the binary file into /usr/local/bin

					
						$ sudo cp mongosh /usr/local/bin
					
				

18. Copy mongosh_crypt_v1.dylib into /usr/local/lib

					
						$ sudo cp mongosh_crypt_v1.dylib /usr/local/lib
					
				

19. Create symbolic links.

					
						$ sudo ln -s $(pwd)/bin/* /usr/local/bin/
					
				

20. In a new terminal window, type

					
						$ mongosh
					
				
mongosh shell

The mongosh prompt appears >. This is the MongoDB Shell. Here you can execute MongoDB queries.