Skip to content

Instantly share code, notes, and snippets.

@alban-care
Created June 13, 2025 13:49
Show Gist options
  • Save alban-care/f7ae417153550d61ed4458231886fde1 to your computer and use it in GitHub Desktop.
Save alban-care/f7ae417153550d61ed4458231886fde1 to your computer and use it in GitHub Desktop.
Install MongoDB and MongoDB Compass on WSL2

Install MongoDB and MongoDB Compass on WSL2

sources:

Compatibility verifications

MongoDB 8.0 Community Edition supports the following 64-bit Ubuntu LTS

  • 24.04 LTS ("Noble")
  • 22.04 LTS ("Jammy")
  • 20.04 LTS ("Focal")

MongoDB only supports the 64-bit versions of these platforms.

cat /etc/lsb-release

Output should be similar to:

DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=24.04
DISTRIB_CODENAME=noble
DISTRIB_DESCRIPTION="Ubuntu 24.04.2 LTS"

Install MongoDB Community Edition

  1. Import the public key
cd ~
sudo apt-get update
sudo apt-get install gnupg curl
curl -fsSL https://www.mongodb.org/static/pgp/server-8.0.asc | \
   sudo gpg -o /usr/share/keyrings/mongodb-server-8.0.gpg \
   --dearmor
  1. Create the list file for MongoDB

To create the list file for Ubuntu 24.04 (Noble) for example:

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" | sudo tee /etc/apt/sources.list.d/mongodb-org-8.0.list
  1. Reload the package database
sudo apt-get update
  1. Install MongoDB Community Server
sudo apt-get install -y mongodb-org
  1. Create the data directory
sudo mkdir -p data/db
sudo chown -R `id -u`:`id -g` data/db

Install MongoDB Compass

  1. Download the latest version of MongoDB Compass from the MongoDB Download Center.
  2. To install the downloaded package on Windows, you can use double-click the .msi file.
  3. Follow the installation instructions in the installer.
  4. After installation, you can launch MongoDB Compass from the Start menu or by searching for "MongoDB Compass" in the Windows search bar.

Start MongoDB

  1. Start the MongoDB service
sudo systemctl start mongod
  1. Verify that MongoDB has started successfully
sudo systemctl status mongod
  1. Enable MongoDB to start on boot
sudo systemctl enable mongod

Connect to MongoDB using Compass and WSL2

  1. Open MongoDB Compass.

Warning: When using MongoDB installed on WSL2, you need to connect to the MongoDB instance using the correct connection string. Since WSL2 runs a separate instance of Linux, you will typically connect to it using localhost and the default port 27017.

if the connection fails, you may need to ensure that the MongoDB service is running in WSL2 and that you are using the correct IP address or hostname.

  1. In the "New Connection" dialog, enter the connection string:

    mongodb://localhost:27017
    
  2. Click "Connect" to establish the connection to your MongoDB instance running on WSL2.

  3. You should now be able to view and manage your MongoDB databases and collections through MongoDB Compass.

Enjoy! ๐Ÿš€

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment