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
-
Install Homebrew (if not already installed):
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
-
Install MongoDB Shell:
brew install mongosh
-
Verify installation:
mongosh --version
Using Manual Download
-
Download the MongoDB Shell package from the MongoDB Download Center
- Select your macOS version
- Choose "tgz" as the package format
-
Extract the downloaded archive:
tar -zxvf mongosh-*-darwin-x64.tgz
-
Move the binary to a directory in your PATH:
sudo cp mongosh-*-darwin-x64/bin/mongosh /usr/local/bin/
-
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
-
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
-
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
-
Update package index:
sudo apt update
-
Install MongoDB Shell:
sudo apt install -y mongodb-mongosh
-
Verify installation:
mongosh --version
Manual Download
-
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
-
Install the package:
sudo dpkg -i mongodb-mongosh_2.1.1_amd64.deb
-
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
-
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
-
Install MongoDB Shell:
# For CentOS 8+ sudo dnf install -y mongodb-mongosh # For CentOS 7 sudo yum install -y mongodb-mongosh
-
Verify installation:
mongosh --version
Manual Download
-
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
-
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
-
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
-
Download the MSI installer from the MongoDB Download Center
- Select Windows as your platform
- Choose "msi" as the package format
-
Run the installer:
- Double-click the downloaded MSI file
- Follow the installation wizard
- Select the "Complete" installation type
- Complete the installation
-
Verify installation by opening Command Prompt and typing:
mongosh --version
Chocolatey
-
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'))
-
Install MongoDB Shell:
choco install mongodb-shell
-
Verify installation:
mongosh --version
Troubleshooting on Windows
- If 'mongosh' command isn't recognized:
- Open Environment Variables (search "environment variables" in Start menu)
- Under System variables, edit PATH
- Add the bin directory (usually
C:\Program Files\mongosh\bin
) - 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:[email protected]/database"
Using MongoDB Shell Configuration File
You can create a mongosh configuration file to customize your experience:
-
Create a
.mongoshrc.js
file:- On macOS/Linux:
~/.mongoshrc.js
- On Windows:
%USERPROFILE%\.mongoshrc.js
- On macOS/Linux:
-
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.