How to install MongoDB on Ubuntu Server 18.04

mongoDB on ubuntu server

If your company is in the business of using, handling or depending on data, chances are you’re in need of a document-oriented, NoSQL database. If you’re unfamiliar with the term, a NoSQL database is a non-relational database that doesn’t use tables filled with rows and columns. Instead, they make use of a storage model that is optimized specifically for the data.

These types of databases offer scalability, flexibility, data distribution, and speed of processing that relational databases can’t match.

One NoSQL database is MongoDB. This database has been adopted by big data and enterprise companies including Adobe, Craigslist, eBay, FIFA, Foursquare, and LinkedIn. MongoDB comes in both an enterprise and community edition. I’ll be demonstrating with the open-source community edition, installing it on Ubuntu Server 18.04.

This edition can be installed from the standard repositories, however, that will likely install an outdated version. Because of that, I’ll show how to deploy a version from the official MongoDB repository. This will install:

  • mongodb-org (this is the meta-package that will install everything below)
  • mongodb-org-server (the mongod daemon)
  • mongodb-org-mongos (the mongos daemon)
  • mongodb-org-shell (the mongo shell)
  • mongodb-org-tools (the MongoDB tools package which includes import, dump, export, files, performance, restore, and stats tools)

Do note that this package only supports 64-bit architecture and LTS (Long Term Support) versions of Ubuntu (so 14.04, 16.04, and 18.04).

Once installed, your Java development company (or whatever sector your business serves) can begin developing for big data.


When installing a major application/service, it’s always best to first run an update/upgrade on the server. Not only will this ensure you have the most recent software, but it’ll also apply any security patches. Do note, however, that should the kernel be updated in this process, you will need to reboot the machine before the updates take effect.

To update and upgrade Ubuntu, log into the server, and issue the following two commands:

sudo apt-get update
sudo apt-get upgrade -y

Once up the update and upgrade completes, reboot your server (if required). You are now ready to install MongoDB, and you won’t even need to bring in your Java developers to take care of this task.

Adding the Repository

The first thing to be done is the addition of the necessary MongoDB repository. To do this, log into your Ubuntu server. From the command line, add the required MongoDB key with the command:

wget -qO - | sudo apt-key add -

If you see an error regarding the wget command, install that tool with:

sudo apt-get install wget

Once you’ve added the key, create a new apt source list file with the command:

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


Now it’s time to install MongoDB. Update apt with the command:

sudo apt-get update

Once apt is updated, install MongoDB with the command:

sudo apt-get install mongodb-org -y

Starting and Enabling the Community Edition

With the database installed, you’ll want to start and enable it to run upon server reboot. Otherwise, you’ll have to manually start it every time the server is restarted.

To start the MongoDB database engine, issue the command:

sudo systemctl start mongod

You’ll then want to enable MongoDB with the command:

sudo systemctl enable mongod

Using MongoDB

In order to start working with Mongo, issue the command:


If you get an error status 62, it means that the version of MongoDB is too new for your server. If that’s the case, you’ll need to uninstall the latest version and install the version from the official Ubuntu repositories. Here are the steps for that process:

  1. Remove the latest version with the command sudo apt-get purge mongodb-org.
  2. Remove any extra dependencies with the command sudo apt-get autoremove.
  3. Install the older version of MongoDB with the command sudo apt-get install mongodb -y.

At this point, you should then have access to the MongoDB command prompt (Figure 1) by issuing the mongo command.

Figure 1

The MongoDB command prompt.

Let’s say you want to create a new database. Unlike relational databases, you don’t use the CREATE command. Instead, you simply issue the use command like so:


Where DATABASE is the name of the database to be created.

This doesn’t actually create the database. In order to finalize that, you must insert data into the new database. Say you create a database named albums. You can then insert data into that database with the command:

db.artists.insert({artistname: "Devin Townsend" })

The above command would insert the string “Devin Townsend” associated with artistname in the database albums. You should see WriteResult({ “nInserted” : 1 }) as a result (Figure 2).

Figure 2

Successfully insertion of data into the new db.

And that’s all there is to installing MongoDB on Ubuntu 18.04 and creating your first database. For more information on using MongoDB, make sure to read the official documentation for the release you’ve installed.

Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like