How to install MongoDB on Ubuntu on your VPS
Rackone systems team · 2 min read · Updated on
Install MongoDB on Ubuntu from the official repository: import the key, add the source, install mongodb-org, start mongod and secure it on your VPS.
NoSQL databases have become a standard part of web applications, and MongoDB is one of the most widely used. This guide shows how to install MongoDB on Ubuntu from the official repository, following MongoDB's own documentation.
What is MongoDB?
MongoDB is a document database: it stores data as JSON-like documents instead of table rows, with drivers for PHP, Python, Node.js and most other languages. It is simple to install and useful even on a single node, such as a Linux VPS.
Install MongoDB on Ubuntu in three steps
The commands install MongoDB Community Edition 9.0 on the Ubuntu LTS releases that MongoDB supports, on 64-bit systems. If Ubuntu's own mongodb package is installed, remove it first: MongoDB does not maintain it, and it conflicts with the official one.
Step 1: add the MongoDB repository
sudo apt install gnupg curl
curl -fsSL https://pgp.mongodb.com/server-9.asc | \
sudo gpg -o /usr/share/keyrings/mongodb-server-9.gpg --dearmor
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-9.gpg ] https://repo.mongodb.org/apt/ubuntu $(. /etc/os-release && echo $VERSION_CODENAME)/mongodb-org/9.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-9.0.listThe second command imports the MongoDB 9 signing key, and signed-by tells apt to accept packages from this repository only if that key signed them. The $(…) part writes your Ubuntu release name into the source, as the documentation does by hand.
Step 2: install MongoDB
sudo apt update
sudo apt install -y mongodb-orgStep 3: start MongoDB
sudo systemctl start mongod
sudo systemctl enable mongod
sudo systemctl status mongod
mongoshenable starts MongoDB at every boot, and mongosh opens the MongoDB shell on the default port, 27017.
Before you open it to the network
MongoDB listens only on 127.0.0.1 by default. Keep it that way until you have enabled authentication and allowed only your application servers through the firewall, as MongoDB's documentation asks before any exposure. The same basics for the server are in how to secure a VPS in 5 steps.
MongoDB shows its full strength on several nodes, with replica sets and sharding: for that, dedicated servers are the right base. To start, a Linux VPS is enough.