Install the Latest Version of MongoDB on Ubuntu 20.04/18.04/16.04

mail
Cyrus Kao
Last modified

MongoDB is one of the most popular NoSQL databases around. In this tutorial we'll be covering how to install the latest version of MongoDB Community Server from its official repository, and the following steps work for Ubuntu 20.04 LTS (Focal), 18.04 LTS (Bionic) and 16.04 LTS (Xenial).

Alternatively, you can install the specific version of MongoDB with the .deb packages provided from the official download page (not recommended):

Download page
Download page of MongoDB

Prerequisites

We'll first import the signing key from MongoDB to verify we're downloading from its official, then add the repository to our package lists so we can install it with APT.

Import GPG Key

Install gnupg if you don't have it on your system already:

sudo apt install gnupg
Bash

Import the public GPG key from MongoDB official:

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

Add Repository

Add the official repository to your APT package lists:

  • Ubuntu 20.04 (Focal)

    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
    Bash
  • Ubuntu 18.04 (Bionic)

    echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list
    Bash
  • Ubuntu 16.04 (Xenial)

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

Installation

Update the package lists and install mongodb-org:

sudo apt update
sudo apt install mongodb-org
Bash

Update to Latest Version

Same goes with updating, update the package lists first then upgrade mongodb-org if newer version is released:

sudo apt update
sudo apt --only-upgrade install mongodb-org
Bash

Start Service

Enable and start the MongoDB service (mongod.service):

sudo systemctl enable --now mongod
Bash

Check if MongoDB is up and running:

sudo systemctl status mongod
Bash
● mongod.service - MongoDB Database Server
     Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2022-02-08 08:31:40 UTC; 4 days ago
       Docs: https://docs.mongodb.org/manual
   Main PID: 892 (mongod)
     Memory: 267.6M
     CGroup: /system.slice/mongod.service
             └─892 /usr/bin/mongod --config /etc/mongod.conf
Output

See Also

Comments

0500