How to install MongoDB on OS X

I started playing with MongoDB this weekend. It’s a cool little database, and John Nunemaker’s MongoMapper gem is a treat. Mongo’s maintainers are nice enough to provide pre-compiled binaries for OS X, but you still have to do a little setup and configuration. (There’s actually a portfile on MacPorts, but it wasn’t up-to-date with the latest version when I found it.)

Here’s how I got the server installed and running as a daemon in OS X, for local development.

Download, unpack, and install the pre-compiled 64-bit binaries:

1
2
3
4
5
curl -O http://downloads.mongodb.org/osx/mongodb-osx-x86_64-1.4.0.tgz
tar xzf mongodb-osx-x86_64-1.4.0.tgz
sudo mv mongodb-osx-x86_64-1.4.0 /usr/local/mongodb
sudo mkdir /usr/local/mongodb_data /var/log/mongodb
sudo chown -R root /usr/local/mongodb

(If you’re on a 32-bit machine, substitute in i386 for each x86_64 above.)

Next, you’ll want to make a config file so you can change the server’s options without fiddling with command-line arguments.

Save as: /usr/local/mongodb/mongod.conf

1
2
3
4
5
# Store data alongside MongoDB instead of the default, /data/db/
dbpath = /usr/local/mongodb_data

# Only accept local connections
bind_ip = 127.0.0.1

Now, we’ll make a launchd job to register the server as an OS X daemon. launchd will start the server at startup, stop it before shutdown, make sure it stays up, and redirect its output to a nice log file.

Save as: /Library/LaunchDaemons/org.mongodb.mongod.plist

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
  "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key>
  <string>org.mongodb.mongod</string>
  <key>ProgramArguments</key>
  <array>
    <string>/usr/local/mongodb/bin/mongod</string>
    <string>run</string>
    <string>--config</string>
    <string>/usr/local/mongodb/mongod.conf</string>
  </array>
  <key>RunAtLoad</key>
  <true/>
  <key>KeepAlive</key>
  <true/>
  <key>WorkingDirectory</key>
  <string>/usr/local/mongodb</string>
  <key>StandardErrorPath</key>
  <string>/var/log/mongodb/output.log</string>
  <key>StandardOutPath</key>
  <string>/var/log/mongodb/output.log</string>
</dict>
</plist>

Now we just need to load the launchd job:


sudo launchctl load /Library/LaunchDaemons/org.mongodb.mongod.plist

And that should do it! Try visiting http://localhost:28017 to see the status console for your database.

One last thing: you should probably add /usr/local/mongodb/bin to your $PATH. That way you can use the other binaries that ship with MongoDB, like the mongo console, mongoexport, and so on.

You can adjust your path the regular way by editing your shell’s profile, or you can use this nice paths.d mechanism that OS X provides:


sudo sh -c 'echo "/usr/local/mongodb/bin" > /etc/paths.d/mongodb'

Thanks to theozaurus in the comments below for that tip.

Comments

  1. theozaurusSeptember 20, 2009 @ 08:33 AM

    Discovered OS X has a pretty neat path handling mechanism:

    sudo sh -c ‘echo “/usr/local/mongodb/bin” > /etc/paths.d/mongodb’

  2. JohnBakuOctober 01, 2009 @ 03:48 PM

    Thanks a lot… was getting tired of loading mongodb up by hand each time I had to restart my computer! :-)

  3. Dan WardOctober 24, 2009 @ 04:01 AM

    Worked flawlessly, thanks.

  4. Chris KampmeierOctober 24, 2009 @ 03:42 PM

    Neat, theozaurus! I didn’t know about /etc/paths.d.

  5. AnlekDecember 21, 2009 @ 08:17 AM

    Great job, worked like a charm!

  6. Bjarki GudlaugssonDecember 28, 2009 @ 09:59 AM

    MacPorts version of MongoDB is now 1.2.0, which is the latest stable version so it should be okay to use it.

  7. Mislav MarohnićDecember 29, 2009 @ 04:18 AM

    I’ve wrote a shell scripts that installs MongoDB using Homebrew and sets it up for your user. I like that much better than system-wide

    Run it with:

    curl -s http://gist.github.com/265272.txt | bash
    
  8. Jai-Gouk KimDecember 29, 2009 @ 05:02 AM

    Thanks. IT WORKS!

  9. Alf MikulaJanuary 02, 2010 @ 09:32 AM

    I had a problem with the MacPorts version of mongodb, which is currently up to date with the latest stable version of 1.2.1. However, MacPorts has a little hiccup in the setup somewhere and I had to symlink one of the libs mongodb uses:

    sudo ln -s /opt/local/lib/nspr/libnspr4.dylib /opt/local/lib/
    
  10. BorisFebruary 19, 2010 @ 07:34 AM

    Thanks much. Fast and painless. You rock!

  11. LukeMarch 02, 2010 @ 06:03 AM

    Thanks a ton man, helped a lot!

  12. stevoMarch 06, 2010 @ 05:09 PM

    I get the following message “Dubious ownership on file (skipping): /Library/LaunchDaemons/org.mongodb.mongod.plist nothing found to load” in the terminal when launch the launchd job.

    How can i fix this?

  13. John BallingerMarch 11, 2010 @ 04:30 PM

    Thanks, this page is permanently booked marked so I can instal this again.

    Cheers, John.

  14. EMarch 13, 2010 @ 08:03 AM

    Thanks :)

  15. Christian RojasMarch 27, 2010 @ 09:16 AM

    Thanks a lot!.

  16. Alex SharpMarch 31, 2010 @ 01:00 PM

    For anyone interested, I have a project on Github that takes care of a lot of the configuration tasks (copying config files, setting up .plist, etc). http://github.com/ajsharp/mongo-config.git

  17. SigfridApril 17, 2010 @ 07:01 PM

    Hello, I have the same issue of Stevo. Any ideas how to fix it?

    Thanks and have a good 1!

  18. Thiago TarantoMay 14, 2010 @ 06:04 PM

    ENG: Excellent! Simple, practical and functional! Thanks a lot!

    PT-BR: Excelente! Simples pratico e funcional Muito obrigado!

  19. Robin MayfieldJune 05, 2010 @ 09:32 AM

    Thanks, Chris. Excellent walkthrough of the Mongo setup on OS X. Really appreciate it. The PATH trick is pretty cool too - thanks, theozaurus!

  20. MartijnJuly 30, 2010 @ 03:54 AM

    This runs mongod as root which is unnecessary and potentially unsafe. Does anyone know how to make it run as a user?

Post a comment

Comment
(not published)
(optional)