MongoDB is one of the most popular document-based database systems and can be installed under the LTS-Ubuntu version 20.04 in just a few steps. You perform the entire in­stal­la­tion via the terminal.

What you’ll need to install MongoDB on Ubuntu

To install MongoDB on your Ubuntu system, you just need a basic knowledge of the most important Linux terminal commands and, of course, Ubuntu as an operating system. Note that the current version of Ubuntu, i.e. version 22.04, does not yet have official MongoDB support (as of Fall 2022). Therefore, you should use Ubuntu 20.04 to install the NoSQL database man­age­ment system. This Ubuntu version also offers you long-term support. In addition, the in­stal­la­tion of MongoDB only works on 64-bit ar­chi­tec­tures, so make sure to install an ap­pro­pri­ate operating system version in advance.

Note

If you like other Linux dis­tri­b­u­tions better, that’s no problem. You can install MongoDB on Debian or any other Linux dis­tri­b­u­tion you like. In that case, however, the in­stal­la­tion process will be slightly different.

Install MongoDB on Ubuntu 20.04

Step 1: Importing the MongoDB key

First, you need to import the MongoDB GPG open key. To do this, first open the terminal. Then type the following command to import the key of the current MongoDB version 6.0:

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

You will be prompted to enter your password. After you have done so, the import process should normally work without fail. However, it is possible that the GNU Privacy Guard, gnupg for short, hasn’t been installed on your system. In this case you will get an error message. To fix this, install the program with the following terminal command:

sudo apt-get install gnupg

Then run the import command again. It should be suc­cess­ful.

Step 2: Create a List-File

In the next step, create the list file ap­pro­pri­ate for your version of Ubuntu. You can also use the terminal for this:

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

Af­ter­wards, you should have your system reloaded again so that the changes take effect, and the MongoDB repos­i­to­ry is added to your system. This process may take a little time.

sudo apt-get update

Step 3: Install MongoDB packages

Next, install the packages needed for the MongoDB version you want to run. In most cases, it is a good idea to select the current version of MongoDB. The following command is then suf­fi­cient for the in­stal­la­tion:

sudo apt-get install -y mongodb-org
Note

Make sure to select the correct package called “mongodb-org” when in­stalling MongoDB. The un­of­fi­cial version “mongodb” provided by Ubuntu should not be used if you follow our step-by-step guide. If you have pre­vi­ous­ly installed the package provided by Ubuntu, uninstall it so that the steps presented here work.

Want to install a specific version of MongoDB? To do this, you need to manually specify the version number of your choice for each package. For example, if you want to install MongoDB version 6.0.1, the command required for this looks like this:

sudo apt-get install -y mongodb-org=6.0.1 mongodb-org-database=6.0.1 mongodb-org-server=6.0.1 mongodb-mongosh=6.0.1 mongodb-org-mongos=6.0.1 mongodb-org-tools=6.0.1

The in­stal­la­tion process may take a few minutes. This is perfectly normal. Once the in­stal­la­tion process is complete, you have installed the latest version of MongoDB on Ubuntu.

Start MongoDB

After you have suc­cess­ful­ly installed the NoSQL database, you can start it with a very simple command:

sudo systemctl start mongod

Under certain cir­cum­stances, an error may occur during the initial startup. In this case, first run the following command to reload all con­fig­u­ra­tion files and restart all units of your system:

sudo systemctl daemon-reload

Now you should be able to start MongoDB easily. For example, to find out if your database startup was suc­cess­ful, you can always check the status with the following terminal command:

sudo systemctl status mongod

Stop or restart MongoDB

Quitting MongoDB is also done with just one terminal command:

sudo systemctl stop mongod

Restart­ing the database works similarly:

sudo systemctl restart mongod

In both cases, you can use the status command shown earlier to check whether MongoDB is in the state you want after executing a command.

Start a Mongosh session

Check the port

Before starting a Mongosh session, verify that MongoDB is running on the correct port. Port 27017 is des­ig­nat­ed by default. You can display open ports with the following terminal command:

netstat -plntu

Startup Shell

To open the shell of MongoDB, the following code line is will work:

mongosh

In the MongoDB shell, you can, for example, add new users to your database or add new roles to them. You can also interact with the database. You can find useful tips on this in our MongoDB Tutorial.

Web Hosting
Hosting that scales with your ambitions
  • Stay online with 99.99% uptime and robust security
  • Add per­for­mance with a click as traffic grows
  • Includes free domain, SSL, email, and 24/7 support

Uninstall MongoDB on Ubuntu

You may find when comparing open-source databases that you prefer another DBMS and want to uninstall MongoDB on Ubuntu again. For whatever reason you decide to uninstall, this is done just as quickly as the in­stal­la­tion. Note that when you uninstall MongoDB, all databases and all stored data disappear as well.

Step 1: Stop MongoDB

Stop MongoDB with the following command:

sudo service mongod stop

Step 2: Uninstall packages

Uninstall all pre­vi­ous­ly installed packages by running the following terminal command:

sudo apt-get purge mongodb-org*

Step 3: Delete databases and log files

In a final step, you need to remove all the databases and all the log files. This is also possible in the terminal:

sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongodb
Go to Main Menu