# How to install MongoDB on Ubuntu 20.04

[MongoDB](https://www.ionos.co.uk/digitalguide/websites/web-development/mongodb-an-introduction-and-comparison-to-mysql/ "MongoDB: an introduction and comparison to MySQL") is one of the most popular document-based database systems and can be installed under the LTS-[Ubuntu](https://www.ionos.co.uk/digitalguide/server/know-how/ubuntu-the-linux-system-for-everyone/ "Ubuntu: The Linux system for everyone") version 20.04 in just a few steps. You perform the entire installation 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](https://www.ionos.co.uk/digitalguide/server/configuration/linux-commands-an-overview-of-terminal-commands/ "Linux commands: An overview of 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 Autumn 2022). Therefore, you should use **Ubuntu 20.04** to install the NoSQL database management system. This Ubuntu version also offers you long-term support. In addition, the installation of MongoDB **only works on 64-bit architectures**, so make sure to install an appropriate operating system version in advance.

---

### Note

If you like other [Linux distributions](https://www.ionos.co.uk/digitalguide/server/configuration/linux-distributions/ "Linux distributions") better, that’s no problem. You can [install MongoDB on Debian](https://www.ionos.co.uk/digitalguide/websites/web-development/install-mongodb-debian/ "Install MongoDB Debian") or any other Linux distribution you like. In that case, however, the installation 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:

```none
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:

```none
sudo apt-get install gnupg
```

Then run the import command again. It should be successful.

### Step 2: Create a List-File

In the next step, **create the list file** appropriate for your version of Ubuntu. You can also use the terminal for this:

```none
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
```

Afterwards, you should have your system reloaded again so that the changes take effect, and **the MongoDB repository** is added to your system. This process may take a little time.

```none
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 sufficient for the installation:

```none
sudo apt-get install -y mongodb-org
```

---

### Note

Make sure to select the correct package called ‘mongodb-org’ when installing MongoDB. The unofficial version ‘mongodb’ provided by Ubuntu should not be used if you follow our step-by-step guide. If you have previously 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:

```none
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 installation process may take a few minutes. This is perfectly normal. Once the installation process is complete, you have installed the latest version of MongoDB on Ubuntu.

---

### Tip: Managed MongoDB from IONOS

**Managed** [**MongoDB from IONOS**](https://cloud.ionos.co.uk/managed/dbaas/managed-mongodb?itc=2E5H5WQ0-HUKYUQ-LTP4SQO "Managed MongoDB from IONOS") enables you to concentrate on the essentials. From installation to operation and maintenance work, IONOS makes sure you always get the best performance from your data banks.

---

## Start MongoDB

After you have successfully installed the [NoSQL](https://www.ionos.co.uk/digitalguide/hosting/technical-matters/nosql/ "NoSQL") database, you can **start it** with a very simple command:

```none
sudo systemctl start mongod
```

Under certain circumstances, an error may occur during the initial start-up. In this case, first **run the following command to reload all configuration files** and restart all units of your system:

```none
sudo systemctl daemon-reload
```

Now you should be able to start MongoDB easily. For example, to find out if your database start-up was successful, you can always check the status with the following terminal command:

```none
sudo systemctl status mongod
```

## Stop or restart MongoDB

Quitting MongoDB is also done with just one terminal command:

```none
sudo systemctl stop mongod
```

Restarting the database works similarly:

```none
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 designated by default. You can display open ports with the following terminal command:

```none
netstat -plntu
```

### Start-up Shell

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

```none
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](https://www.ionos.co.uk/digitalguide/websites/web-development/mongodb-tutorial-first-steps/ "MongoDB tutorial: first steps").

## Uninstall MongoDB on Ubuntu

You may find when [comparing open-source databases](https://www.ionos.co.uk/digitalguide/hosting/technical-matters/open-source-databases-comparison/ "Open source databases comparison") that you prefer another [DBMS](https://www.ionos.co.uk/digitalguide/hosting/technical-matters/database-management-systems-dbms/ "Database Management Systems (DBMS)") and want to uninstall MongoDB on Ubuntu again. For whatever reason you decide to uninstall, this is done just as quickly as the installation. 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:

```none
sudo service mongod stop
```

### Step 2: Uninstall packages

Uninstall all previously installed packages by running the following terminal command:

```none
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:

```none
sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongodb
```


This is a markdown version of: [https://www.ionos.co.uk/digitalguide/websites/web-development/install-mongodb-ubuntu/](https://www.ionos.co.uk/digitalguide/websites/web-development/install-mongodb-ubuntu/) for AI/LLM consumption.