MongoDB is an open-source general-purpose, document-based NoSQL database server which uses JSON documents. It is created keeping the current requirements of the cloud era & application developers in mind. It boasts many features, some of which are,

  • Schemaless
  • Provides replication
  • Easy to scale-out
  • index on any attribute
  • auto-sharding
  • Deep/rich query abilities, etc

There are two versions of MongoDB available,

  • Community Version, which is free
  • Enterprise version, which is paid version

For this tutorial, we are focusing on the Community version only & will learn to install MongoDB on Ubuntu using any of the two methods mentioned below

1- Install MongoDB on Ubuntu using apt

The first step would be to install ‘GnuPG’ on your Ubuntu systems in order to install MongoDB on Ubuntu. Normally it’s installed but you can run the following command anyway,

$ sudo apt-get install gnupg

We will now import the public keys for MongoDB repositories using the following command,

$ wget -qO - | sudo apt-key add -

Now we need to create the source list file for MongoDB. You need to run the following command based on the Ubuntu version you are using,

Ubuntu 20.04

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

Ubuntu 18.04

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

Ubuntu 16.04

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

Now update the system repositories list with update command,

$ sudo apt-get update

We can now install the latest stable MongoDB version using the following command,

$ sudo apt-get install -y mongodb-org

You can also install a particular version by mentioning the version number, like,

$ sudo apt-get install -y mongodb-org=4.4.2 mongodb-org-server=4.4.2 mongodb-org-shell=4.4.2 mongodb-org-mongos=4.4.2 mongodb-org-tools=4.4.2

So this was a method to install MongoDB on Ubuntu using the APT package manager.  Now let’s discuss how we can install it using the tar or source packages.

2- Install MongoDB on Ubuntu using tar packages

Before we can download & install MongoDB using tar packages. We must have some packages installed on our servers,

$ sudo apt-get install libcurl4 openssl

Note:- This command is for Ubuntu 20.04 & 18.04 only, if installing MongoDB on Ubuntu 16.04, you need to run the following command,

$ sudo apt-get install libcurl3 openssl liblzma5

Next. we will create a directory for keeping the MongoDB packages,

$ mkdir -p /data/mongodb

Now download the latest tar file for MongoDB,

$ wget

Next, we need to extract the file to the created directory,

$ tar -xvzf mongodb-src-r4.4.2.tar.gz -C /data/mongodb

Next, we will create symbolic links to the binaries directory,

$ sudo ln -s /data/mongodb/bin/* /usr/local/bin/

Now, we need to create a data directory & log directory as well,

$ sudo mkdir -p /var/lib/mongo

$ sudo mkdir -p /var/log/mongo

Next, we have change ownership of these folders to mongod,

$ sudo chown -R mongod:mongod /var/lib/mongo

$ sudo chown -R mongod:mongod /var/log/mongo

Configuring MongoDB

After the installation is complete, we can start the MongoDB with the following command,

$ sudo systemctl start mongod

Note:- You can also start MongoDB with the following command if you have installed it using the tar package,

$ mongod --dbpath /var/lib/mongo --logpath /var/log/mongodb/mongod.log --fork

Once the DB has started, we can connect to it on the local system by running the following command from the terminal,

$ mongo

Now, that is the way to connect MongoDB from the local system only but we can also allow connections from remote systems. For that, we need to make some changes to the config file.

Open the MongoDB config file,

$ sudo vi /etc/mongod.conf

& look for the line with “net.bindIp” & then change it to following,


Or you can also mention a single IP address like,


You can also mention multiple IPs or a network subnet. Also if you have started your DB with the following command,

$ mongod --dbpath /var/lib/mongo --logpath /var/log/mongodb/mongod.log --fork

Then you need to also add ‘--bind_ip’ parameter followed by the remote IP address, like,

$ mongod --dbpath /var/lib/mongo --logpath /var/log/mongodb/mongod.log --bind_ip --fork 

Once you have made the required changes, we can then apply the changes by restarting the services,

$ sudo systemctl stop mongod

$ sudo systemctl start mongod

Or if you used the other method to start the DB, then kill the MongoDB service & start it again. So that’s it, this completes our tutorial on how to install MongoDB on Ubuntu. Do send in any questions or queries you might have using the comment box below.

If you think we have helped you or just want to support us, please consider these:-

Connect to us: Facebook | Twitter

Linux TechLab is thankful for your continued support.