A 32 bit version of Raspbian is currently the most common OS that runs on a Raspberry PI. RPI 3 comes equipped with a 64 bit CPU, however at the moment only experimental 64 bit versions of Raspbian may be found. There is, however a 64 bit ARM version of SUSE Linux available for RPI 3 and probably in the near future 64 bit platforms for RPI 3 will become more common, such as Fedora.

The highest MongoDB version that will run on 32 bit is v3.2. As of MongoDB v3.4 only 64 bit versions are supported. MongoDB v3.2 is the latest version that you will be able to run on a 32 bit Raspbian. In addition, you will not be able to run MongoDB with the WiredTiger storage engine since it only works on 64 bit versions. You will have to start the DB with the MMAPv1 storage engine. Also MMAPv1 has a limit of 2GB of storage, at least on ARM32. Once your DB hits that limit, you’re out of luck.

Below you will find steps for building MongoDB for both ARM32 and ARM64 (aarch64).

Building MongoDB on ARM32

The steps below describe how you can compile MongoDB 3.2 on your Raspberry PI with 32 bit ARM architecture.

$ uname -a
Linux mypi 4.4.50-v7+ #970 SMP Mon Feb 20 19:18:29 GMT 2017 armv7l GNU/Linux

1) Log in as a regular user.

For instance “pi” user and cd to home directory.

$ cd

2) Update/upgrade your system.

$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get install wget

3) Download and unpack mongodb source code.

$ mkdir install
$ cd install
$ wget https://fastdl.mongodb.org/src/mongodb-src-r3.2.12.tar.gz
$ tar xvf mongodb-src-r3.2.12.tar.gz
$ cd mongodb-src-r3.2.12

4) Check install steps in docs/building.md and verify prerequisites.

Note that scons will be installed in the next step, in case you don’t have it already.

$ cat docs/building.md
$ gcc --version
$ python --version
$ scons --version

5) Install dependencies.

$ sudo aptitude install scons build-essential
$ sudo aptitude install libboost-filesystem-dev libboost-program-options-dev libboost-system-dev libboost-thread-dev
$ sudo aptitude install python-pymongo

6) Temporarily increase swap space.
You may run into compilation and/or linking issues due to insufficient swap space. You can temporarily increase it with the steps below. Don’t forget to delete the swap file after you reboot.

sudo dd if=/dev/zero of=/mytempswapfile bs=1024 count=524288
sudo chmod 0600 /mytempswapfile
sudo mkswap /mytempswapfile
sudo swapon /mytempswapfile

7) Generate additional sources.

$ cd src/third_party/mozjs-38/
$ ./get_sources.sh
$ ./gen-config.sh arm linux
$ cd -

Note: if you skip these steps you may encounter the following compilation error:

Compiling build/opt/mongo/scripting/mozjs/base.o
In file included from :0:0:
/usr/include/stdc-predef.h:59:1: fatal error: js-config.h: No such file or directory
#endif
^
compilation terminated.
scons: *** [build/opt/mongo/scripting/mozjs/base.o] Error 1
scons: building terminated because of errors.

8) Compile and build.

Note: this could take a long time.

While you certainly could run “scons all” you will run into several challenges. First of all, WiredTiger will not work on a 32 bit architecture so you will encounter some compilation challenges with it. It is recommended to simply exclude the WiredTiger storage engine altogether. That means you will run MongoDB with the MMAPv1 storage engine only. Second, if you just want to run mongod/mongos and mongo you don’t need to compile all the tests, which will take a very long time to compile and link. Third, compiling everything will generate somewhere in the neighbourhood of 25GB of build files, while just mongo, mongod and mongos will take up around 4GB.

Probably all you need is the following:

$ scons core --wiredtiger=off --mmapv1=on

Or if you’re not going to use mongos you could even compile as follows:

$ scons mongo mongod --wiredtiger=off --mmapv1=on

Note that you need to explicitly specify the mmapv1 option as shown above, otherwise this storage engine will not get compiled into the mongod binary.

9) Reduce binaries file size.

You will notice that the binaries that are generated under ./build/opt/mongo are quite large:
-rwxr-xr-x 1 pi pi 156M Mar 7 13:26 mongo
-rwxr-xr-x 1 pi pi 429M Mar 9 14:36 mongod
-rwxr-xr-x 1 pi pi 217M Mar 9 16:52 mongos

That is because they contain debugging information. You can remove this with the strip command, for instance

$ strip -s mongo

The resulting files are now much smaller:
-rwxr-xr-x 1 pi pi 14M Mar 9 20:32 mongo
-rwxr-xr-x 1 pi pi 27M Mar 9 20:32 mongod
-rwxr-xr-x 1 pi pi 13M Mar 9 20:33 mongos

10) Copy binaries.

Finally copy the binaries to their bin location:

$ cd build/opt/mongo
$ sudo cp mongo mongod mongos /usr/local/bin/

Building MongoDB on aarch64 (ARM64)

The steps below describe how you can compile MongoDB 3.4 on your Raspberry PI with 64 bit ARM architecture.

$ uname -a
Linux mypi 4.4.57-18.3-default #1 SMP Thu Mar 30 06:39:47 UTC 2017 (39c8557) aarch64 aarch64 aarch64 GNU/Linux

If you want to install MongoDB 3.4 and higher on your Raspberry PI 3, you will first have to install 64-bit openSUSE Leap (JeOS Image). At the time of this writing, the latest version is Leap 42.2. It works great but you may run into wlan issues that can luckily be resolved. Once you have the 64 bit SUSE platform running on your RPI 3 you will have to use zypper to install gcc5 or gcc6 and scons. Then download the mongodb source code for 3.4 or higher. You can compile with “scons mongod –wiredtiger=on –mmapv1=off –disable-warnings-as-errors”. You may run into a build problem for the WiredTiger engine however the fix is rather simple. I added 2 CFLAGS lines in mongodb-src-r3.4.3/src/third_party/wiredtiger/SConscript:

elif env.TargetOSIs('linux'):
    env.Append(CFLAGS=["-march=armv8-a+crc"])
    env.Append(CFLAGS=["-mtune=generic"])
    env.Append(CPPPATH=["build_linux"])
    env.Append(CPPDEFINES=["_GNU_SOURCE"])

Once the build is done, don’t forget to reduce the binary files with the strip command.

Note that you can’t compile MongoDB 3.2 on an aarch64 architecture, you need at least version 3.4 or higher.

44 Replies to “Compile and install MongoDB on Raspberry PI”

  1. using gcc version 6.3.0 20170516 (Raspbian 6.3.0-18+rpi1)
    I’ve got the following error while building:

    In file included from src/mongo/platform/compiler.h:132:0,
    from src/mongo/util/invariant.h:30,
    from src/mongo/base/string_data.h:39,
    from build/opt/mongo/base/error_codes.h:31,
    from src/mongo/base/status.h:34,
    from src/mongo/client/dbclient.cpp:36:
    src/mongo/client/dbclientcursor.h: In member function ‘void mongo::DBClientCursor::_assertIfNull() const’:
    src/mongo/util/assert_util.h:233:28: error: nonnull argument ‘this’ compared to NULL [-Werror=nonnull-compare]

    1. Simply use that:
      scons all –disable-warnings-as-errors –wiredtiger=off –mmapv1=on –warn=no-all

  2. Thanks for this tutorial.
    Most (if not all) compile errors are, in fact, warnings. As warnings are considered as errors, you should pass an extra argument to the `scons` command at step 8: `–disable-warnings-as-errors`

  3. Hello. I try this on cubieboard1:
    $uname -a
    Linux cubieboard 4.14.15-sunxi #28 SMP Mon Jan 29 07:24:48 CET 2018 armv7l armv7l armv7l GNU/Linux

    It was compiled without errors.

    But when I run mongo I get an error:
    2018-02-21T09:47:44.479+0300 F – [main] Invalid access at address: 0
    2018-02-21T09:47:44.520+0300 F – [main] Got signal: 11 (Segmentation fault).

    Full backtrace:
    https://pastebin.com/cBXzGKQU

    Any ideas how to win this?

    1. There are quite a few JIRA items out on similar issues, however those are generally for the mongod binary. Are you at least able to run mongod and then connect to your database from a shell on a different workstation? Also, which version of mongo are you running?

  4. Hi,
    have you guys ever experienced a memory freeze on armv7l raspberry 3?

    I tried several times, but it just freezes when doing the following command, although all the previous commands are running without any errors.

    “`bash
    $ scons mongo mongod –wiredtiger=off –mmapv1=on
    “`

    Thanks

    1. Perhaps try and increase your swap space as well as compile one binary at a time. For instance, scons mongod, then scons mongo.

  5. Thank you for the helpful instruction. By this I could compile 3.2 on a Odroid HC2 with Debian Stretch. One question remains: Is there a script or switch to generate all files on the right places in the system, e.g. init.d, conf and others, or do I need to do that manually?

    1. I am not aware of any such scripts… perhaps there’s a scons argument that can do this, similar to “make install”, but I’m not aware of it. I simply copied the binaries to /usr/local/bin since there are only a few of them (mongod, mongos, mongo). The other files, such as mongo.conf and the directories to store the logs and data files I created manually.

      1. How will another package, which depends on mongodb, during apt-get install will recognize that it is already there. Is there a file, which needs to be tweaked?

        1. Places where apt-get stores data are usually /var/cache/apt and /var/lib/apt. I’m not entirely sure how apt-get checks whether or not a dependency exists, I am guessing it keeps that data somewhere in /var/lib/apt. However, if you do run into a situation like that, it may be easier to simply use the -m or –ignore-missing argument in apt-get if it complains about a missing mongo package. I’m sure there’s probably other or better ways to deal with this.

  6. Hi. I’ve tried this on RP 3 A+
    Almost nothing work(a new board on market). Suse don’t boot…etc.etc.debian arm 64 worked…it took 2 days to compile…

    the best solution now is Ubuntu arm 64!

    netplan is a pain in the a*s!!! f*** netplan!
    but after setting netplan (wifi only for rp3 A+) just apt-get update / apt-get install mongodb.. and it’s done… working 64bit version πŸ™‚

    Raspberry Pi 3B/3B+/A+:
    ubuntu-18.04.2-preinstalled-server-arm64+raspi3.img.xz
    http://cdimage.ubuntu.com/ubuntu/releases/bionic/release/ubuntu-18.04.2-preinstalled-server-arm64+raspi3.img.xz

    full instruction(not needed)
    https://wiki.ubuntu.com/ARM/RaspberryPi

  7. Thanks for the instruction. Tried on OrangePI PC board with OpenMediaVault 4.x for ARM image and got the following error:
    scons: Building targets …
    Compiling build/opt/mongo/scripting/mozjs/base.o
    cc1plus: fatal error: js-config.h: No such file or directory
    compilation terminated.
    scons: *** [build/opt/mongo/scripting/mozjs/base.o] Error 1
    scons: building terminated because of errors.

    The CPU is ArmV71.
    What can be the problem?

    1. Turns out I missed one step in your instruction. Was able to compile successfully.
      Thanks again for the instruction.

  8. Hi Koen,
    great guide, I’ve managed to have mongo up and running with no troubles!
    What about the tools for the v3.2 though? Like mongodump, mongorestore and so on?
    Cheers!

  9. The link in step 3 is outdated. I suggest using the version from GitHub: https://github.com/mongodb/mongo/archive/r3.2.12.tar.gz

    When using the source from GitHub with the link given above, repository metadata will not be set correctly. That is why in step 8, one needs to add MONGO_VERSION=3.2.12 when running scons, e.g. scons MONGO_VERSION=3.2.12 core –wiredtiger=off –mmapv1=on

  10. thank you – this was very useful! I could build mongod and mongo successfully for Raspberry Pi 4.
    There was one compilation error (undefined reference to major and minor) which could be fixed by adding #include to the file src/mongo/db/storage/mmap_v1/mmap_v1_engine.cpp.

  11. Use the following build command: scons mongo mongod –disable-warnings-as-errors –wiredtiger=off –mmapv1=on –warn=no-all.
    There was one compilation error (undefined reference to major and minor) which could be fixed by adding #include sys/sysmacros.h to the file src/mongo/db/storage/mmap_v1/mmap_v1_engine.cpp.
    Create a database folder (eg. /home/pi/data_db) and start mongod with parameters mongod –storageEngine=mmapv1 –dbpath=/home/pi/data_db

  12. I have errors:

    src/third_party/boost-1.56.0/boost/mpl/assert.hpp:188:21: error: unnecessary parentheses in declaration of ‘assert_arg’ [-Werror=parentheses]
    failed ************ (Pred::************
    ^
    src/third_party/boost-1.56.0/boost/mpl/assert.hpp:193:21: error: unnecessary parentheses in declaration of ‘assert_not_arg’ [-Werror=parentheses]
    failed ************ (boost::mpl::not_::************

      1. I didn’t realize you cant edit edit these comments,

        the errors are in the file:
        /install/mongodb-src-r3.2.12/src/third_party/boost-1.56.0/boost/mpl/assert.hpp

        these declarations specifically:

        template
        failed ************ (Pred::************
        assert_arg( void (*)(Pred), typename assert_arg_pred<Pr$
        );

        template
        failed ************ (boost::mpl::not_::************
        assert_not_arg( void (*)(Pred), typename assert_arg_pre$
        );

        I cant tell what the error is, does anyone else see it?

        1. Using the updated package from this github repo:
          https://github.com/mongodb/mongo/archive/r3.2.12.tar.gz

          and using the scons command:
          scons MONGO_VERSION=3.2.12 core –wiredtiger=off –mmapv1=on –disable-warnings-as-errors

          I got the error:

          src/mongo/db/storage/mmap_v1/mmap_v1_engine.cpp: In function void mongo::{anonymous}::checkReadAhead(const string&)’:
          src/mongo/db/storage/mmap_v1/mmap_v1_engine.cpp:170:61: error: ‘major’ was not declared in this scope
          ring path = str::stream() << "/sys/dev/block/" << major(dev) << ':' << minor(dev)
          ^~~~~

          src/mongo/db/storage/mmap_v1/mmap_v1_engine.cpp:170:82: error: 'minor' was not declared in this scope
          eam() << "/sys/dev/block/" << major(dev) << ':' << minor(dev)
          ^~~~~

          src/mongo/db/storage/mmap_v1/mmap_v1_engine.cpp:170:82: note: suggested alternative: 'mknod'
          eam() << "/sys/dev/block/" << major(dev) << ':' << minor(dev)
          ^~~~~
          mknod
          scons: *** [build/opt/mongo/db/storage/mmap_v1/mmap_v1_engine. o] Error 1
          scons: building terminated because of errors.

          =( it was working for several hours before it got to this error message, sadly it seems that there are too many errors

          1. I fixed this by adding this line:

            #include

            to src/mongo/db/storage/mmap_v1/mmap_v1_engine.cpp

      2. Did you find a solution? I’m getting the same error here. I also try other versions without luck (3.6.19 and 4.4.0). I need this solution because I can’t install Ubuntu.

  13. I initially started tackling each of these errors one by one. If you add the following to your SConstruct file in your appropriate ToolChainIs section (around line 1760 for me) it will keep the compiler from treating this warning as an error:

    AddToCCFLAGSIfSupported(myenv, ‘-Wno-parentheses’)

    This is a know problem with the Boost sources.

    However, after battling several more errors, I decided to comment out the -Werror flag here:

    #if env.TargetOSIs(‘linux’, ‘osx’, ‘solaris’):
    # if not has_option(“disable-warnings-as-errors”):
    # env.Append( CCFLAGS=[“-Werror”] )

    I wish I could tell you that fixed everything, but it’s been about 4 hours and its still compiling. I am trying to get 3.2 up and running on a Pi Zero w.

  14. Thank you very much. I built 3.2.22 mongod and mongo successfully on my RPi 3A with Raspberry Pi OS (formerly raspian) ‘buster’ and am currently following the suggestion to create a proper .deb package to satisfy dependencies.

    1. I try the version you specified, but I’m getting this error:

      from src/mongo/shell/dbshell.cpp:34:
      src/third_party/boost-1.56.0/boost/mpl/assert.hpp:188:21: error: unnecessary parentheses in declaration of ‘assert_arg’ [-Werror=parentheses]
      failed ************ (Pred::************
      ^
      src/third_party/boost-1.56.0/boost/mpl/assert.hpp:193:21: error: unnecessary parentheses in declaration of ‘assert_not_arg’ [-Werror=parentheses]
      failed ************ (boost::mpl::not_::************
      ^
      cc1plus: all warnings being treated as errors
      scons: *** [build/opt/mongo/shell/dbshell.o] Error 1
      scons: building terminated because of errors.

  15. Guys, if you get errors like “src/third_party/boost-1.56.0/boost/mpl/assert.hpp:188:21: warning: unnecessary parentheses in declaration of ‘assert_arg’ [-Wparentheses]”
    Just use this: scons –disable-warnings-as-errors core –wiredtiger=off –mmapv1=on
    @Koen you should put this in your code :p

  16. I was getting errors along the line of “mmap_v1_engine.cpp:176:70: error: ‘major’ was not declared in this scope…”
    Add the following line to mmap_v1_engine.cpp to correct this

    #include

    Successfully compiled on a Raspberry Pi3 B+ running Raspbian Buster

  17. Hi Guys

    I’m trying to compile mongo 3.2 on a raspi 4 8GB running 32bit Buster with gcc8 and the compiler stumbles from warning/error to warning/error. Can I really igonre them all?
    Did somebody manage to compile the src code with gcc 8 at all?
    I need mongo 3.2 because I want to run Rocket.Chat.

  18. @Brian Egge, thanks for the link to your wiki showing how to compile mongodb for RPi 32 bit.

    It works fine but I changed line 438 in your commands list from include %lt;sys/macros.h> to be include

    Thanks again for your contribution πŸ™‚

    1. Doh! That formatted wrong…. the include text between the less than and greater than symbols should read sys/sysmacros.h

      I hope that makes sense…

  19. Hello i have this error :

    scons: Reading SConscript files …
    File “/home/pi/install/mongodb-src-r3.2.12/SConstruct”, line 484

    print “version.json does not contain a version string”

    ^

    SyntaxError: Missing parentheses in call to ‘print’. Did you mean print(“version.json does not contain a version string”)?

    thanks for your help

Leave a Reply to Chris Cancel reply

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

The reCAPTCHA verification period has expired. Please reload the page.