MongoDB is a document-oriented database popular for APIs, Node.js apps and analytics. Ubuntu does not ship it in its own repositories, so you install it from the official MongoDB repository. This guide sets it up on a VPS Linux Infrawire, turns on authentication and limits who can reach it.
📋 Prerequisites
- A VPS running Ubuntu 24.04 (noble) on x86_64 or ARM64, with root SSH access (IP and root password are in your customer area)
- A CPU exposing the AVX instruction set (required by MongoDB 5.0 and later):
grep -c avx /proc/cpuinfomust return a number above 0 - At least 2 GB of RAM recommended
1. Add the official repository
Import the signing key and declare the repository for Ubuntu 24.04.
Bashapt update && apt install -y curl gnupg curl -fsSL https://www.mongodb.org/static/pgp/server-8.0.asc | gpg -o /usr/share/keyrings/mongodb-server-8.0.gpg --dearmor echo 'deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-8.0.gpg ] https://repo.mongodb.org/apt/ubuntu noble/mongodb-org/8.0 multiverse' > /etc/apt/sources.list.d/mongodb-org-8.0.list
2. Install and start the service
Bashapt update apt install -y mongodb-org systemctl enable --now mongod systemctl status mongod --no-pager
The mongodb-org meta-package installs the server (mongod), the shell (mongosh) and the tools. Data lives in /var/lib/mongodb, logs in /var/log/mongodb/mongod.log.
3. Create the administrator
Authentication is still off, so the local shell connects without credentials. Create the first administrator in the admin database; passwordPrompt() asks for the password so it never appears in your shell history.
Bashmongosh
JavaScript1use admin 2db.createUser({ 3 user: 'admin', 4 pwd: passwordPrompt(), 5 roles: [ { role: 'userAdminAnyDatabase', db: 'admin' }, 'readWriteAnyDatabase' ] 6})
4. Enable authentication
Edit /etc/mongod.conf and add (or uncomment) the security block:
YAMLsecurity: authorization: enabled
Bashsystemctl restart mongod
From now on every connection must authenticate.
5. Create an application user
Your application should never use the administrator account. Give it a user limited to its own database.
Bashmongosh -u admin -p --authenticationDatabase admin
JavaScript1use appdb 2db.createUser({ 3 user: 'appuser', 4 pwd: passwordPrompt(), 5 roles: [ { role: 'readWrite', db: 'appdb' } ] 6})
The matching connection string is mongodb://appuser:PASSWORD@127.0.0.1:27017/appdb?authSource=appdb.
6. Network access and firewall
By default MongoDB listens on 127.0.0.1 only, which is the safest setting when the application runs on the same VPS. To allow another server, add the VPS address in /etc/mongod.conf:
YAMLnet: port: 27017 bindIp: 127.0.0.1,VPS_IP
Then allow only the application server in the firewall, and never open port 27017 to the whole Internet:
Bashufw allow OpenSSH ufw allow from APP_SERVER_IP to any port 27017 proto tcp ufw enable systemctl restart mongod
✅ Verify the installation
Bashmongod --version mongosh --eval 'db.runCommand({ ping: 1 })' mongosh -u appuser -p --authenticationDatabase appdb appdb --eval 'db.test.insertOne({ ok: true })' ss -ltnp | grep 27017
The last command shows which address MongoDB listens on. An unauthenticated query such as db.getUsers() must now fail with an authorization error.
🔧 Troubleshooting
mongod does not start (Illegal instruction or exit code 132) : the virtual CPU does not expose AVX. Check grep avx /proc/cpuinfo; without it MongoDB 8.0 cannot run on that machine.
Failed to start with permission errors : fix the ownership of the data and socket files with chown -R mongodb:mongodb /var/lib/mongodb /var/log/mongodb and remove a stale /tmp/mongodb-27017.sock, then restart. Details are in journalctl -u mongod.
Authentication failed : the user was created in a specific database. Pass the right one with --authenticationDatabase (or authSource in the URI).
Remote connection times out : check bindIp, the ufw status rules and that the client IP matches the one you allowed.
🎉 MongoDB 8.0 is installed, authenticated and ready for your application!