Skip to main content

How to install Mongo Shell (mongosh) on Mac, Ubuntu, CentOS, Windows

MongoDB Shell mongosh is the modern command-line interface for interacting with MongoDB databases. This guide walks you through installing mongosh on Mac, Ubuntu, CentOS, or Windows.

macOS

Homebrew

  1. Install Homebrew (if not already installed):

    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
  2. Install MongoDB Shell:

    brew install mongosh
  3. Verify installation:

    mongosh --version

Using Manual Download

  1. Download the MongoDB Shell package from the MongoDB Download Center

    • Select your macOS version
    • Choose "tgz" as the package format
  2. Extract the downloaded archive:

    tar -zxvf mongosh-*-darwin-x64.tgz
  3. Move the binary to a directory in your PATH:

    sudo cp mongosh-*-darwin-x64/bin/mongosh /usr/local/bin/
  4. Verify installation:

    mongosh --version

Troubleshooting on macOS

  • If you see "command not found" after installation:
    export PATH=$PATH:/usr/local/bin
  • For permission issues:
    sudo chmod +x /usr/local/bin/mongosh

Ubuntu

apt

  1. Import MongoDB GPG key:

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

    If that doesn't work, try:

    wget -qO- https://www.mongodb.org/static/pgp/server-7.0.asc | sudo tee /etc/apt/trusted.gpg.d/mongodb.asc
  2. Add MongoDB repository:

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

    sudo apt update
  4. Install MongoDB Shell:

    sudo apt install -y mongodb-mongosh
  5. Verify installation:

    mongosh --version

Manual Download

  1. Download the MongoDB Shell package:

    wget https://downloads.mongodb.com/compass/mongodb-mongosh_2.1.1_amd64.deb

    Note: Replace the version number with the latest available

  2. Install the package:

    sudo dpkg -i mongodb-mongosh_2.1.1_amd64.deb
  3. Verify installation:

    mongosh --version

Troubleshooting on Ubuntu

  • If you encounter dependency issues:
    sudo apt --fix-broken install
  • For repository key issues:
    sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 656408E390CFB1F5

CentOS

yum/dnf

  1. Create MongoDB repository file:

    sudo tee /etc/yum.repos.d/mongodb-org-7.0.repo << EOF
    [mongodb-org-7.0]
    name=MongoDB Repository
    baseurl=https://repo.mongodb.org/yum/redhat/\$releasever/mongodb-org/7.0/x86_64/
    gpgcheck=1
    enabled=1
    gpgkey=https://www.mongodb.org/static/pgp/server-7.0.asc
    EOF
  2. Install MongoDB Shell:

    # For CentOS 8+
    sudo dnf install -y mongodb-mongosh
    
    # For CentOS 7
    sudo yum install -y mongodb-mongosh
  3. Verify installation:

    mongosh --version

Manual Download

  1. Download the MongoDB Shell package:

    # For CentOS 8+
    wget https://downloads.mongodb.com/compass/mongodb-mongosh-2.1.1.el8.x86_64.rpm
    
    # For CentOS 7
    wget https://downloads.mongodb.com/compass/mongodb-mongosh-2.1.1.el7.x86_64.rpm

    Note: Replace the version number with the latest available

  2. Install the package:

    # For CentOS 8+
    sudo dnf install -y ./mongodb-mongosh-2.1.1.el8.x86_64.rpm
    
    # For CentOS 7
    sudo yum install -y ./mongodb-mongosh-2.1.1.el7.x86_64.rpm
  3. Verify installation:

    mongosh --version

Troubleshooting on CentOS

  • If repository issues occur:
    sudo yum clean all
    sudo yum makecache
  • For SELinux-related issues:
    sudo setenforce 0  # Temporarily disable SELinux

Windows

Using MSI Installer

  1. Download the MSI installer from the MongoDB Download Center

    • Select Windows as your platform
    • Choose "msi" as the package format
  2. Run the installer:

    • Double-click the downloaded MSI file
    • Follow the installation wizard
    • Select the "Complete" installation type
    • Complete the installation
  3. Verify installation by opening Command Prompt and typing:

    mongosh --version

Chocolatey

  1. Install Chocolatey (if not already installed), in PowerShell (Admin):

    Set-ExecutionPolicy Bypass -Scope Process -Force
    [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072
    iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
  2. Install MongoDB Shell:

    choco install mongodb-shell
  3. Verify installation:

    mongosh --version

Troubleshooting on Windows

  • If 'mongosh' command isn't recognized:
    1. Open Environment Variables (search "environment variables" in Start menu)
    2. Under System variables, edit PATH
    3. Add the bin directory (usually C:\Program Files\mongosh\bin)
    4. Restart Command Prompt

Connecting to a MongoDB Server

After installing mongosh, connect to a MongoDB server with:

mongosh "mongodb://hostname:27017"

Or with authentication:

mongosh "mongodb://username:password@hostname:27017/database"

For MongoDB Atlas:

mongosh "mongodb+srv://username:password@cluster.mongodb.net/database"

Using MongoDB Shell Configuration File

You can create a mongosh configuration file to customize your experience:

  1. Create a .mongoshrc.js file:

    • On macOS/Linux: ~/.mongoshrc.js
    • On Windows: %USERPROFILE%\.mongoshrc.js
  2. Add custom configurations, for example:

    // Custom prompt
    prompt = () => {
      return `${db.getName()} > `;
    };
    
    // Custom helper function
    function countDocs(collection) {
      return db[collection].countDocuments();
    }

For more information, visit the official MongoDB Shell documentation.

Explore the standard for database governance

Ask ChatGPT about Bytebase

Get an independent take on whether Bytebase fits your team.

Ask ChatGPT