How to Install MongoDB on Ubuntu
Install MongoDB Community Edition on Ubuntu 24.04 from the official repo, enable authentication, and create your first database and user.

MongoDB is a document database that stores flexible JSON-like records — a natural fit for Node.js apps and anything with evolving schemas. This guide installs MongoDB Community Edition on Ubuntu 24.04 from MongoDB's official repository (the Ubuntu default package is outdated), then enables authentication and creates users.
Why the official repository?
Ubuntu's bundled MongoDB is old and unmaintained. MongoDB's own apt repository provides current, supported releases with security updates. It takes a few extra steps to add, and it is the only sensible way to install it.
Add the MongoDB repository
Import the signing key and add the source. MongoDB 7.0 supports Ubuntu 24.04:
sudo apt install -y gnupg curl
curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc | \
sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg --dearmor
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu noble/mongodb-org/7.0 multiverse" | \
sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
noble is Ubuntu 24.04's codename. Update and install:
sudo apt update
sudo apt install -y mongodb-org
Start and enable the service
sudo systemctl start mongod
sudo systemctl enable mongod
systemctl status mongod
Connect with the Mongo shell to confirm it responds:
mongosh
You should land at a test> prompt.

Create an admin user
Out of the box MongoDB has no authentication — anyone who can reach the port has full access. Fix this immediately. In mongosh, create an admin user:
use admin
db.createUser({
user: "admin",
pwd: "a-strong-admin-password",
roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
})
Exit with exit.
Enable authentication
Edit /etc/mongod.conf and add (or uncomment) the security section:
security:
authorization: enabled
Restart MongoDB to apply it:
sudo systemctl restart mongod
Now connections must authenticate:
mongosh -u admin -p --authenticationDatabase admin
Create an application user
With auth on, create a user scoped to just your app's database:
use appdb
db.createUser({
user: "appuser",
pwd: "a-strong-password",
roles: [ { role: "readWrite", db: "appdb" } ]
})
Your Node app then connects with a URI like mongodb://appuser:password@127.0.0.1:27017/appdb.
Keep it bound to localhost
By default MongoDB binds to 127.0.0.1 via bindIp in mongod.conf — leave it that way unless you genuinely need remote access. Verify:
sudo ss -ltnp | grep 27017
An exposed, unauthenticated MongoDB is one of the most-scanned targets on the internet, so if you ever open it, require auth, restrict the port with UFW, and use TLS. Our Linux VPS hardening checklist is worth applying to the whole box.
Backups
Back up a database with mongodump:
mongodump --uri="mongodb://appuser:password@127.0.0.1:27017/appdb" --out ~/backup-$(date +%F)
Automate it with a cron job.
Documents, collections, and a quick tour
MongoDB stores documents (JSON-like records) grouped into collections (the rough equivalent of tables). There is no fixed schema, so documents in a collection can differ — flexible, but a little discipline still helps. Connect and try the basic operations:
use appdb
db.users.insertOne({ name: "Ada", email: "ada@example.com", roles: ["admin"] })
db.users.find({ roles: "admin" })
db.users.updateOne({ name: "Ada" }, { $set: { active: true } })
db.users.deleteOne({ name: "Ada" })
Notice queries and updates are themselves documents — a style that pairs naturally with JavaScript and Node.
Add indexes for speed
Without indexes, MongoDB scans every document in a collection. Create an index on fields you query often:
db.users.createIndex({ email: 1 }, { unique: true })
The unique option also enforces no duplicate emails at the database level. Review them with db.users.getIndexes(). Indexing the right fields is the difference between instant and sluggish as a collection grows.
FAQ
Why is MongoDB not in the standard Ubuntu repo anymore?
Because of licensing (MongoDB's SSPL), Debian and Ubuntu do not ship current MongoDB. Installing from MongoDB's official apt repository, as above, is the supported route.
Is MongoDB safe to run with default settings?
No — the default has authentication disabled. Always create an admin user and set authorization: enabled before putting any data in it, and keep it bound to localhost.
MongoDB vs a SQL database — when should I use it?
Choose MongoDB when your data is document-shaped, schemas evolve rapidly, or you are deep in the Node ecosystem. Choose PostgreSQL or MySQL for relational data, complex joins, and strict transactions.
How do I check MongoDB's logs?
The service logs to /var/log/mongodb/mongod.log, and sudo journalctl -u mongod -e shows the systemd view. Start there if mongod will not start.
Nxeon VPS hosting for developers gives you full root and NVMe storage to run MongoDB privately next to your Node app — with free migration help to move an existing dataset over.