Sling Academy
Home/MongoDB/How to install MongoDB Shell (mongosh) on Windows, Mac, and Ubuntu

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

Last updated: February 01, 2024

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.

Next Article: Where does MongoDB store data and how to change it

Previous Article: How to view the disk space used by MongoDB

Series: MongoDB Tutorials

MongoDB

You May Also Like

  • MongoDB: How to combine data from 2 collections into one
  • Hashed Indexes in MongoDB: A Practical Guide
  • Partitioning and Sharding in MongoDB: A Practical Guide (with Examples)
  • Geospatial Indexes in MongoDB: How to Speed Up Geospatial Queries
  • Understanding Partial Indexes in MongoDB
  • Exploring Sparse Indexes in MongoDB (with Examples)
  • Using Wildcard Indexes in MongoDB: An In-Depth Guide
  • Matching binary values in MongoDB: A practical guide (with examples)
  • Understanding $slice operator in MongoDB (with examples)
  • Caching in MongoDB: A practical guide (with examples)
  • CannotReuseObject Error: Attempted illegal reuse of a Mongo object in the same process space
  • How to perform cascade deletion in MongoDB (with examples)
  • MongoDB: Using $not and $nor operators to negate a query
  • MongoDB: Find SUM/MIN/MAX/AVG of each group in a collection
  • References (Manual Linking) in MongoDB: A Developer’s Guide (with Examples)
  • MongoDB: How to see all fields in a collection (with examples)
  • Type checking in MongoDB: A practical guide (with examples)
  • How to query an array of subdocuments in MongoDB (with examples)
  • MongoDB: How to compare 2 documents (with examples)