Tutorial

How To Install MongoDB on Ubuntu 20.04

Published on July 31, 2020

Manager, Developer Education

English
How To Install MongoDB on Ubuntu 20.04
Not using Ubuntu 20.04?Choose a different version or distribution.
Ubuntu 20.04

An earlier version of this tutorial was written by Melissa Anderson.

Introduction

MongoDB is a document database used in many modern web applications. It is classified as a NoSQL database because it does not rely on a traditional table-based relational database structure.

Instead, it uses JSON-like documents with dynamic schemas, meaning that, unlike relational databases, MongoDB does not require a predefined schema before you add data to a database. You can alter the schema at any time and as often as is necessary without having to set up a new database with an updated schema.

In this tutorial you’ll install MongoDB on an Ubuntu 20.04 server, test it, and learn how to manage it as a systemd service.

Prerequisites

To follow this tutorial, you will need:

1-Click deploy a MongoDB database using DigitalOcean Managed Databases. Let DigitalOcean focus on scaling, maintenance, and upgrades for your database.

Step 1 — Installing MongoDB

Ubuntu’s official package repositories include a stable version of MongoDB. However, as of this writing, the version of MongoDB available from the default Ubuntu repositories is 3.6, while the latest stable release is 4.4.

To obtain the most recent version of this software, you must include MongoDB’s dedicated package repository to your APT sources. Then, you’ll be able to install mongodb-org, a meta-package that always points to the latest version of MongoDB.

To start, import the public GPG key for the latest stable version of MongoDB by running the following command. If you intend to use a version of MongoDB other than 4.4, be sure to change 4.4 in the URL portion of this command to align with the version you want to install:

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

cURL is a command line tool available on many operating systems used to transfer data. It reads whatever data is stored at the URL passed to it and prints the content to the system’s output. In the following example, cURL prints the content of the GPG key file and then pipes it into the following sudo apt-key add - command, thereby adding the GPG key to your list of trusted keys.

Also, note that this curl command uses the options -fsSL which, together, essentially tell cURL to fail silently. This means that if for some reason cURL isn’t able to contact the GPG server or the GPG server is down, it won’t accidentally add the resulting error code to your list of trusted keys.

This command will return OK if the key was added successfully:

Output
OK

If you’d like to double check that the key was added correctly, you can do so with the following command:

  1. apt-key list

This will return the MongoDB key somewhere in the output:

Output
/etc/apt/trusted.gpg -------------------- pub rsa4096 2019-05-28 [SC] [expires: 2024-05-26] 2069 1EEC 3521 6C63 CAF6 6CE1 6564 08E3 90CF B1F5 uid [ unknown] MongoDB 4.4 Release Signing Key <packaging@mongodb.com> . . .

At this point, your APT installation still doesn’t know where to find the mongodb-org package you need to install the latest version of MongoDB.

There are two places on your server where APT looks for online sources of packages to download and install: the sources.list file and the sources.list.d directory. sources.list is a file that lists active sources of APT data, with one source per line and the most preferred sources listed first. The sources.list.d directory allows you to add such sources.list entries as separate files.

Run the following command, which creates a file in the sources.list.d directory named mongodb-org-4.4.list. The only content in this file is a single line reading deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse:

  1. echo "deb [ arch=amd64,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

This single line tells APT everything it needs to know about what the source is and where to find it:

  • deb: This means that the source entry references a regular Debian architecture. In other cases, this part of the line might read deb-src, which means the source entry represents a Debian distribution’s source code.
  • [ arch=amd64,arm64 ]: This specifies which architectures the APT data should be downloaded to. In this case, it specifies the amd64 and arm64 architectures.
  • https://repo.mongodb.org/apt/ubuntu: This is a URI representing the location where the APT data can be found. In this case, the URI points to the HTTPS address where the official MongoDB repository is located.
  • focal/mongodb-org/4.4: Ubuntu repositories can contain several different releases. This specifies that you only want version 4.4 of the mongodb-org package available for the focal release of Ubuntu (“Focal Fossa” being the code name of Ubuntu 20.04).
  • multiverse: This part points APT to one of the four main Ubuntu repositories. In this case, it’s pointing to the multiverse repository.

After running this command, update your server’s local package index so APT knows where to find the mongodb-org package:

  1. sudo apt update

Following that, you can install MongoDB:

  1. sudo apt install mongodb-org

When prompted, press Y and then ENTER to confirm that you want to install the package.

When the command finishes, MongoDB will be installed on your system. However it isn’t yet ready to use. Next, you’ll start MongoDB and confirm that it’s working correctly.

Step 2 — Starting the MongoDB Service and Testing the Database

The installation process described in the previous step automatically configures MongoDB to run as a daemon controlled by systemd, meaning you can manage MongoDB using the various systemctl commands. However, this installation procedure doesn’t automatically start the service.

Run the following systemctl command to start the MongoDB service:

  1. sudo systemctl start mongod.service

Then check the service’s status. Notice that this command doesn’t include .service in the service file definition. systemctl will append this suffix to whatever argument you pass automatically if it isn’t already present, so it isn’t necessary to include it:

  1. sudo systemctl status mongod

This command will return output like the following, indicating that the service is up and running:

Output
● mongod.service - MongoDB Database Server Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled) Active: active (running) since Tue 2020-06-09 12:57:06 UTC; 2s ago Docs: https://docs.mongodb.org/manual Main PID: 37128 (mongod) Memory: 64.8M CGroup: /system.slice/mongod.service └─37128 /usr/bin/mongod --config /etc/mongod.conf

After confirming that the service is running as expected, enable the MongoDB service to start up at boot:

  1. sudo systemctl enable mongod

You can further verify that the database is operational by connecting to the database server and executing a diagnostic command. The following command will connect to the database and output its current version, server address, and port. It will also return the result of MongoDB’s internal connectionStatus command:

  1. mongo --eval 'db.runCommand({ connectionStatus: 1 })'

connectionStatus will check and return the status of the database connection. A value of 1 for the ok field in the response indicates that the server is working as expected:

Output
MongoDB shell version v4.4.0 connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("1dc7d67a-0af5-4394-b9c4-8a6db3ff7e64") } MongoDB server version: 4.4.0 { "authInfo" : { "authenticatedUsers" : [ ], "authenticatedUserRoles" : [ ] }, "ok" : 1 }

Also, note that the database is running on port 27017 on 127.0.0.1, the local loopback address representing localhost. This is MongoDB’s default port number.

Next, we’ll look at how to manage the MongoDB server instance with systemd.

Step 3 — Managing the MongoDB Service

As mentioned previously, the installation process described in Step 1 configures MongoDB to run as a systemd service. This means that you can manage it using standard systemctl commands as you would with other Ubuntu system services.

As mentioned previously, the systemctl status command checks the status of the MongoDB service:

  1. sudo systemctl status mongod

You can stop the service anytime by typing:

  1. sudo systemctl stop mongod

To start the service when it’s stopped, run:

  1. sudo systemctl start mongod

You can also restart the server when it’s already running:

  1. sudo systemctl restart mongod

In Step 2, you enabled MongoDB to start automatically with the server. If you ever wish to disable this automatic startup, type:

  1. sudo systemctl disable mongod

Then to re-enable it to start up at boot, run the enable command again:

  1. sudo systemctl enable mongod

For more information on how to manage systemd services, check out Systemd Essentials: Working with Services, Units, and the Journal.

Conclusion

In this tutorial, you added the official MongoDB repository to your APT instance, and installed the latest version of MongoDB. You then tested Mongo’s functionality and practiced some systemctl commands.

As an immediate next step, we strongly recommend that you harden your MongoDB installation’s security by following our guide on How To Secure MongoDB on Ubuntu 20.04. Once it’s secured, you could then configure MongoDB to accept remote connections.

You can find more tutorials on how to configure and use MongoDB in these DigitalOcean community articles. We also encourage you to check out the official MongoDB documentation, as it’s a great resource on the possibilities that MongoDB provides.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products


Tutorial Series: Getting Started With Cloud Computing

This curriculum introduces open-source cloud computing to a general audience along with the skills necessary to deploy applications and websites securely to the cloud.

About the authors
Default avatar

Manager, Developer Education

Technical Writer @ DigitalOcean

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
4 Comments


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

The tutorial is okay but after the install I tried to start the mongo service but it failed with the exit-code

I used the following commands to fix it and it worked.

chown -R mongodb:mongodb /var/lib/mongodb
chown mongodb:mongodb /tmp/mongodb-27017.sock

worked like a charm !

Hi, thanks for your helpful article.

I have used your instructions once and it worked fine. But I’m doing the same thing in my new server, and surprisingly it’s not working!

after line:

sudo apt update

in step 1, I get the following error:

E: Failed to fetch https://repo.mongodb.org/apt/ubuntu/dists/focal/mongodb-org/4.4/InRelease  403  Forbidden [IP: 65.9.58.40 443]
E: The repository 'https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 InRelease' is not signed.

Thanks for the tutorial, but I’m stuck!

When I try to run the cURL command to get the key added, I get the following error:

curl: (77) error setting certificate verify locations: CAfile: /etc/ssl/certs/ca-certificates.crt CApath: /etc/ssl/certs Warning: apt-key is deprecated. Manage keyring files in trusted.gpg.d instead(see apt-key(8)). gpg: no valid OpenPGP data found.

What’s weird is, when I run sudo apt-key list, it shows MongoDB 4.4 key has been added… but when I run sudo apt update, I get an error basically saying there’s no certificate.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.