How to install MongoDB Shell (mongosh) on Windows, Mac, and Ubuntu

Updated: February 1, 2024 By: Guest Contributor Post a comment

Introduction

Installing the MongoDB Shell (mongosh) is the first step many developers take towards interacting with MongoDB, a popular NoSQL database. Mongosh allows you to create, read, update, and delete data within your MongoDB instance, as well as manage the database configuration. In this guide, we will walk through the installation process for mongosh on Windows, Mac, and Ubuntu systems, covering different installation methods and tips for a smooth setup.

Prerequisites

Before installing mongosh, ensure that you have the following:

  • Network connectivity to download mongosh from the internet.
  • Administrative access to your computer (especially for Windows).
  • Basic knowledge of terminal (or command prompt) commands.

Installing on Windows

Using the MongoDB Installer Package

To install mongosh on Windows:

  1. Visit the official MongoDB download page for mongosh.
  2. Select the latest version and download the Windows (.msi) installer.
  3. Run the installer and follow the instructions to install mongosh on your system.
  4. Once installed, you can open mongosh by searching for it in the Start menu, or by running mongosh in the Command Prompt.

Installing via Chocolatey

If you prefer package managers, you can install mongosh using Chocolatey:


choco install mongodb-shell
  

After the installation completes, run mongosh in Command Prompt to confirm the installation.

Installing on Mac

Using the MongoDB Installer Package

For Mac users:

  1. Go to the mongosh download page and download the macOS (.pkg) installer.
  2. Open the downloaded package and follow the installer’s instructions.
  3. Once installed, open a terminal and run mongosh to start the MongoDB shell.

Installing via Homebrew

You can also use Homebrew by running:


brew install mongodb/brew/mongodb-community-shell
 

This will install mongosh as well as the necessary dependencies. To launch, type mongosh in your terminal.

Installing on Ubuntu

Using the Official MongoDB Repository

To install on Ubuntu:

Step 1: Import the MongoDB public key:


wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -

Step 2: Add the MongoDB repository:


echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu $(lsb_release -cs)/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list
  

Step 3: Update the local package database:


sudo apt-get update
  

Step 4: Install mongosh:


sudo apt-get install -y mongodb-mongosh
  

Finally, verify the installation by running mongosh in your terminal.

Verifying Your Installation

Regardless of the operating system, once mongosh is installed, you can verify it by running:


mongosh --version
  

This command should return the version of mongosh that is currently installed on your system. It ensures that mongosh has been installed correctly and that it’s accessible from the terminal or command line.

Connecting to a MongoDB Database

After installing mongosh, you can connect to your MongoDB database with the following command:


mongosh "mongodb://localhost:27017/mydb"
  

Replace “localhost:27017” with the address of your MongoDB server and “mydb” with the name of the database you want to interact with.

Conclusion

With mongosh installed, you are now ready to explore and manage your MongoDB databases across any of these platforms. Installation is just the beginning, now dive into the world of MongoDB operations and queries to take full advantage of your database system.