Installing mongodb on M1 mac

Viewed 1111

I tried opening the terminal using Rosetta but when I run brew install mongodb-community@5.0 I get an error that says:

Error: Cannot install under Rosetta 2 in ARM default prefix (/opt/homebrew)! To rerun under ARM use: arch -arm64 brew install ... To install under x86_64, install Homebrew into /usr/local.

However, when I run it as arch -arm64 brew install mongodb-community@5.0, it again complains with:

arch: posix_spawnp: brew: Bad CPU type in executable

Any help would be appreciated.

Thanks in advance.

2 Answers

There are two versions of brew:

  • Intel version (in /usr/local/homebrew)
  • ARM64 version for Apple Silicone (in /opt/homebrew/)

When you just type brew you are probably using native ARM64 version, for which there is no mongodb server yet.

You can check which version is your default with this:

$ which brew
/opt/homebrew/bin/brew

The solution I find most practical is to define alias for Intel brew like this in .zshrc:

# arm64/intel brew
alias brew86="arch -x86_64 /usr/local/Homebrew/bin/brew" 
alias brewARM="/opt/homebrew/bin/brew"

# ARM64 brew is default
eval "$(/opt/homebrew/bin/brew shellenv)"

Then you can use intel brew to install intel based apps like this:

brew86 install mongodb-community
brew86 services
...

And you can use ARM64 brew to install native ARM64 stuff like this:

brew install mongosh

Important note - do not use "Open using rosetta2" to launch terminal window in which you gonna run these commands. You wish to be in native ARM64, then install Intel apps only when is absolutely needed with brew86.

For some reason even though I had Homebrew in /usr/local the one for M1 was being executed. Solved it by manually navigating in /usr/local/Homebrew/bin and running the commands with ./brew.

Related