How to Install MongoDB on Ubuntu 20.04

MongoDB is one of the most popular NoSQL databases, it is open source and freely available to download. It stores data in the form of flexible documents like JSON rather than the usual table style method found in SQL databases. MongoDB is used to store and work on data without the use of schema or ordering patterns. The MongoDB NoSQL database is widely used in web applications because it has easier integration with various programming languages.
This tutorial describes how to install and configure MongoDB Community Edition on Ubuntu 20.04.

Installing MongoDB on Ubuntu 20.04

To install the latest version of MongoDB Community Edition on your Ubuntu server, you need to install necessary dependencies as shown.

$ sudo apt update
$ sudo apt install dirmngr gnupg apt-transport-https ca-certificates software-properties-common

Import the repository’s GPG key and add the MongoDB repository with:

$ wget -qO - | sudo apt-key add -
$ sudo add-apt-repository 'deb [arch=amd64] focal/mongodb-org/4.4 multiverse'

At the time of writing this article, the latest version of MongoDB is version 4.4. To install another version, replace 4.4 with your preferred version.
Once the repository is enabled, install the mongodb-org meta-package by typing:

$ sudo apt install mongodb-org

During the MongoDB installation, it will create the configuration file /etc/mongod.conf, data directory /var/lib/mongodb and the log directory /var/log/mongodb.
By default, MongoDB runs using the mongodb user account. If you change the user, you must also change the permission to the data and log directories to assign access to these directories.
Then start and verify the mongod process by running the following command.

$ sudo systemctl start mongod 
$ sudo systemctl status mongod

To verify whether the installation has completed successfully, connect to the MongoDB database server using the mongo tool, and print the connection status:

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

The output will look something like below:

MongoDB shell version v4.4.0
connecting to: mongodb://
Implicit session: session { "id" : UUID("2af3ab0e-2197-4152-8bd0-e33efffe1464") }
MongoDB server version: 4.4.0
  "authInfo" : {
    "authenticatedUsers" : [ ],
    "authenticatedUserRoles" : [ ]
  "ok" : 1

A value of 1 for the ok field indicates success.

Uninstall MongoDB Community Edition

To completely remove MongoDB including MongoDB applications, the configuration files, and any directories containing data and logs, issue the following commands.

$ sudo service mongod stop
$ sudo apt-get purge mongodb-org*
$ sudo rm -r /var/log/mongodb
$ sudo rm -r /var/lib/mongodb


In the above guide, you learned how to install MongoDB on Ubuntu 20.04 server. For more information, you can visit the MongoDB documentation at Mongo Doc.

Leave a Reply

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to top button