Atlantic.Net Blog

How to Install MongoDB on Ubuntu 20.04

Verified and Tested 06/01/21

Introduction

In this How-To, we will walk you through the basic install and configuration of MongoDB on your Ubuntu 20.04 Cloud Server.

MongoDB is database software that stores specified information in one single table document instead of creating separate table documents for an item. This makes it easier to work with and space-efficient.

Install Required Dependency

First, you will need to install some dependencies to your system. You can install all of them with the following command:

apt-get install dirmngr gnupg apt-transport-https ca-certificates software-properties-common -y

Once all the dependencies are installed, you can proceed to the next step.

Installing MongoDB on Ubuntu

By default, the latest version of MongoDB is not included in the Ubuntu default repository. So you will need to add the MongoDB repository to your system.

First, download and add the GPG key with the following command:

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

Next,add the MongoDB repository to APT with the following command:

add-apt-repository 'deb [arch=amd64] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse'

Once the repository has been added, install the MongoDB with the following command:

apt-get install mongodb-org -y

Once the installation has been completed, start the Mongo service and enable it to start at system reboot:

systemctl enable --now mongod

Test MongoDB Installation

After installing MongoDB, you can verify the MongoDB version with the following command:

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

You should get the following output:

MongoDB shell version v4.4.6
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("e839ac21-1232-4863-87b3-2be8eeb2dde6") }
MongoDB server version: 4.4.6
{
	"authInfo" : {
		"authenticatedUsers" : [ ],
		"authenticatedUserRoles" : [ ]
	},
	"ok" : 1
}

Next, connect to the Mongo Shell with the following command:

mongo

Once you are connected, run the following command to test the MongoDB

> db.test.save( { atlantic: 100 } )

Output:

WriteResult({ "nInserted" : 1 })
> db.test.find()

Output:

{ "_id" : ObjectId("60b5e3ebe40bb5bb2fa369ae"), "atlantic" : 100 }

Next, exit from the MongoDB with the following command:

> exit

Congratulations! You have just installed MongoDB on your Ubuntu 20.04 VPS. Thank you for following along in this How-To and check back with us for any new updates.

Get a $250 Credit and Access to Our Free Tier!

Free Tier includes:
G3.2GB Cloud VPS a Free to Use for One Year
50 GB of Block Storage Free to Use for One Year
50 GB of Snapshots Free to Use for One Year