Installing MongoDB on latest MacOS Intel and M1 base processors with Homebrew
Hey there, i'll take it you are here because you have had a hard time installing Mongodb on your macOS yes? Well i had the same issue and, below i will share the reason why and how you can fix this issue.
Why the issue?
Actually, Catalina 10.5 upwards has a surprise change: it won't allow changes to the root directory, this was discussed on the reddit here.
How to fix it?
Here is how you can get this fixed and have mongodb running locally on your macos.
- Make sure you have Homebrew installed properly, you can follow here on how to install brew.
- Now, for some reason you need to install xcode tools, you don't need to actually install xcode itself, but you need xcode tools to avoid it throwing an error. we can do this with the command:
xcode-select --install
After successfully installing xcode tools, to install mongodb we need to first tap into mongodb, think of it like, brew doesn't know or has never heard of this package before, so we just saying "hey brew you can tap into any other resource" in our case we would say:
brew tap mongodb/brew
and here pretty much we have brew saying, "okay, i am now aware of one more resource which is mongodb" From here we can now install mongodb with brew by saying;
brew install mongodb-community@5.0
Note: you can specify the version you wanna install
Now from here this won't give you immediate access to mongodb, you need to at least start the services, it's a server and it needs to be started. So, lets get started with the services of mongodb.
So we will run the command bellow which will say to brew "hey brew, you have services and from that i wanna start a spacial service called mongodb-community@5.0" with the command below
brew services start mongodb-community@5.0
After running this, you should get a successfull message saying:
==> Successfully started `mongodb-community@5.0` (label: homebrew.mxcl.mongodb-community@5.0)
Note:Now similarly to stop this service you use the same command except you will pass the argument stop as seen below:
brew services stop mongodb-community@5.0
Whenever you need to use mongodb you will have to run the command to start it. But obviously you don't wanna do this again and again, you wanna keep this up and running and for that all you have to do is link it.
Here things will look different depending on the processor you are running, if you are on M1
chip or intel
.
- Intel based:
brew link mongodb-community@5.0
- M1 chip based:
mongod --config /opt/homebrew/etc/mongod.conf --fork
...and that's it!!! From here you can just run:
mongosh
Hope that helps!
PS: You can also use MongoDB Compass to visually manage your mongodb databases.
Post originally published at blog.fotiecodes.com