How To Install MongoDB on Ubuntu 16.04 (Xenial)

Select distribution:
Traducciones al Español

In this MongoDB tutorial, we explain how to install the database on Ubuntu 16.04, and then provide a short guide on some basic features and functions.

Install MongoDB on Ubuntu 16.04

MongoDB is a database engine that provides access to non-relational, document-oriented databases. It is part of the growing NoSQL movement, along with databases like Redis and Cassandra (although there are vast differences among the many non-relational databases).

MongoDB seeks to provide an alternative to traditional relational database management systems (RDBMS). In addition to its schema-free design and scalable architecture, MongoDB provides a JSON output and specialized, language-specific bindings that make it particularly attractive for use in custom application development and rapid prototyping. MongoDB has been used in a number of large scale production deployments and is currently one of the most popular database engines across all systems.

Since MongoDB can require a significant amount of RAM, we recommend using a High Memory Linode with this guide.

Before You Begin



  • Update your system:

      sudo apt-get update && sudo apt-get upgrade

Add the MongoDB Repository

The mongodb-server package from the Ubuntu repository includes version 2.6. However, this version reached end of life in October 2016, so it should not be used in production environments. The most current version available is 4.0 and, as of this writing, the default Ubuntu repositories do not contain an updated package.

Because the Ubuntu repositories don’t contain a current version, we’ll need to use the MongoDB repository.

  1. Import the MongoDB public GPG key for package signing:

  2. Add the MongoDB repository to your sources.list.d directory:

  3. Update your repositories. This allows apt to read from the newly added MongoDB repo:

    sudo apt-get update

Install MongoDB

Now that the MongoDB repository has been added, we’re ready to install the latest stable version of MongoDB:

sudo apt-get install mongodb-org

This command installs mongodb-org, a meta-package that includes the following:

  • mongodb-org-server - The standard MongoDB daemon, and relevant init scripts and configurations
  • mongodb-org-mongos - The MongoDB Shard daemon
  • mongodb-org-shell - The MongoDB shell, used to interact with MongoDB via the command line
  • mongodb-org-tools - Contains a few basic tools to restore, import, and export data, as well as a variety of other functions.

These packages provide a good base that will serve most use cases, and we recommend installing them all. However, if you want a more minimal installation, you can selectively install packages from the above list rather than using the mongodb-org metapackage.

For more information on the installation process and options, refer to the official MongoDB installation tutorial.

Configure MongoDB

The configuration file for MongoDB is located at /etc/mongod.conf, and is written in YAML format. Most of the settings are well commented within the file. We’ve outlined the default options below:

  • dbPath indicates where the database files will be stored (/var/lib/mongodb by default)
  • systemLog specifies the various logging options, explained below:
    • destination tells MongoDB whether to store the log output as a file or syslog
    • logAppend specifies whether to append new entries to the end of an existing log when the daemon restarts (as opposed to creating a backup and starting a new log upon restarting)
    • path tells the daemon where to send its logging information (/var/log/mongodb/mongod.log by default)
  • net specifies the various network options, explained below:
    • port is the port on which the MongoDB daemon will run
    • bindIP specifies the IP addresses MongoDB to which binds, so it can listen for connections from other applications

These are only a few basic configuration options that are set by default.

We strongly recommend uncommenting the security section and adding the following:

File: /etc/mongod.conf
  authorization: enabled

The authorization option enables role-based access control for your databases. If no value is specified, any user will have the ability to modify any database. We’ll explain how to create database users and set their permissions later in this guide.

For more information on how to customize these and other values in your configuration file, refer to the official MongoDB configuration tutorial.

After making changes to the MongoDB configuration file, restart the service as shown in the following section.

Start and Stop MongoDB

To start, restart, or stop the MongoDB service, issue the appropriate command from the following:

sudo systemctl start mongod
sudo systemctl restart mongod
sudo systemctl stop mongod

You can also enable MongoDB to start on boot:

sudo systemctl enable mongod

Create Database Users

If you enabled role-based access control in the Configure MongoDB section, create a user administrator with credentials for use on the database:

  1. Open the mongo shell:

  2. By default, MongoDB connects to a database called test. Before adding any users, create a database to store user data for authentication:

    use admin
  3. Use the following command to create an administrative user with the ability to create other users on any database. For better security, change the values mongo-admin and password:


    Keep these credentials in a safe place for future reference. The output will display all the information written to the database except the password:

    Successfully added user: {
        "user" : "mongo-admin",
        "roles" : [
                    "role" : "userAdminAnyDatabase",
                    "db" : "admin"
  4. Exit the mongo shell:

  5. Test your connection to MongoDB with the credentials created in Step 3, using the admin database for authentication:

    mongo -u mongo-admin -p --authenticationDatabase admin



  6. As the mongo-admin user, create a new database to store regular user data for authentication. The following example calls this database user-data:

    use user-data

    Create a new, non-administrative user to enter test data. Change both example-user and password to something relevant and secure:


    To create additional users, repeat Steps 6 and 7 as the administrative user, creating new usernames, passwords and roles by substituting the appropriate values.

  8. Exit the mongo shell:



Manage Data and Collections


  1. Open the MongoDB shell using the example-user we created above:

    mongo -u example-user -p --authenticationDatabase user-data
  2. Create a new database. This example calls it exampleDB:

    use exampleDB



  3. Create a new collection called exampleCollection:

    db.createCollection("exampleCollection", {capped: false})





  5. Insert the data into exampleCollection, using the insert method:


    The output for each of these operations will show the number of objects successfully written to the current working database:

    WriteResult({ "nInserted" : 1 })
  6. Confirm that the exampleCollection collection was properly created:

    show collections

    The output will list all collections containing data within the current working database:



    The output will resemble the following:





    Running the command above returns a list of documents containing the {"name" : "John Doe"} object:


Additional MongoDB Functionality

As noted above, MongoDB has an available collection of language-specific drivers that can be used to interact with your databases from within non-JavaScript applications. One advantage these drivers provide is the ability to allow applications written in different languages to use the same database without the strict need for an object data mapper (ODM). If you do want to use an object data mapper, however, many well-supported ODMs are available.

The mongodb-org-tools package we installed also includes several other tools such as mongodump and mongorestore for creating and restoring backups and snapshots, as well as mongoimport and mongoexport for importing and exporting content from extended JSON, or supported CSV or TSV files.

To view the available options or how to use a particular method, append .help() to the end of your commands. For example, to see a list of options for the find method in Step 6 of Manage Data and Collections:


More Information


This page was originally published on


Your Feedback Is Important


Join the conversation.