Skip to content

MongoDB installation instructions

Install MongoDB Community Edition on Debian source

Use this tutorial to install MongoDB 8.0 Community Edition using the apt package manager. Follow these steps to install MongoDB Community Edition using the apt package manager.

Import the public key.

From a terminal, install gnupg and curl if they are not already available:

sudo apt-get install gnupg curl

To import the MongoDB public GPG key, run the following command:

curl -fsSL https://www.mongodb.org/static/pgp/server-8.0.asc | sudo gpg -o /usr/share/keyrings/mongodb-server-8.0.gpg --dearmor
Create the list file.

Create the list file for Debian 12 (Bookworm):

echo "deb [ signed-by=/usr/share/keyrings/mongodb-server-8.0.gpg ] http://repo.mongodb.org/apt/debian bookworm/mongodb-org/8.0 main" | sudo tee /etc/apt/sources.list.d/mongodb-org-8.0.list
Reload the package database.

Issue the following command to reload the local package database:

sudo apt-get update
Install MongoDB Community Server.

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

sudo apt-get install -y mongodb-org

Run MongoDB Community Edition

Start MongoDB.

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

sudo systemctl start mongod

If you receive an error similar to the following when starting mongod: 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.

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
Stop MongoDB.

As needed, you can stop the mongod process by issuing the following command:

sudo systemctl stop mongod
Restart MongoDB.

You can restart the mongod 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.

Begin using MongoDB.

Start a mongosh session on the same host machine as the mongod. You can run mongosh without any command-line options to connect to a mongod that is running on your localhost with default port 27017.

mongosh

For more information on connecting using mongosh, such as to connect to a mongod instance running on a different host and/or port, see the mongosh documentation.

Install MongoDB Community with Docker source

Pull the MongoDB Docker Image
docker pull mongo:latest
Run the Image as a Container
docker run --name mongodb -p 27017:27017 -d mongodb/mongodb-community-server:latest

The -p 27017:27017 in this command maps the container port to the host port. This allows you to connect to MongoDB with a localhost:27017 connection string.

Check that the Container is Running
docker container ls
Connect to the MongoDB Deployment with mongosh
mongosh --port 27017
Validate Your Deployment

To confirm your MongoDB instance is running, run the Hello command:

db.runCommand(
{
hello: 1
}
)

The result of this command returns a document describing your mongod deployment:

{
isWritablePrimary: true,
topologyVersion: {
processId: ObjectId("63c00e27195285e827d48908"),
counter: Long("0")
},
maxBsonObjectSize: 16777216,
maxMessageSizeBytes: 48000000,
maxWriteBatchSize: 100000,
localTime: ISODate("2023-01-12T16:51:10.132Z"),
logicalSessionTimeoutMinutes: 30,
connectionId: 18,
minWireVersion: 0,
maxWireVersion: 20,
readOnly: false,
ok: 1
}

For more options see: MongoDB Installation Tutorials