DEV Community

Cover image for Installing MongoDB 8.0 with Ansible on Ubuntu & Debian
Athreya aka Maneshwar
Athreya aka Maneshwar

Posted on • Edited on

Installing MongoDB 8.0 with Ansible on Ubuntu & Debian

Hello, I'm Maneshwar. I'm building git-lrc, an AI code reviewer that runs on every commit. It is free, unlimited, and source-available on Github. Star Us to help devs discover the project. Do give it a try and share your feedback for improving the product.

---

MongoDB 8.0 is here — and it’s faster, stricter,and more powerful than ever. Whether you're spinning up a dev server or laying the groundwork for production, automating your setup is key.

In this post, I’ll show you how to install MongoDB 8.0 cleanly using Ansible, the preferred tool of DevOps sorcerers and lazy sysadmins alike.

Let’s go from zero to mongod in ~10 tasks.

What You’ll Need

  • An Ubuntu 22.04 (Jammy) or Debian 12 (Bookworm) machine
  • Ansible installed on your local/dev machine
  • SSH access to your target server

Step-by-Step: Ansible Tasks Breakdown

Let’s break the setup into bite-sized YAML pieces.

1. Install Dependencies

We’ll need GPG and a few tools for downloading MongoDB's GPG key and repo config:

- name: Install prerequisites for MongoDB
  ansible.builtin.package:
    name:
      - gnupg
      - curl
      - wget
    state: present
  become: true
Enter fullscreen mode Exit fullscreen mode

2. Add MongoDB's GPG Key (Debian-style)

Debian Bookworm prefers keys to be stored in /usr/share/keyrings:

- name: Add MongoDB GPG key
  ansible.builtin.shell: >
    curl -fsSL https://www.mongodb.org/static/pgp/server-8.0.asc |
    gpg -o /usr/share/keyrings/mongodb-server-8.0.gpg --dearmor
  args:
    creates: /usr/share/keyrings/mongodb-server-8.0.gpg
  become: true
Enter fullscreen mode Exit fullscreen mode

3. Add MongoDB Repository (Debian)

We add the official MongoDB 8.0 repo:

- name: Add MongoDB repository
  ansible.builtin.apt_repository:
    repo: "deb [ signed-by=/usr/share/keyrings/mongodb-server-8.0.gpg ] http://repo.mongodb.org/apt/debian bookworm/mongodb-org/8.0 main"
    state: present
    filename: mongodb-org-8.0
  become: true
Enter fullscreen mode Exit fullscreen mode

For Ubuntu Jammy (22.04), you can also add:

- name: Add MongoDB repository (Ubuntu)
  ansible.builtin.apt_repository:
    repo: "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/8.0 multiverse"
    state: present
    filename: mongodb-org-8.0-ubuntu
  become: true
Enter fullscreen mode Exit fullscreen mode

This makes it flexible for both systems.

4. Install MongoDB Itself

- name: Install MongoDB
  ansible.builtin.package:
    name: mongodb-org
    state: present
    update_cache: true
  become: true
Enter fullscreen mode Exit fullscreen mode

This installs:

  • mongod (the database server)
  • mongos (the sharding router)
  • mongosh (MongoDB’s newer shell)

5. Start and Enable MongoDB Service

Let’s make sure it starts now and every time the server reboots:

- name: Ensure mongod service is started and enabled
  ansible.builtin.service:
    name: mongod
    state: started
    enabled: true
    daemon_reload: true
  become: true
Enter fullscreen mode Exit fullscreen mode

Now you can check if it’s running:

sudo systemctl status mongod
Enter fullscreen mode Exit fullscreen mode

Bonus: Install and Verify mongosh

- name: Install MongoDB shell
  ansible.builtin.package:
    name: mongodb-mongosh
    state: present
  become: true

- name: Check MongoDB shell version
  ansible.builtin.command: mongosh --version
  register: mongosh_version
  become: true

- name: Display MongoDB shell version
  ansible.builtin.debug:
    var: mongosh_version.stdout
Enter fullscreen mode Exit fullscreen mode

You’ll get clean output like:

ok: [your-server] => {
    "mongosh_version.stdout": "2.0.1"
}
Enter fullscreen mode Exit fullscreen mode

Which tells you that your shell is good to go.

Tip: Alternative GPG method

Some systems still expect keys in /etc/apt/trusted.gpg.d:

- name: Add MongoDB GPG key (alternative method)
  ansible.builtin.shell: >
    wget -qO- https://www.mongodb.org/static/pgp/server-8.0.asc |
    tee /etc/apt/trusted.gpg.d/server-8.0.asc
  args:
    creates: /etc/apt/trusted.gpg.d/server-8.0.asc
  become: true
Enter fullscreen mode Exit fullscreen mode

Use this only if the keyring method gives you issues.

Recap: What We Just Did

With this Ansible playbook, you:

✅ Installed MongoDB 8.0 on Debian or Ubuntu
✅ Pulled and trusted the GPG keys
✅ Set up MongoDB’s official APT repository
✅ Installed and started the mongod service
✅ Verified your shell with mongosh

You can now securely connect via:

mongosh
Enter fullscreen mode Exit fullscreen mode

Or remotely if enabled.

What’s Next?

Now that MongoDB is up, you’ll probably want to:

  • Create an admin user with db.createUser
  • Enable authentication in mongod.conf
  • Add TLS for production
  • Set up backups or replication
  • Mmonitor it with Prometheus or Grafana

MongoDB is powerful, but with Ansible, it’s also reproducible, secure, and version-controlled.

Happy deploying! 🚀


With LiveAPI, you can generate interactive API docs that allow users to search and execute endpoints directly from the browser.

If you're tired of updating Swagger manually or syncing Postman collections, give it a shot.

git-lrc
*AI agents write code fast. They also silently remove logic, change behavior, and introduce bugs -- without telling you. You often find out in production.

git-lrc fixes this. It hooks into git commit and reviews every diff before it lands. 60-second setup. Completely free.*

Any feedback or contributors are welcome! It's online, source-available, and ready for anyone to use.

⭐ Star it on GitHub:

GitHub logo HexmosTech / git-lrc

Free, Unlimited AI Code Reviews That Run on Commit




AI agents write code fast. They also silently remove logic, change behavior, and introduce bugs -- without telling you. You often find out in production.

git-lrc fixes this. It hooks into git commit and reviews every diff before it lands. 60-second setup. Completely free.

See It In Action

See git-lrc catch serious security issues such as leaked credentials, expensive cloud operations, and sensitive material in log statements

git-lrc-intro-60s.mp4

Why

  • 🤖 AI agents silently break things. Code removed. Logic changed. Edge cases gone. You won't notice until production.
  • 🔍 Catch it before it ships. AI-powered inline comments show you exactly what changed and what looks wrong.
  • 🔁 Build a

Top comments (0)