# Install MongoDB Community Edition on Ubuntu

# Install MongoDB Community Edition

Follow these steps to install MongoDB Community Edition using the apt package manager.

# 1.Import the public key used by the package management system.

From a terminal, issue the following command to import the MongoDB public GPG Key from https://www.mongodb.org/static/pgp/server-4.2.asc:

wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc | sudo apt-key add -

The operation should respond with an OK.

However, if you receive an error indicating that gnupg is not installed, you can:

  1. Install gnupg and its required libraries using the following command:
sudo apt-get install gnupg
  1. Once installed, retry importing the key:
wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc | sudo apt-key add -

# 2.Create a list file for MongoDB.

Create the list file /etc/apt/sources.list.d/mongodb-org-4.2.list for your version of Ubuntu.

echo 'deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/4.2 multiverse' | sudo tee /etc/apt/sources.list.d/mongodb-org-4.2.list

# 3.Reload local package database.

Issue the following command to reload the local package database:

sudo apt-get update

# 4.Install the MongoDB packages.

You can install either the latest stable version of MongoDB or a specific version of MongoDB.

  • Install the latest version of MongoDB.
  • Install a specific release of MongoDB.
sudo apt-get install -y mongodb-org=4.2.7 \
mongodb-org-server=4.2.7 mongodb-org-shell=4.2.7 \
mongodb-org-mongos=4.2.7 mongodb-org-tools=4.2.7

To install the latest stable version, issue the following

sudo apt-get install -y mongodb-org

Optional. Although you can specify any available version of MongoDB, apt-get will upgrade the packages when a newer version becomes available. To prevent unintended upgrades, you can pin the package at the currently installed version:

echo 'mongodb-org hold' | sudo dpkg --set-selections
echo 'mongodb-org-server hold' | sudo dpkg --set-selections
echo 'mongodb-org-shell hold' | sudo dpkg --set-selections
echo 'mongodb-org-mongos hold' | sudo dpkg --set-selections
echo 'mongodb-org-tools hold' | sudo dpkg --set-selections

For help with troubleshooting errors encountered while installing MongoDB on Ubuntu, see our troubleshooting (opens new window) guide.

# Run MongoDB Community Edition

  • ulimit Considerations

    Most Unix-like operating systems limit the system resources that a session may use. These limits may negatively impact MongoDB operation. See UNIX ulimit Settings (opens new window) for more information.

  • Directories

    If you installed via the package manager, the data directory /var/lib/mongodb and the log directory /var/log/mongodb are created during the installation.By default, MongoDB runs using the mongodb user account. If you change the user that runs the MongoDB process, you must also modify the permission to the data and log directories to give this user access to these directories.

  • Configuration File

    The official MongoDB package includes a configuration file (opens new window) (/etc/mongod.conf). These settings (such as the data directory and log directory specifications) take effect upon startup. That is, if you change the configuration file while the MongoDB instance is running, you must restart the instance for the changes to take effect.

# Procedure

Follow these steps to run MongoDB Community Edition on your system. These instructions assume that you are using the official mongodb-org package – not the unofficial mongodb package provided by Ubuntu – and are using the default settings.

Init System

To run and manage your mongod (opens new window) process, you will be using your operating system’s built-in init system (opens new window). Recent versions of Linux tend to use systemd (which uses the systemctl command), while older versions of Linux tend to use System V init (which uses the service command).

If you are unsure which init system your platform uses, run the following command:

ps --no-headers -o comm 1

Then select the appropriate tab below based on the result:

  • systemd

# 1. Start MongoDB.

You can start the mongod (opens new window) process by issuing the following command:

sudo systemctl start mongod

If you receive an error similar to the following when starting mongod (opens new window):

Failed to start mongod.service: Unit mongod.service not found.

Run the following command first:

sudo systemctl daemon-reload

Then run the start command above again.

# 2. Verify that MongoDB has started successfully.

sudo systemctl status mongod

You can optionally ensure that MongoDB will start following a system reboot by issuing the following command:

sudo systemctl enable mongod

# 3. Stop MongoDB.

As needed, you can stop the mongod (opens new window) process by issuing the following command:

sudo systemctl stop mongod

# 4. Restart MongoDB.

You can restart the mongod (opens new window) process by issuing the following command:

sudo systemctl restart mongod

You can follow the state of the process for errors or important messages by watching the output in the /var/log/mongodb/mongod.log file.

# 5. Begin using MongoDB.

Start a mongo (opens new window) shell on the same host machine as the mongod (opens new window). You can run the mongo (opens new window) shell without any command-line options to connect to a mongod (opens new window) that is running on your localhost with default port 27017:

mongo

For more information on connecting using the mongo (opens new window) shell, such as to connect to a mongod (opens new window) instance running on a different host and/or port, see The mongo Shell (opens new window).

To help you start using MongoDB, MongoDB provides Getting Started Guides (opens new window) in various driver editions. See Getting Started (opens new window) for the available editions.

# Uninstall MongoDB Community Edition

To completely remove MongoDB from a system, you must remove the MongoDB applications themselves, the configuration files, and any directories containing data and logs. The following section guides you through the necessary steps.

WARNING

This process will completely remove MongoDB, its configuration, and all databases. This process is not reversible, so ensure that all of your configuration and data is backed up before proceeding.

# 1. Stop MongoDB.

Stop the mongod (opens new window) process by issuing the following command:

sudo service mongod stop

# 2. Remove Packages.

Remove any MongoDB packages that you had previously installed.

sudo apt-get purge mongodb-org*

# 3. Remove Data Directories.

Remove MongoDB databases and log files.

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

# Additional Information

# Localhost Binding by Default

By default, MongoDB launches with bindIp (opens new window) set to 127.0.0.1, which binds to the localhost network interface. This means that the mongod can only accept connections from clients that are running on the same machine. Remote clients will not be able to connect to the mongod, and the mongod will not be able to initialize a replica set (opens new window) unless this value is set to a valid network interface.

This value can be configured either:

WARNING

Before binding to a non-localhost (e.g. publicly accessible) IP address, ensure you have secured your cluster from unauthorized access. For a complete list of security recommendations, see Security Checklist (opens new window). At minimum, consider enabling authentication (opens new window) and hardening network infrastructure (opens new window).

For more information on configuring bindIp (opens new window), see IP Binding (opens new window).

# MongoDB Community Edition Packages

MongoDB Community Edition is available from its own dedicated repository, and contains the following officially-supported packages:

Package Name Description
mongodb-org A metapackage that automatically installs the component packages listed below.
mongodb-org-server Contains the mongod (opens new window) daemon, associated init script, and a configuration file (opens new window) (/etc/mongod.conf). You can use the initialization script to start mongod (opens new window) with the configuration file. For details, see Run MongoDB Community Edition (opens new window).
mongodb-org-mongos Contains the mongos (opens new window) daemon.
mongodb-org-shell Contains the mongo (opens new window) shell.
mongodb-org-tools Contains the following MongoDB tools:mongodump (opens new window)mongorestore (opens new window)bsondump (opens new window)mongoimport (opens new window)mongoexport (opens new window)mongostat (opens new window)mongotop (opens new window)mongofiles (opens new window)install_compass (opens new window) script