Raspberry Pi MongoDB Installation – The working guide!

In this tutorial I will go through how to install MongoDb on your Raspberry Pi and also give a few tips on typical issues and fixes.
MongoDb is one of the most popular “NoSQL” databases out there. NoSQL releases you from the constraints of a typical relational database. They essentially allow you to store data in “key – value” stores which means you do not really have a typical schema as you would see in most other databases. They are often all open source and are horizontally scalable. MongoDB is said to be a document-oriented database which is designed for ease of development and scaling.

![](http://media.mongodb.org/logo-mongodb.png) MongoDB

So why am I not considering MYSQL and the likes? Well I already run MySql on my Pi and I have to say the performance has been perfectly fine. This blog runs solely of my Pi and the wordpress data all sits on MySql. I have heard a lot of arguments against using it on the Pi due to resource usage etc, however it has run perfectly fine for me with little overhead. However with all of that being said you should never constrain yourself and I am currently working on a number of projects that require me to hold lots of random data together in a database. I want to be able to work quickly and not spend time having to set up table after table and check my relations, field types etc. I just want to store data and access it simply. This is where MongoDB comes in and really works well.

Before kicking off with the installation instructions I will add a few links for further reading below so you can get a bit more information about MongoDb, its usages, its good and bad points and also why you should consider NoSQL.

Further Reading:
What is noSql?
Advantages & Disadvantages of NoSql
When NoSQL makes sense to use

Lets get on with the installation. Firstly get yourself logged in to a terminal window on your Pi, either from the desktop or SSH straight in. As always we will run the following two commands:

sudo apt-get update
sudo apt-get upgrade

Next we will dive in to grabbing a fair few libraries. If you have followed any of my previous blogs you will probably have a few of these already but essentially these will allow us to use github, build tools, python implementation of mongo client and various file/system extensions.

sudo apt-get install build-essential libboost-filesystem-dev libboost-program-options-dev libboost-system-dev libboost-thread-dev scons libboost-all-dev python-pymongo git

Just hit yes to any of the other libraries it wants to pull in once you use the above command. We then move on to grabbing a version of MongoDb that has been edited to work on the Pi. If you try and install Mongo most other ways you get compile errors and that comes back to the same issues we have seen with the likes of Mono, at the moment quite a few products out there do not support the Pi architecture. But that is not a problem for us today as Skrabban has hosted an edited version for us.

cd ~

git clone https://github.com/skrabban/mongo-nonx86

This will clone the repository in to the mongo-nonx86 directory.

cd mongo-nonx86

We are now sat in an uncompiled version of MongoDb for the Pi, we will have to compile this ourselves on the Pi. If you are using one of the model A Pi’s this is mostly likely going to be a problem for you as the amount of RAM can be too low and max out. I will also point out that compiling takes up SD Card space so make sure you over 500mb free.

The following code should be executed within our mongo-nonx86 directory. It will execute “Scons” which is a software construction program written in python. WARNING: The below code will take hours to run on your Pi, try letting it run over night.

sudo scons

This above code will build the repository for us. When I first ran this after a few hours I hit an error during compiling. I expect it was because I ran “scons” without sudo. However even with the terminating error I just ran “sudo scons” once more and everything built correctly.

We can now run the installation code.

sudo scons --prefix=/opt/mongo install

Once again the above code can take a few hours to complete, for me it took about 4 hours in total. You should now have an installation of mongo on your Pi. We now need to do some general housekeeping with folders and permissions then finally an init.d script to be able to kick off mongodb easily.

Permissions for users and groups:

sudo adduser --firstuid 100 --ingroup nogroup --shell /etc/false --disabled-password --gecos "" --no-create-home mongodb

A folder for our log files to go:

sudo mkdir /var/log/mongodb/

Permissions for log file:

sudo chown mongodb:nogroup /var/log/mongodb/

A folder for our state data:

sudo mkdir /var/lib/mongodb

Permissions for the folder:

sudo chown mongodb:nogroup /var/lib/mongodb

Moving our init.d script to etc:

sudo cp debian/init.d /etc/init.d/mongod

Moving our config file to etc:

sudo cp debian/mongodb.conf /etc/

Linking folders up:

sudo ln -s /opt/mongo/bin/mongod /usr/bin/mongod

The above code as said does a lot of housekeeping but you may notice that when trying to run the init.d script you hit some errors. We need to finalise permissions and make sure the init.d script is executable so complete the installation with the final statements.

sudo chmod u+x /etc/init.d/mongod

sudo update-rc.d mongod defaults

You may get some warning when executing the above statement however these can be ignored.

We can now start MongoDB using the following command:

sudo /etc/init.d/mongod start

To connect to the instance you can then type:


You should then see connected to test:

pi@raspberrypi ~ $ mongo
MongoDB shell version: 2.1.1-pre-
connecting to: test

So now we have a mongo instance running on our server at http://localhost:27017 however mongo also has a very basic interface you can access at http://localhost:28017 just to give basic information.

If you click a link from that page and are hit with:

REST is not enabled.  use --rest to turn on.

Then we simply need to edit the init.d script and add in –rest as a parameter. The code below will show you how to do this.

sudo nano /etc/init.d/mongod

Scroll down to:

DAEMON_OPTS=${DAEMON_OPTS:-"--dbpath $DATA --logpath $LOGFILE run"}

And add in –rest to give:

DAEMON_OPTS=${DAEMON_OPTS:-"--dbpath $DATA --logpath $LOGFILE run"}
DAEMON_OPTS="$DAEMON_OPTS --config $CONF --rest"

Hit Ctrl+X, Y and then restart mongodb using:

sudo /etc/init.d/mongod restart

You will then have all of the options available to you through the mongo UI webpage. Well, for now thats about it. In later tutorials I will go through creation of collections, saving data, reading data and some of the uses you can put MongoDB to. If you have any problems/comments please leave them in the comments section below and I’ll get back to you with answers in no time.


You may also like...

21 Responses

  1. Great guide! Thanks.

    You forgot to include:

    sudo ln -s /opt/mongo/bin/mongo /usr/bin/

    Without that, running “mongo” gives an error.

  2. david says:

    I have problem with mongo command. When I type mongo i get: command not found.

  3. Andrey says:

    I was need to add some, becouse bash cant find command ‘mongo’:

    nano .bashrc
    add the path at the end of this file:
    export PATH

    and i was need to use flashcard bigger then 4GB, because i have an error – no space

  4. Darryl says:


    Still works well. Compile time is about 3 hours total on a Pi2.

  5. Luca Di Gregorio says:

    I’ve tried follow the steps but an error occurs, it seems compiling the d_migrate.cpp.
    g++: internal compiler error: Killed (program cc1plus)
    I’ve also seen that the warning “swp{p} use is deprecated for ARMv6 and ARMv7” appears a lot of time.

  6. Jon Meyer says:

    Great instructions! Thanks!

    I think you forgot the following though:
    ln -s /opt/mongo/bin/mongo /usr/bin/mongo

    Thanks again.


  7. Mike Jones says:

    hello – just found this and it was quite helpful to me. I’m setting up a home automation project using ESP8266 modules, mosquitto, node-red, and hopefully a mongodb.
    did you ever write any more about common usage of mongodb?
    one note – if I run /etc/init./mongod start, and it’s already running, I get a message that its already running. However, if I enter sudo mongod I get “–bash: dongod: command not found”. I can access the :28017 port fine, just can’t do anything from command line. Any thoughts on this? Thanks – Mike

  8. Martyn says:

    I had to add /opt/mongo/bin to my path before the mongo client would connect.
    Everything else went very smoothly. took about 8 hours total compiling on a raspberry pi b+

    export PATH

  9. Victor says:

    Thank you for the nice tutorial, however i think by running both scons and scons –prefix=/opt/mongo install you are compiling Mongo twice.
    It will also be nice if you can mention that, when building mongo on a Pi2 you can use 4 cores by adding –jobs=4.

  10. Bluice Zhen says:

    Can I translate this article to Chinses, and show in my blog? I will write your name and url in my blog.

  11. Vithya says:

    I followed all the instructions to installMOngoDB on my Raspberry Pi. I finished installation and started MongoDB. Then, I ran command ‘mongo’ to start the shell. I got the below error…
    pi@raspberrypi ~ $ mongo
    bash: mongo: command not found
    Please let me know solution to resolve and run mongo shell.

  12. Vithya says:

    I am able to start mongo shell now by entering full path as /opt/mongo/bin/mongo

    I added documents to the collection.

    Now I face import error with pymongo

    I am getting the below,
    import pymongo
    importError: No module names pymongo

    Is it due to the incompatibility issue between pymongo and MongoDB versions? Please guide me.

  13. Robert says:

    Nice article. Thank you for this tutorial.

  14. Jose says:

    HI I need help

    During command scons –prefix=/opt/mongo install I had a disconnection and when I try to reexecute had this message:
    root@raspberrypi:/home/pi# scons –prefix=/opt/mongo install;date

    scons: *** No SConstruct file found.
    File “/usr/lib/scons/SCons/Script/Main.py”, line 904, in _main

    What can I do to complete this command

  15. Jeremy says:

    Hello! I am pretty sure I followed the above steps perfectly and I’m still getting “Could not find /usr/bin/mongod” when trying “sudo /etc/init.d/mongod start”. I’m running on Raspbian 4.1.6 (?). Thanks!

    • Jeremy says:

      To anyone reading this, ignore my question. I figured it out. Basically, it was taking forever to do the install and compile, so my SSH session was timing out and I was unaware they weren’t finished. Patience got me there. This tutorial is awesome. Thanks!

  16. Juan says:

    Work perfectly! Thanks you so much!!

  17. Marko says:

    Hi! I get the following while trying the scons command after a few seconds

    scons: *** [build/linux2/normal/mongo/db/db.o] Error 1
    scons: building terminated beacause of errors

    I am running it on a Raspberry Pi 2. Thanks in advance for any help.

  18. Gee says:

    Please note that mongodb v3 does not support the ARM V6 cpu of raspi1. Only raspi2 brings V7 which is compatible.

    Check http://andyfelong.com/2015/12/mongodb-3-0-7-on-raspberry-pi-2/ for instructions

  19. Chris says:

    Jeremy, do a task like this in a screen session. That way if your connection dies you can reconnect later.
    Look up ‘GNU screen’

Leave a Reply

Your email address will not be published. Required fields are marked *