Main Tutorials

How to install mongoDB on Ubuntu

mongodb-ubuntu

This guide shows you how to install MongoDB on Ubuntu.

  1. Ubuntu 12.10
  2. MongoDB 2.2.3

1. Add 10gen package to source.list.d

Ubuntu 12 comes with a “mongo” package, but not the latest version.


$ sudo apt-cache search mongodb
mongodb
mongodb-clients
mongodb-dev
mongodb-server

It’s recommended to add 10gen package to /etc/apt/sources.list.d, as it contains the latest stable MongoDB. Create a /etc/apt/sources.list.d/mongo.list file, and declared the 10gen distro.


$ sudo vim /etc/apt/sources.list.d/mongo.list
/etc/apt/sources.list.d/mongo.list

##10gen package location

deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen

2. Add GPG Key

10gen package required GPG key, imports it :


$ sudo apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10

If you didn’t imports the GPG key, apt-get update will hits following error message :


GPG error: http://downloads-distro.mongodb.org dist Release: 
The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 9ECBEC467F0CEB10

3. Update package

Update your apt-get list.


$ sudo apt-get update

Search “mongodb” again, a new 10gen package is appearing now. Get the “mongodb-10gen“, it contains the latest stable MongoDB.


$ sudo apt-cache search mongodb
mongodb
mongodb-clients
mongodb-dev
mongodb-server

mongodb-10gen
mongodb18-10gen
mongodb20-10gen

4. Install mongodb-10gen

Everything is ready, now you can Install MongoDB :


$ sudo apt-get install mongodb-10gen

5. Where is MongoDB?

MongoDB is installed and started.


$ ps -ef | grep mongo
mongodb   5262     1  0 15:27 ?        00:00:14 /usr/bin/mongod --config /etc/mongodb.conf
mkyong    5578  3994  0 16:29 pts/0    00:00:00 grep --color=auto mongo

$ mongo -version
MongoDB shell version: 2.2.3

All MongoDB executable files are stored at /usr/bin/


$ ls -ls /usr/bin | grep mongo
 4220 -rwxr-xr-x 1 root   root     4317928 Feb  2 08:11 mongo
10316 -rwxr-xr-x 1 root   root    10563336 Feb  2 08:11 mongod
10320 -rwxr-xr-x 1 root   root    10563664 Feb  2 08:11 mongodump
10284 -rwxr-xr-x 1 root   root    10526736 Feb  2 08:11 mongoexport
10324 -rwxr-xr-x 1 root   root    10567768 Feb  2 08:11 mongofiles
10296 -rwxr-xr-x 1 root   root    10539056 Feb  2 08:11 mongoimport
10272 -rwxr-xr-x 1 root   root    10514544 Feb  2 08:11 mongooplog
10272 -rwxr-xr-x 1 root   root    10518512 Feb  2 08:11 mongoperf
10320 -rwxr-xr-x 1 root   root    10563632 Feb  2 08:11 mongorestore
 6644 -rwxr-xr-x 1 root   root     6802848 Feb  2 08:11 mongos
10312 -rwxr-xr-x 1 root   root    10556560 Feb  2 08:11 mongostat
10272 -rwxr-xr-x 1 root   root    10515856 Feb  2 08:11 mongotop

The “mongodb control script” is generated at /etc/init.d/mongodb


$ ls -ls /etc/init.d | grep mongo
 0 lrwxrwxrwx 1 root root   21 Feb  2 08:11 mongodb -> /lib/init/upstart-job

The MongoDB config file is at /etc/mongodb.conf

/etc/mongodb.conf

# mongodb.conf

# Where to store the data.

# Note: if you run mongodb as a non-root user (recommended) you may
# need to create and set permissions for this directory manually,
# e.g., if the parent directory isn't mutable by the mongodb user.
dbpath=/var/lib/mongodb

#where to log
logpath=/var/log/mongodb/mongodb.log

logappend=true

#port = 27017

#......

6. Controlling MongoDB

Some commands to control the MongoDB.

Starting MongoDB


$ sudo service mongodb start

Stoping MongoDB


$ sudo service mongodb stop

Restarting MongoDB


$ sudo service mongodb restart

References

  1. Official guide to install MongoDB on Ubuntu
  2. Debian Linux apt-get package management cheat sheet
  3. Install MongoDB on Mac OS X
  4. Install MongoDB on Windows

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
26 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Jorge Urdaneta
10 years ago

Thank you so much!

thili
8 years ago

I got error like this what can do for it.

E: Type ‘ckage’ is not known on line 1 in source list /etc/apt/sources.list.d/mongo.list

E: The list of sources could not be read.

Uncle Suliman
9 years ago

Very nice, thanks!

GregTheHun
10 years ago

Just a question, I was trying to get this to work with the newest versions of MongoDB, PHP 5.5.1 and Nginx.

For some reason I can’t install the driver by saying sudo apt-get install php5-mongo. I think it has to do with the fact that I’m using a newer version of PHP so it’s looking for the wrong version.

Any suggestions on how to install a newer php5-mongo driver?

Rob
10 years ago

Nice post!

Is it possible to install MongoDB without root privileges? Ideally I’d like to install it in my home directory. I can’t seem to get it working. Any help would be appreciated. Thanks.

nice
10 years ago

nice!!

Srikar
10 years ago

Thanks Mkyong 🙂

sohel
10 years ago
Reply to  Srikar

Thanks!!!!!!!!!!

Sreekanth
11 years ago

Thanks alot…:)

Aaron Newton
11 years ago

This worked nicely for me. However, I found that I kept getting socket error while trying to replace my MongoDB 2.0.+ install with this 2.2.+ version, so I found that I had to run the following remove command first:

sudo apt-get remove mongodb-clients mongodb-dev mongodb-server
Dave
11 years ago

I hope you can help: i followed your instructions and it appears mongo started, because the command “mongo” works from so far every folder where i navigate – yet, your step 5 above:

“Now, MongoDB is installed, started, and auto start MongoDB script is generated to “/etc/init/mongo” and “/etc/init.d/mongo“…”,

when i try those at the cmd line, result in this: “No such file or directory”.

what am i doing wrong? i am thinking now that if i reboot my system [ubuntu 12.04 running as a virtual object on vmplayer], i will be unable to get mongo started again.

how can i start, and stop the mongo db?

bit
11 years ago

Nice!

George
11 years ago

Thanks for the tutorial. Couple of points:

1- I had to import the keys (step 3) before step 2, otherwise step 2 always fails
2- It appears that I can only connect to the db if I run mongo with sudo

So please update your tutorial, thanks a lot.

Hai
11 years ago

Awesome. Thank you.

deFrance
11 years ago

what is mongo db ?? is it a database ??

George
11 years ago
Reply to  deFrance

What is google? Is it a search engine where people can lookup mongodb to know what it is?

Joey
11 years ago

Thank you!

Ajith
11 years ago

Nice tutorial 🙂

Interracial Pop Culture
11 years ago

Perfect tutorial thanks.

vini
12 years ago

Thanks a lot….!!! This article helped a lot !!!

ruvan
12 years ago

thanks mongo db 2 is now up and running on ubuntu11
going to test with pymongo and flask

dante
12 years ago

thank you!