Back to Article List

How to install MongoDB on Ubuntu 20.04

www.mongodb.com

MongoDB is a source-available cross-platform document-oriented database program. Classified as a NoSQL database program, MongoDB uses JSON-like documents with optional schemas. MongoDB is developed by MongoDB Inc. and licensed under the Server Side Public License. — Wikipedia

Prerequisites

Before we start the process of installing MongoDB, first we need a few things.

  • A webserver running Ubuntu 20.04. For this tutorial, I am using a dedicated server from Spin Servers.
  • A non-root user
  • A firewall configured with UFW (uncomplicated firewall)

*For simplicity of this article I will use the root user account. In a live environment, you should always take steps to ensure the operation of a secure server.

Update your system

I always check my system for a quick update before doing any work. You can do so with the command:

$ sudo apt-get update

If you have a few updates finish those up and let's get started.

Step 1 — Import Public Key (GPG)

There are two ways we can go about installing MongoDB. The first, by using Ubuntu’s package repositories, and the second by using MongoDB’s repositories. You will find the version Ubuntu provides can be an older version than what MongoDB themselves will provide.

To keep up with the current version, 5.0, we will use MongoDB’s dedicated repository.

First, we need to import the public (GPG) key using cURL.

$ curl -fsSL https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -

If the operation goes successfully, the command will return an OK .

If you receive an error that gnupg in not installed, you can install with the following command:

$ sudo apt-get install gnupg

Then retry importing the key.

Step 2 — Create a list file & Install MongoDB

Now that we have our GPG key, we need to create a list file, /etc/apt/sources.list.d/mongodb-org-5.0.list for your version of Ubuntu. In this case, it's 20.04.

To create this file use command:

echo “deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0 multiverse” | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list

After adding the list file, run the update command.

$ sudo apt update

Install MongoDB.

$ sudo apt install mongodb-org

You will be asked a Y/N question, type Y, and press enter.

MongoDB is now installed on your server, but, you will need to start the service and confirm it’s operating correctly.

Step 3 — Starting MongoDB

Since the install process doesn’t manually start the service, we will need to take care of that.

Using systemctl , we will use the command below to start the service.

$ sudo systemctl start mongod.service

The service should now be running and we can check the status of the process by using the command:

$ sudo systemctl status mongod

You should receive the output above.

Now that we know the service is running correctly, we need to enable MongoDB to startup at boot.

$ sudo systemctl enable mongod

You now have a successful install of MongoDB on your Ubuntu 20.04 webserver! You can view more information about MongoDB on their website at https://docs.mongodb.com/

If you have questions about any of our services, please contact us via email at[email protected] or by using our live chat at spinservers.com.

Spin Servers is a US-based provider of dedicated servers & affordable private cloud solutions. Our motto is to let you “spin up servers” fast, seamlessly, and affordably.