How to Install MongoDB Community Edition on Linux

Install MongoDB Community Edition on Linux

MongoDB provides packages for common Linux systems for the best installation experience. The recommended approach to run MongoDB is using these packages. The following guidelines will walk you through the installation of these systems:

Install MongoDB Community Edition on Red Hat or CentOS

Using the yum package manager, install MongoDB 5.0 Community Edition on Red Hat Enterprise Linux, CentOS Linux, or Oracle Linux.

MongoDB Version

MongoDB 5.0 Community Edition is installed in this tutorial. Use the version drop-down option in the upper-left corner of this page to select the documentation for a different version of MongoDB Community.

Using the yum package manager, follow these steps to install MongoDB Community Edition.

  • 1. Set up the system for package management (yum)

Create a /etc/yum.repos.d/mongodb-org-5.0.repo file so that you can install MongoDB directly using yum:

[mongodb-org-5.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/5.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-5.0.asc

The .rpm files are also available directly from the MongoDB repository. The downloads are categorised by Red Hat / CentOS version (for example, 7), MongoDB release version (for example, 5.0), and architecture (for example, x86 64).

  • 2. Install the MongoDB packages

Run the following command to install the most recent stable version of MongoDB:

sudo yum install -y mongodb-org

To install a specific version of MongoDB, provide each component package individually and include the version number to the package name, as shown below:

sudo yum install -y mongodb-org-5.0.5 mongodb-org-database-5.0.5 mongodb-org-server-5.0.5 mongodb-org-shell-5.0.5 mongodb-org-mongos-5.0.5 mongodb-org-tools-5.0.5

You can use any MongoDB version that is currently available. When a newer version of a package becomes available, yum updates the packages. Pin the package to avoid inadvertent upgrades. Add the following exclude directive to your /etc/yum.conf file to pin a package:

exclude=mongodb-org,mongodb-org-database,mongodb-org-server,mongodb-org-shell,mongodb-org-mongos,mongodb-org-tools

  • 3. Start MongoDB

You can start the mongod process by issuing the following command:

sudo systemctl start mongod

If you get an error that looks like this when starting mongod:

Mongod.service could not be started because the unit mongod.service was not found.

First, run the following command:

sudo systemctl daemon-reload

Then perform the above-mentioned start command once more.

  • 4. Check to see if MongoDB is up and running

You can use the following command to see if the mongod process has started successfully:

sudo systemctl status mongod

Optionally, you can verify that MongoDB starts after a system reboot by running the following command:

sudo systemctl enable mongod

Install MongoDB Community Edition on Ubuntu

To use the apt package manager to install MongoDB Community Edition, follow these steps.

  • 1. Import the package management system’s public key onto your computer

To import the MongoDB public GPG Key from https://www.mongodb.org/static/pgp/server-5.0.asc, run the following command from a terminal:

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

OK should be the response from the operation.

If you get an error saying gnupg isn’t installed, you can try the following:

Use the following command to install gnupg and its needed libraries:

sudo apt-get install gnupg

Once installed, retry importing the key:

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

  • 2. Create a MongoDB list file

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

The instructions below are for Ubuntu 20.04. (Focal).

Create the mongodb-org-5.0.list file in /etc/apt/sources.list.d/mongodb-org-5.0.list for Ubuntu 20.04 (Focal):

echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list
  • 3. Reload local package database

To reload the local package database, run the following command:

sudo apt-get update

  • 4. Install the MongoDB packages

You have the option of installing either the most recent stable version of MongoDB or a specific MongoDB version.

sudo apt-get install -y mongodb-org

  • 5. Start MongoDB

You can start the mongod process by issuing the following command:

sudo systemctl start mongod

If you get an error that looks like this when starting mongod:

Mongod.service could not be started because the unit mongod.service was not found.

First, run the following command:

sudo systemctl daemon-reload

Then perform the above-mentioned start command once more.

  • 6. Check to see if MongoDB is up and running

You can use the following command to see if the mongod process has started successfully:

sudo systemctl status mongod

Optionally, you can verify that MongoDB starts after a system reboot by running the following command:

sudo systemctl enable mongod

Previous Post
Next Post

Leave a Reply

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