How to install MongoDB on Ubuntu 20.04
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 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.
If you like other Linux distributions better, that’s no problem. You can install MongoDB on 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:
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 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:
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.
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:
sudo apt-get install -y mongodb-org
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:
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.
Start MongoDB
After you have successfully installed the NoSQL database, you can start it with a very simple command:
sudo systemctl start mongod
Under certain circumstances, an error may occur during the initial startup. In this case, first run the following command to reload all configuration 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 successful, 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
Restarting 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 designated 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.
- 99.9% uptime
- PHP 8.3 with JIT compiler
- SSL, DDoS protection, and backups
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 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:
sudo service mongod stop
Step 2: Uninstall packages
Uninstall all previously 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