How to install MongoDB on Ubuntu 20.04 or 18.04

MongoDB is an open source document-oriented database system based on NoSQL technology. It is compatible with the development of modern web applications.

This incredible database includes features like flexibility, expressive query languages, secondary indexes and more. In addition, it offers great scalability and performance to build modern applications with powerful databases.

Instead of saving data in tables, as in relational databases, MondoDB saves it in BSON data structures (a specification of the JSON type) with a dynamic schema.This makes integrating data in certain applications easier and faster.

MongoDB is widely used in the industry since it is suitable for production use thanks to its multiple functionalities. In addition, its source code is available for operating systems like; Linux, Windows, OSX and Solaris.

Read: How to create a user in MongoDB

MongoDB installation

MongoDB is available on Ubuntu official repositories. In order to install it, you would first have to update the packages list as follows: :

sudo apt update

Next run the command :

sudo apt install -y mongodb                                               [install mongodb linux command]

Several packages which contain the most recent MongoDB stable version will be installed.

Read: How to use the APT command on Ubuntu/Debian Linux systems

After the installation is completed, the DB server will start automatically as verified below :

sudo systemctl status mongodb

Which will check the status of the service. In the output, you should see an active status being returned which means that the server is up and running successfully.

You should be able to connect to the database server and retrieve the connection status as follows:

mongo –eval ‘db.runCommand({ connectionStatus: 1 })’

This will display connection status, the database version as well as the server address along with other details.

Here is an example of such information :

MongoDB server version: 4.4.10

“authInfo” : {
“authenticatedUsers” : [ ],
“authenticatedUserRoles” : [ ]
“ok” : 1

Notice the OK field. A value of 1 shows that the server is active and running.

Read: How to install MySQL on Ubuntu 18.04

If the service is not active, you can start it manually using the command :

sudo systemctl start mongodb

To stop the service, invoke the command :

sudo systemctl stop mongodb

In case you would like to restart the service, issue the command :

sudo systemctl restart mongodb

Note however that by default, MongoDB is launched automatically as soon as the service is up and running. You can however override this using the command :

sudo systemctl disable mongodb

And to enable it, run :

sudo systemctl enable mongodb

Note that in order to configure MongoDB, you would need to edit the file /etc/mongod.conf.
You can also locate the MongoDB configuration file by executing this command:

sudo locate mongod.conf

It will return the absolute path of the configuration file. Head over to the official documentation website for more information.


If you like the content, we would appreciate your support by buying us a coffee. Thank you so much for your visit and support.


Marianne elanotta

As a graduate in communication technologies, Marianne likes to share the latest technological advances in various fields. She likes to program in Java OO and Javascript and prefers to work on Open source operating systems. She likes to play chess and computer games in her spare time along with her 2 kids.

Leave a Reply