Install MongoDB on a Raspberry Pi

Background

At the current time (October 2023) your options for installing 64 bit MongoDB directly on a Raspberry Pi are somewhat limited. This is partly because as of MongoDB 5.0 (released July 2021), the official ARM64 version of MongoDB requires ARM v8.2-A or later microarchitecture1. However, Raspberry Pi model 4 has an ARM Cortex A722 which uses ARM v8.0-A microarchitecture3. Raspberry Pi 5 users should be ok since it has an ARM Cortex A764 which uses the ARM v8.2-A microarchitecture5, and therefore should in theory be able to install the latest version of Mongo – version 7.0 at the time of writing.

Implications for Raspberry Pi 4 and Earlier

Raspberry Pi model 4 and earlier will only support MongoDB v4 from the official Mango repositories, where the latest version is v4.4 (note: v4 will be end of life early in 20246). However, even with this option there are some complications over installation – Mongo seem to have half assed many of their builds as the official Mongo repos for Debian Bookworm7 and Bullseye8 are missing most of the ARM64 packages required for an install.

Turning to the official Mongo repos for Ubuntu – the latest, Jammy9, is also missing many of the required packages. Focal10 does seem to have a complete set of packages and so this is the one we would have to use. Even here however, there is a problem – the newest packages in the repo are v4.4.25, however any version newer than v4.4.18 causes a fatal error of Illegal instruction on a Pi 4, leading me to think that Mongo were already using instructions from the ARM v8.2-A architecture in these builds.

To summarize – if you have a Raspberry Pi 4 or earlier, and you want to use the official Mongo repo, then no matter which 64bit OS you are running, you will need to install Mongo DB v4.4.18 from the Ubuntu Focal repo. There are thankfully some unofficial builds of Mongo 6.0 and 7.0 that will work, see below.

Installation – Raspberry Pi 5

You should be able to install the latest version, 7.0 at time of writing, to an Ubuntu Jammy (or Debian Bullseye) OS using the following at a shell prompt (either directly or via SSH). Note that although there is a Mongo v7.0 repo for Debian Bullseye, it is also missing required packages, so you need to use the repo for Ubuntu.

In a command shell on the Pi or via SSH, add the repo to the Pi’s repo list (all one line):

echo 'deb [arch=arm64] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse' | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list

and then install the MongoDB 7.0 GPG key (all one line):

sudo wget -O /etc/apt/trusted.gpg.d/mongodb-repo.asc https://www.mongodb.org/static/pgp/server-7.0.asc

Install mongo:

sudo apt update
sudo apt install -y mongodb-org

Test the installation works by getting the Mongo version:

mongod --version

Installation of MongoDB 6.0 would be similar.

Installation – Raspberry Pi 4 (or older?)

Option 1 – MongoDB 4.4 From Official Repo

You need to use a 64-bit OS on your Pi, but it could be Raspberry Pi OS (formerly Raspbian) which is based off of Debian, or Ubuntu (also based off Debian).

In a command shell on the Pi or via SSH, add the repository to the Pi’s repo list (all one line):

echo 'deb [arch=arm64] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse' | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list

Install the MongoDB 7.0 GPG key (all one line):

sudo wget -O /etc/apt/trusted.gpg.d/mongodb-repo.asc https://www.mongodb.org/static/pgp/server-4.4.asc

Install mongodb v4.4.18 (later versions will not work – see Background above). The following are each a single line:

sudo apt update
sudo apt-get install -y mongodb-org=4.4.18 mongodb-org-server=4.4.18 mongodb-org-shell=4.4.18 mongodb-org-mongos=4.4.18 mongodb-org-tools=4.4.18

Make sure that the packages are not replaced during an update/upgrade (all one line):

sudo apt-mark hold mongodb-org mongodb-org-server mongodb-org-shell mongodb-org-mongos mongodb-org-tools

Test the installation works by getting the Mongo version:

mongod --version

If you see a fatal error with message containing signal=ILL or Illegal instruction it means you have somehow installed a version of Mongo that is not compatible with the Pi’s instruction set.

Option 2 – Unofficial MongoDB 6.0 and 7.0 Versions

An alternative to the official builds is to download the source for the version you want and build it. This would be somewhat slow on a Pi or you could go through the hassle of setting up the tool chain to cross compile on your desktop system .

Fortunately there are some prebuilt binaries available here, thanks to a Mongo employee who seems to have taken pity on the Raspberry community.

MongoDB As A Service

After installing mongo, if you need it to run automatically as a service at boot:

sudo systemctl enable mongod

and want to start it now:

sudo systemctl start mongod

and check its status:

sudo systemctl status mongod


References:

  1. https://www.mongodb.com/docs/manual/administration/production-notes/#arm64 ↩︎
  2. https://en.wikipedia.org/wiki/Raspberry_Pi#:~:text=The%20Raspberry%20Pi%205%20uses,GPU%20clocked%20at%20800%20MHz. ↩︎
  3. https://en.wikipedia.org/wiki/List_of_ARM_processors ↩︎
  4. https://www.raspberrypi.com/products/raspberry-pi-5/#specification ↩︎
  5. https://en.wikipedia.org/wiki/List_of_ARM_processors ↩︎
  6. https://www.mongodb.com/support-policy/lifecycles ↩︎
  7. https://repo.mongodb.org/apt/debian/dists/bookworm/mongodb-org/4.4/main/binary-arm64/ ↩︎
  8. https://repo.mongodb.org/apt/debian/dists/bullseye/mongodb-org/4.4/main/binary-arm64/ ↩︎
  9. https://repo.mongodb.org/apt/ubuntu/dists/jammy/mongodb-org/4.4/multiverse/binary-arm64/ ↩︎
  10. https://repo.mongodb.org/apt/ubuntu/dists/focal/mongodb-org/4.4/multiverse/binary-arm64/ ↩︎