How to Install MongoDB 5 on Ubuntu 20.04

In this tutorial, we’re going to show you how to install MongoDB on an Ubuntu 20.04 server. Beginner-friendly, detailed, step-by-step instructions.

Requirements

This is what you’ll need:

  • Sudo/root access to the server
  • An Ubuntu 20.04 server. You can get one from Vultr or Linode. You can also use SolaDrive, in which case they will manage the server for you and install MongoDB for you for free.
  • Basic knowledge of the CLI.

This tutorial is for the community edition of MongoDB. As of writing, the latest stable MongoDB release is 5.0

We’ll use the mongodb-org package that’s maintained by MongoDB themselves, instead of the default one that’s already included in Ubuntu’s repositories, which is usually an older version.

So, connect to your server and follow the steps:

Step 1: Update Ubuntu

First, update your server with the following commands:

sudo apt-get update
sudo apt-get upgrade

Step 2: Add the MongoDB public GPG key

Run the following command to add the GPG key:

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

Step 3: Create MongoDB a list file

To create a list file for MongoDB, run the following 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

Step 4: Update packages list

Update the packages again:

sudo apt-get update

And now, you’re finally ready to install MongoDB.

Step 5: Install MongoDB

Run the following command to install MongoDB Community Edition 5.0 on Ubuntu 20.04:

sudo apt-get install mongodb-org

And that’s it! MongoDB is installed.

Now we can move on to using MongoDB.

Step 6: Start MongoDB and enable it at boot

Next, start the MongoDB service:

sudo systemctl start mongod

Check the status and verify that it’s running:

sudo systemctl status mongod

And enable it to start on boot:

sudo systemctl enable mongod

Step 7: Configure MongoDB

By default, the configuration file for MongoDB is located at “/etc/mongod.conf”. You can do various configurations and edits via that file.

Enable password authentication

To enable password authentication (which means users will have to log in with a username and password to make read or edit the database), open the configuration file:

sudo nano /etc/mongod.conf

Find the line that says #security and uncomment it (remove the #) and add “authorization: enabled”:

security:
  authorization: enabled

Restart the MongoDB service for the changes to take effect:

sudo systemctl restart mongod

Enable remote access

To enable remote access to your MongoDB database, you need to edit the configuration file again. More specifically, the “network interfaces” section. You need to add the MongoDB server password to the bindIp line:

sudo nano /etc/mongod.conf
# network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1,your.mongodb.server.ip

After you’ve added the IP, restart the service:

sudo systemctl restart mongod

And if you’re using a firewall, allow access for port 27017. It’s recommended that you only allow access from a specific IP address (the IP of the server you’re gonna connect to the database from). You can use UFW:

sudo ufw allow from your.mongodb.server.ip to any port 27017

And that’s pretty much it!

What’s the difference between packages mongodb and mongodb-org?

The package “mongodb” is included in Ubuntu’s repositories by default, but it’s usually an older version and it’s not maintained by the official MongoDB organization. On the other hand, mongodb-org is maintained by themselves and it’s more up-to-date. It’s recommended to use mongodb-org, but you can use mongodb too.

This post was last modified on August 18, 2021 2:36 am

Categories: Knowledgebase
Tags: MongoDB

View Comments (1)

  • Dear Friend, thanks for your tutorial.

    However, I missed the part where you create an admin user. This must be done *before* enabling password authentication.

Related Post