Install MongoDB and Node.js on a Raspberry Pi

This tutorial is the second part of the series on raspberry pi. In this part we will focus on the installation of MongoDB and Node.js that will run at startup. MongoDB is a popular NOSQL database that is often used with Node.js which is a JavaScript runtime mainly used to build server-side applications.


Let’s get started with the MongoDB installation which is pretty simple:

$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get install mongodb-server

And to start it as a service when the raspberry pi starts just have to enter this command:

$ sudo service mongod start

The binaries are stored in the /usr/bin/ folder while the datas are in the /var/lib/mongodb/ folder. You can check everything is ok by using the mongo shell:

$ mongo


Now we are going to install the Node.js server on our raspberry pi and put it as a service. Firstly download the latest version:

$ wget

You can download another version here but be careful to take the linux-armv7l distribution.

Once the archive downloaded, extract the package, move it in the /opt/node folder and create the symbolic links:

$ tar -xvzf node-v5.11.0-linux-armv7l.tar.gz
$ sudo mv node-v5.11.0-linux-armv7l /opt/node

$ sudo mkdir /opt/bin

$ sudo ln -s /opt/node/bin/* /opt/bin/

To finish the installation, let’s add the binaries in the PATH:

$ sudo nano /etc/profile


Press CTRL+O then CTRL+X to save the file. Voilà! The installation is done, check that everything is ok by taping these commands:

$ npm --version
$ node -v

Run your web server as a service

Now that you have node and npm installed on your raspberry pi 2 you can work with your web server. Imagine you have a really simple on the /home/pi/dev/node folder named app.js with this code:

const http = require('http');

const hostname = '';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World\n');

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);

Here is just a hello world to illustrate the tutorial that you can run using this command node /home/pi/dev/node/app.js

To run it as a service, we are firstly going to create a file in the /etc/init.d/ folder to have our own service. Let’s create a node file:

$ sudo nano /etc/init.d/node

And put the following content:

# /etc/init.d/node

if [ true != "$INIT_D_SCRIPT_SOURCED" ] ; then
    set "$0" "$@"; INIT_D_SCRIPT_SOURCED=true . /lib/init/init-d-script

# Provides:          node
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Starts the DAEMON_PATH/DAEMONOPTS server
# Description:       Starts the DAEMON_PATH/DAEMONOPTS server

export PATH=$PATH:/opt/node/bin



case "$1" in
    printf "%-50s" "Starting $NAME..."
    PID=`$DAEMON $DAEMONOPTS > /dev/null 2>&1 & echo $!`
    #echo "Saving PID" $PID " to " $PIDFILE
    if [ -z $PID ]; then
        printf "%s\n" "Fail"
        echo $PID > $PIDFILE
        printf "%s\n" "Ok"
    printf "%-50s" "Checking $NAME..."
    if [ -f $PIDFILE ]; then
        PID=`cat $PIDFILE`
        if [ -z "`ps axf | grep ${PID} | grep -v grep`" ]; then
            printf "%s\n" "Process dead but pidfile exists"
            echo "Running"
        printf "%s\n" "Service not running"
    printf "%-50s" "Stopping $NAME"
    PID=`cat $PIDFILE`
    if [ -f $PIDFILE ]; then
        kill -HUP $PID
        printf "%s\n" "Ok"
        rm -f $PIDFILE
        printf "%s\n" "pidfile not found"
    $0 stop
    $0 start

    echo "Usage: $0 {status|start|stop|restart}"
    exit 1

exit 0                                

This file simply describes how to service should start or stop. Then you can start your custom service using this command:

$ sudo service node start

You just have to restart your raspberry pi 2 and everything should be ok. If not try this command line sudo update-rc.d node defaults


The installation of MongoDB and Node.js is pretty simple. If you have some troubles or questions don’t hesitate to leave a comment.


1 Star2 Stars3 Stars4 Stars5 Stars (8 votes, average: 4.88 out of 5)
Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInShare on RedditPin on Pinterest


Time limit is exhausted. Please reload CAPTCHA.

  1. Ken · April 24

    cant get mongo to work!!! on latest raspbian with a Pi2

  2. Aegis · April 24

    Hello guys, been using this tutorial to install NodeJS on my Raspberry Pi 3 and all work except the “service” part. I put the app.js file exactly as said and place the node file in /etc/init.d/ but as i try to run it by using sudo service node start i got the following :

    Failed to start node.service: Unit node.service failed to load: No such file or directory.

    Can you explain me where i went wrong ?

    • Aegis · April 24

      Ok so find the mistake. There is a double quote that is not actually correct right after “app.js”. I removed it and put a correct double quote, then “systemctl daemon-reload” and it works

      • nad · April 24

        Thank you!

      • JD · April 24


  3. John Wargo · April 24

    There’s a typo in the instructions:
    sudo apt-get mongodb-server
    should be:
    sudo apt-get install mongodb-server

  4. PaulB · April 24

    When starting Mongodb the command should be:-
    sudo service mongodb start NOT sudoservice mongod start as above!!

  5. Victor · April 24

    Hello, if for one reason the web server crashes, it will restar automatically using this configuration? or I have to do something else? Thanks

  6. m · April 24

    to run mongod
    sudo /etc/init.d/mongodb start

  7. Matt · 21 Days Ago

    sudo service mongod start
    doesn’t work for me, I keep getting:
    Failed to start mongod.service: Unit mongod.service failed to load: No such file or directory.
    Any idea?
    Setup: Raspberry 3 with a fresh+unaltered install of Raspbian 4.4 (download today).

  8. Pradip · 19 Days Ago

    Getting following error:

    pi@raspberrypi ~ $ sudo apt-get install mongodb-server
    Reading package lists… Done
    Building dependency tree
    Reading state information… Done
    Package mongodb-server is not available, but is referred to by another package.
    This may mean that the package is missing, has been obsoleted, or
    is only available from another source

    E: Package ‘mongodb-server’ has no installation candidate

    • Mitch · 15 Days Ago

      Thanks for the post, for me this worked fine on my Pi 3, but maybe it’s because I tried earlier in June when there’s wasn’t an Arm7 release for the Pi. AT that time I followed Andy Felong’s setup recommendations which may have paved the way for this success.

      I had compiled a Mongo 3 release, but the binary was 160 times the size of this Mongo 2 release.

      My advice to others would be to review any setup prerequisites in the Mongo install docs first. It’s likely that in time these issues will be baked into the package.