Debian MongoDB

Install MongoDB 4 on Debian 10

MongoDB is a NoSQL database. The latest version of MongoDB is version 4. Officially, MongoDB has package repositories for Debian 8 and Debian 9. MongoDB does not have any packages or repositories for Debian 10 at the time of this writing. If you try to use Debian 9 package repository of MongoDB on Debian 10, you will see that there are some unmet dependencies. This method of installing MongoDB 4 on Debian 10 is only for testing purpose. For the production environment, you should wait till MongoDB is officially released on Debian 10. But in any case we show you how to get it done now.

In this article, I am going to show you how to install MongoDB 4 on Debian 10 and solve the unmet dependency problems. I will also give you hints along the way to install MongoDB when Debian 10 is officially supported.

I will be using minimal installation of Debian 10 operating system for the demonstration. But, it will also work on Debian 10 desktop operating systems. So, let’s get started.

Adding GPG Key of MongoDB Repository:

You have to install wget and gnupg in order to download and add the GPG key of MongoDB official package repository to Debian 10.

First, update the APT package repository cache with the following command:

$ sudo apt update

The APT package repository cache should be updated.

Now, install wget and gnupg with the following command:

$ sudo apt install wget gnupg

Now, press Y and then press <Enter> to confirm the installation.

wget and gnupg should be installed.

Now, run the following command to add the GPG key of the MongoDB official package repository on your Debian 10 machine.

$ wget -qO - https://www.mongodb.org/static/pgp/server-4.0.asc
| sudo apt-key add -

The GPG key should be added.

Adding the Official Package Repository of MongoDB 4:

At the time of this writing, there is no official package repository of MongoDB 4 for Debian 10. But, we can use the Debian 9 package repository on Debian 10 as long as there is no official package repository is available for Debian 10.

To add the official MongoDB 4 package repository for Debian 9 on Debian 10, run the following command.

$ echo "deb http://repo.mongodb.org/apt/debian stretch/mongodb-org/4.0 main"
| sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list

The official MongoDB 4 package repository of Debian 9 should be added to your Debian 10 machine.

NOTE: If in future, there is any official MongoDB package repository available for Debian 10, you may be able to add it as follows.

$ echo "deb http://repo.mongodb.org/apt/debian buster/mongodb-org/4.0 main"
| sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list

Now, update the APT package repository cache with the following command:

$ sudo apt update

Installing MongoDB 4:

To install MongoDB 4, you have to run the following command.

$ sudo apt install mongodb-org

If MongoDB had an official Debian 10 package repository, then this command would be enough to install MongoDB 4. As I am using official MongoDB package repository of Debian 9 on Debian 10, if I try to install MongoDB 4 like this, I will get an error as you can see in the screenshot below.

Let’s try to see why mongodb-org-server is not going to be installed.

$ sudo apt install mongodb-org-server

Well, mongodb-org-server requires libcurl3. But, Debian 10 uses libcurl4. It does not have any libcurl3 support.

To solve this problem, we can add the official Debian 9 package repository on Debian 10 and install libcurl3 library package from there.

To add the official Debian 9 package repository on Debian 10, run the following command:

$ echo "deb http://deb.debian.org/debian/ stretch main"
| sudo tee /etc/apt/sources.list.d/debian-stretch.list

The official Debian 9 package repository should be added on your Debian 10 machine.

Now, update the APT package repository cache with the following command:

$ sudo apt update

The APT package repository cache should be updated.

Now, install libcurl3 with the following command:

$ sudo apt install libcurl3

Now, press Y and then press <Enter> to confirm the installation.

libcurl3 should be installed.

Now, install MongoDB 4 with the following command:

$ sudo apt install mongodb-org

Now, press Y and then press <Enter> to confirm the installation.

MongoDB 4 should be installed.

Now, run the following command to check whether MongoDB 4 is working correctly.

$ mongod --version

As you can see, I am running MongoDB 4.0.11 and it’s working correctly.

Managing MongoDB Services:

Now, check whether MongoDB service is running with the following command:

$ sudo systemctl status mongod

As you can see, the MongoDB service is inactive, which means it’s not running. The MongoDB service is also disabled, which means it is not added to the system startup yet. So, it won’t start automatically on system reboot.

Now, start the MongoDB service with the following command:

$ sudo systemctl start mongod

If you check the MongoDB service status again, MongoDB service should be active/running as you can see in the screenshot below. Great!

$ sudo systemctl status mongod

Now, add MongoDB service to the system startup of your Debian 10 machine with the following command:

$ sudo systemctl enable mongod

MongoDB service should be added to the system startup and it will start automatically on system boot from now on.

To remove MongoDB service from the system startup, run the following command:

$ sudo systemctl disable mongod

If you want to stop the MongoDB service, run the following command:

$ sudo systemctl stop mongod

If you want to restart the MongoDB service, run the following command:

$ sudo systemctl restart mongod

You should be connected to the MongoDB 4 server. Here, you can run any MongoDB 4 queries.

Now, create a new database test and use it as follows:

> use test;

Now, create a new collection users in the test database and insert an entry to the users collection as follows:

> db.users.insert({user: 'Bob', country: 'USA'});

Now, list all the entries of the users collection as follows:

> db.users.find();

As you can see, the entry we’ve inserted earlier is listed. So, MongoDB is working correctly.

Now, exit out of Mongo Shell with the following query.

> exit

So, that’s how you install MongoDB 4 on Debian 10. Thanks for reading this article.

About the author

Shahriar Shovon

Shahriar Shovon

Freelancer & Linux System Administrator. Also loves Web API development with Node.js and JavaScript. I was born in Bangladesh. I am currently studying Electronics and Communication Engineering at Khulna University of Engineering & Technology (KUET), one of the demanding public engineering universities of Bangladesh.