75

I'm on osx6.8 and need to install an earlier version of Mongodb, how do I install an earlier version with HomeBrew?

The below didn't work :(

dream-2:app2 star$ brew install mongodb-2.6.10
Error: No available formula for mongodb-2.6.10 
Searching formulae...
Searching taps...
dream-2:app2 star$ 

Edit:
I'm getting a message to explain how this post is unique compared to another one, well, the answer to the other question is super long and complex and it's specific to postgresql and doesn't really answer my question.

Ruvee
  • 8,611
  • 4
  • 18
  • 44
Agent Zebra
  • 4,410
  • 6
  • 33
  • 66
  • 1
    possible duplicate of [Homebrew install specific version of formula?](http://stackoverflow.com/questions/3987683/homebrew-install-specific-version-of-formula) – Cody Haines May 21 '15 at 16:22

7 Answers7

230

Note: In September 2019 mongodb was removed from homebrew core, so these instructions have been updated to use mongodb-community instead, installed from the external tap.

If your current installation is still the pre-September mongodb package then you will need to replace mongodb-community with just mongodb on the lines marked with #*# below.

Another option is to simply upgrade away from the deprecated package now.

I already have the latest version of mongo installed, thanks to.

brew tap mongodb/brew

brew install mongodb-community

But I want to switch to the old version sometimes. First, install it:

brew search mongo

brew install mongodb-community@3.2

Let's stop the current mongodb, if it is running:

brew services stop mongodb/brew/mongodb-community           #*#

# or if you had started it manually

killall mongod

Now I want 3.2 on my PATH instead of the latest:

brew unlink mongodb-community                               #*#

brew link --force mongodb-community@3.2

(Apparently it needs --force because it is keg-only.)

Now I have 3.2 on my PATH, I can start the test DB:

mongod --version

brew services start mongodb/brew/mongodb-community

# or start your own mongod from the command-line

When I am finished, I can do the reverse to switch back to the latest version:

brew services stop mongodb/brew/mongodb-community

brew unlink mongodb-community@3.2

brew link mongodb-community                                 #*#

brew services start mongodb/brew/mongodb-community          #*#

And restart again.

joeytwiddle
  • 29,306
  • 13
  • 121
  • 110
  • 10
    Unsure why this is listed at the bottom, this is by far the best answer, and also the most upvoted – Simon Jul 21 '18 at 17:38
  • I got into a rabbit hole with different versions, and this is the only solution that worked for me. – Devin Oct 19 '18 at 16:11
  • 1
    brew now tells you after installing an alternate version of a formula (e.g. brew install mongodb@3.6) to set your $PATH via: `echo 'export PATH="/usr/local/opt/mongodb@3.6/bin:$PATH"' >> ~/.bash_profile` – pnd Feb 22 '19 at 17:01
  • Why this is not the answer? This actually helped me to solve, thanks tons! – Zeid Tisnes Feb 29 '20 at 11:02
  • @pnd Updating `PATH` is an alternative to relinking, but we must then `source ~/.bash_profile` in every open terminal we care about. I proposed `unlink` and `link` because that switches mongo version for the whole system, which may reduce confusion. Advanced users can certainly update `PATH` if they prefer. – joeytwiddle Jun 03 '20 at 03:36
47

When trying to install old versions of something with homebrew, it's usually useful to start with brew search packagename, in this case, there's a 2.6 version available under homebrew/versions/mongodb26

So, to install that version:

brew install homebrew/versions/mongodb26

Edit

This answer has certainly become very dated. Take a look at the answer below for a valid way to accomplish this in 2021.

Cody Haines
  • 1,157
  • 10
  • 16
  • Thank you. I tried it, terminal has hung I think, this was the last thing it said, (what does it mean?): `==> Installing mongodb26 from homebrew/homebrew-versions ==> Downloading https://fastdl.mongodb.org/src/mongodb-src-r2.6.8.tar.gz ######################################################################## 100.0% ==> /usr/local/opt/scons/bin/scons install --prefix=/usr/local/Cellar/mongodb26/2.6.8 -j8 --cc=/usr/bin/gcc-4.2 --cxx=/usr/bin/g++-4.2 --osx-version-min=10.6` – Agent Zebra May 21 '15 at 16:44
  • 1
    How long did you let it run before terminating it? That's just a typical homebrew installation. It's compiling mongodb from source, so it should take a couple minutes and you won't necessarily see any output. – Cody Haines May 21 '15 at 17:00
  • Thanks I didn't know that o.O! lol, I tried it again, eventually it loaded. Lot's of crazy messages on doing mongod -v but, maybe I can figure it out from here. Thanks for your help! – Agent Zebra May 21 '15 at 17:07
  • 1
    `mongod --version` has much fewer 'crazy messages'. More specifically, `mongod -v` simply runs mongod in verbose mode, instead of displaying the version information. Good luck :) – Cody Haines May 21 '15 at 17:17
  • Thank very much, great help. Why is -v different when used with mongod ? When I do node -v I get the version number o.O?! – Agent Zebra May 21 '15 at 17:24
  • what if I want to get 2.4.14? The above command will install 2.4.12! – Mahshid Zeinaly Dec 10 '15 at 23:17
  • To have launchd start homebrew/versions/mongodb26 now and restart at login: `brew services start homebrew/versions/mongodb26`; Or, if you don't want/need a background service you can just run: `mongod --config /usr/local/etc/mongod.conf` – Wtower Jul 05 '16 at 07:59
  • I was just trying to install mongodb version 3.2 and your answer helped me a lot (`brew install homebrew/versions/mongodb32`), thank you! However, `brew search mongodb` didn't reveal **any** clue, that a version 3.2 was available and what the package name would be. Am I missing something? How can I search for all available versions? – qqilihq Jan 04 '17 at 09:26
  • This assumes you have an earlier version already installed. Some of the other answers are much better. – Simon Jul 21 '18 at 17:37
  • 10
    This answer seems a bit dated. `homebrew/versions was deprecated.` – frhd Aug 29 '18 at 12:43
  • Yeah, the answer below is much better than this one now. – Cody Haines Jul 11 '19 at 04:14
  • This answer is not valid anymore since Brew team removed the old Mongodb package, the one bellow works better ( with the official tap from MongoDB team ) – mim Dec 02 '19 at 20:34
4
curl -O https://fastdl.mongodb.org/osx/mongodb-osx-x86_64-3.2.12.tgz
tar -zxvf mongodb-osx-x86_64-3.2.12.tgz
mkdir -p mongodb
cp -R -n mongodb-osx-x86_64-3.2.12/ mongodb
export PATH=<mongodb-install-directory>/bin:$PATH #path to the dir created in step 3
mkdir -p /data/db
sudo chown -R $(whoami) /data/
mongod
CodeShadow
  • 3,503
  • 1
  • 17
  • 16
  • Homebrew just allows to install min 3.0. However, I need to use 2.6. Fortunately, your comment helped me to solve this and also helped me to install 2 version running at same time. – TommyDo Sep 17 '20 at 04:44
3

Instead of using homebrew you can use docker to install as many versions of mongodb as you want. Each mongodb can then run on separate ports.

Install docker with brew cask install docker and then open Docker.app. After docker is running, go to Terminal and install your mongodb version by selecting an image from https://hub.docker.com/_/mongo/ like so: docker run -d -p 28017:27017 --name mongo4 mongo:latest

Verify that it's running with docker ps and you can connect to mongodb from your app using port 28017. Repeat the steps with a different name and port to install more versions. Enjoy!

Vidar
  • 1,008
  • 14
  • 16
  • how do i verify the version of mongodb running in this docker instance? I looked at docker 3 years ago but at the time it was a bit too none intuitive for me so i kept using vms, any help would be appreciated – sed Jan 19 '19 at 12:07
3

Addition to the excellent answer of joeytwiddle :

if you don't want to link then unlink the old version of the software, you can just run it from the "cellar" (/usr/local/Cellar/), where brew installed it. Use ls /usr/local/Cellar/ to find the exact path of the executable. For example in my case, to run mongo shell:

/usr/local/Cellar/mongodb@3.6/3.6.7/bin/mongo
xiaoju
  • 351
  • 3
  • 11
2

I was able to install it using these instructions:

Installing MongoDB on OSX for local development

Over the last week, I’ve been building our MongoDB cluster on EC2 for production. For development, however, we’ll still need to install MongoDB locally. I’m running OSX 10.6.8, but these install instructions should be the same on all modern OSX versions.

Installing on OSX is much more pleasant than on EC2 (actually it’s just as easy on EC2, but since it’s a simpler setup there’s n real configuration or head scratching).

Download the latest binary:

curl -O http://fastdl.mongodb.org/osx/mongodb-osx-x86_64-2.0.2.tgz

Note!: If you don’t have wget installed, simply download the file above by visiting the link in your web browser and move it into your home directory.

We’re going to install everything under /usr/local/mongodb to keep things organized.

Create the directories (switch 'youruser' with your home user name):

sudo mkdir /usr/local/mongodb

sudo mkdir /usr/local/mongodb/log

sudo mkdir/usr/local/mongodb/data

sudo chown youruser /usr/local/mongodb/log

sudo chown youruser /usr/local/mongodb/data

sudo chgrp staff /usr/local/mongodb/log

sudo chgrp staff /usr/local/mongodb/data

Un-tar the binaries and move them into the correct folder:

tar -xvzf ~/mongodb-osx-x86_64-2.0.2.tgz

sudo mv ~/mongodb-osx-x86_64-2.0.2/* /usr/local/mongodb/

Create a config file for mongod:

sudo vi /usr/local/mongodb/mongod.conf

Paste:

dbpath=/usr/local/mongodb/data

logpath=/usr/local/mongodb/log/mongod.log

logappend=false

bind_ip=127.0.0.1

Note: dbpath and logpath specify the path to their respective files, logappend is set to overwrite the log file on each start of the database server, bind_ip only allows local connections.

Create an alias so that issuing mongod always read the config file:

vi ~/.profile

Paste:

# MongoDB Alias'

alias mongod="/usr/local/mongodb/bin/mongod --config=/usr/local/mongodb/mongod.conf"

All done, you should be able to simply type mongod after you reload the shell to start MongoDB. I preferred not to start mongod on boot, but there are other who prefer to and there’s plenty of documentation online to show you how to set that up with launchd and creating a .plist.

http://alexanderwong.me/post/15259867190/installing-mongodb-on-osx-for-local-development

Livi17
  • 1,620
  • 3
  • 25
  • 43
  • sudo chown youruser /usr/local/mongodb/log sudo chown youruser /usr/local/mongodb/data can be edited to sudo chown $(whoami) /usr/local/mongodb/log sudo chown $(whoami) /usr/local/mongodb/data – CodeShadow Mar 30 '17 at 06:29
  • Homebrew is the best tool for serious business. Breaking changes in the tool itself, breaking changes across versions of popular applications. – siefca Jan 29 '22 at 13:14
2

If you want to install an earlier MongoDB version on mac. Go to the link https://docs.mongodb.com/v3.6/tutorial/install-mongodb-on-os-x/ and select the version which you want to install and its very easy to install, just give a try. There will be around 2-3 commands for installation.

For example if you want to install version 3.6

brew tap mongodb/brew
brew install mongodb-community@3.6

If you got some error

If you need to have mongodb-community@3.6 first in your PATH run:
  echo 'export PATH="/usr/local/opt/mongodb-community@3.6/bin:$PATH"' >> ~/.bash_profile

Then run

export PATH="/usr/local/opt/mongodb-community@3.6/bin:$PATH"

To confirm if it successfully installed:

mongod -version
am2505
  • 2,194
  • 15
  • 19