This post shows students and new users the steps to install MongoDB database server on Ubuntu Linux. MongoDB, a free open source, schema-free document-oriented database that can be used to build powerful websites and applications.

MongoDB uses a flexible Similar to JSON document data store where fields are not unique and may vary from document to document. It also does not require a predefined schema and the data structure can be changed at any time during data modifications.

The steps below will show you how to install and configure MongoDB Community Edition on Ubuntu Linux. Installation is pretty straightforward.

The MongoDB packages are included in the default Ubuntu repositories, however the Ubuntu repositories versions are not the latest. To install the latest version, you will need to install the MongoDB package repository on Ubuntu Linux, and this tutorial will show you how.

Also, for students and new users learning Linux, the easiest place to start learning is Ubuntu Linux. Ubuntu is the modern open source Linux operating system for desktops, servers, and other devices.

To start installing MongoDB on Ubuntu Linux, follow the steps below.

How to add the MongoDB repository on Ubuntu Linux

As mentioned above, one can simply run apt get install command on Ubuntu to download and install MongoDB. However, the versions of the Ubuntu repositories are usually not the most recent.

To install the latter, we will have to add the MongoDB repository to Ubuntu Linux. But first, run the following commands to install the necessary packages and dependencies.

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

Then import the MongoDB repository key and create a repository file with the following commands.

At the time of writing, the latest version of MongoDB is the version 5.0. You can visit the link below for details on the future version number, then replace the commands below.

MongoDB repositories

wget -qO - | sudo apt-key add -

Then run the following commands to create a repository file for the MongoDB version 5.0.

echo "deb [ arch=amd64,arm64 ] focal/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list

Also, the repository above is for Ubuntu 20.04 (focal). If you are using another version of Ubuntu Linux, be sure to replace focal with the codename of that version. Ubuntu 18.04 will be (bionic).

How to install MongoDB on Ubuntu Linux

After the repository is created and enabled, run the following commands to update the Ubuntu package index and install MongoDB.

sudo apt update
sudo apt install mongodb-org

The above commands will install the following packages along with the MongoDB kernel.

The following packages will be installed on your system:

  • server-org-mongodb – MongoDB server
  • mongodb-org-mongos – MongoDB Daemon
  • mongodb-org-shell – The mongo shell, an interactive JavaScript interface for MongoDB.
  • mongodb-org-tools – MongoDB tools to import and export data

After installing MongoDB, the following commands can be used to stop, start, and allow MongoDB to start automatically when systems start.

sudo systemctl stop mongod
sudo systemctl start mongod
sudo systemctl enable mongod

By default, MongoDB listens on the port 27017. After installation, the local server should be able to communicate with MongoDB. To check if MongoDB is running and active, run the following commands:

sudo systemctl status mongod

You should see something like the following lines:

mongod.service - MongoDB Database Server
     Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled)
     Active: active (running) since Thu 2020-05-21 12:37:32 CDT; 52s ago
   Main PID: 3409 (mongod)
     Memory: 72.7M
     CGroup: /system.slice/mongod.service
             └─3409 /usr/bin/mongod --config /etc/mongod.conf

May 21 12:37:32 ubuntu2004 systemd[1]: Started MongoDB Database Server.

To connect to the MongoDB shell, run the following commands:

mongo --host

You should see something like the following lines:

ongoDB shell version v4.2.6
connecting to: mongodb://
Implicit session: session { "id" : UUID("1b88c27f-e477-4a29-b562-835ee56591b5") }
MongoDB server version: 4.2.6
Welcome to the MongoDB shell.
For interactive help, type "help".

MongoDB configuration on Ubuntu Linux

The MongoDB configuration file is called mongod.conf and is in the / etc directory. The file is in YAML Format.

The default settings are sufficient for most environments. However, if you want to enable authentication and other settings, you will need to modify your configuration file.

For example, you can enable authentication by changing the line in the configuration file to match the following.

Open the configuration file as root:

sudo nano /etc/mongod.conf

Then change the line below to enable authentication.

  authorization: enabled

Save and restart MondoDB.

How to add MongoDB administrator

As mentioned above, authentication is not enabled for MongoDB and the administrator account is not enabled by default. In a production environment, it may be necessary to protect your server and enable user authentication.

If you want to enable authentication, run the commands to create a new administrator user after you have logged into the MongoDB server.

Access the MongoDB shell by typing the following commands:


From the shell, enter the following commands to connect to the administration database.

> use admin

Then run the following commands to create a new administrator user called administration and password. Replace strong_password_here with the password you want to use.

    user: "admin",
    pwd: "strong_password_here",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]

You should see a successful message that the admin user was created.

Successfully added user: {
	"user" : "admin",
	"roles" : [
			"role" : "root",
			"db" : "admin"

Please exit and continue below to enable MongoDB login authentication.

Now, to log in with the administrator account, run the following commands.

mongo -u admin -p --authenticationDatabase admin

You should do that!


In this tutorial we have seen how to install and configure MongoDB on Ubuntu Linux. If you find any errors above or have something to add, use the comment form below.

Write A Comment