You open Terminal, type mongo and zsh answers zsh: command not found: mongo. The shell is telling you that no program called mongo exists in any folder of your PATH. Today there are two possible reasons, and the first one is far more common than older tutorials admit:

  1. The mongo command no longer exists. The legacy mongo shell was replaced by mongosh. MongoDB staff have confirmed that, as of MongoDB 6.0, the legacy shell is no longer included in the server packages. A fresh install of a current version never creates a mongo command, however correct your PATH is.
  2. The tool is installed, but zsh cannot find it. The folder that holds the program is not in your PATH. This happens often with Homebrew on Apple Silicon, where everything lives under /opt/homebrew, and with manual tarball installs.

This guide shows how to tell which case you have and how to fix it on macOS and Linux. I tested the zsh behaviour described below in zsh 5.9.

The quick fix: use mongosh

Try this first:

mongosh

If it opens a prompt, you are done. Replace mongo with mongosh in your commands and in any tutorial you follow. MongoDB staff note that the commonly used commands are implemented in the new shell, so you are unlikely to miss anything. If zsh says command not found: mongosh as well, continue with the steps below.

Know the three pieces

Part of the confusion comes from MongoDB being several programs, and each one has its own install:

  • mongod is the database server.
  • mongosh is the interactive shell that replaced mongo. It can also connect to remote servers.
  • The Database Tools are mongodump, mongorestore, mongoexport, mongoimport and similar programs. They do not include the shell.

Step 1: Check what is installed

command -v mongosh
command -v mongod

Each command prints a path when the program is on your PATH and prints nothing when it is not. In zsh, which mongo prints mongo not found in that case. To see what the package manager has installed:

# macOS with Homebrew
brew list --formula | grep -i mongo

# Debian and Ubuntu
dpkg -l | grep -i mongo

# RHEL, Rocky Linux and AlmaLinux
rpm -qa | grep -i mongo

Read the result like this:

  • mongosh is found: nothing is broken. Use mongosh.
  • mongosh is installed but not found: your PATH is the problem. Go to step 3.
  • Nothing is installed: go to step 2.

Step 2: Install what is missing

macOS (Homebrew)

Use MongoDB's official Homebrew tap:

brew tap mongodb/brew
brew install mongodb-community
brew install mongosh

The first formula installs the server. According to the tap's README, you install mongosh separately when you need the shell, and mongodb-database-tools for the dump and restore tools. To pin a version, use a name such as mongodb-community@8.0, as in MongoDB's macOS tutorial. To start the server as a background service:

brew services start mongodb-community

Linux

Add MongoDB's package repository for your distribution, then install the shell package. On Debian and Ubuntu:

sudo apt install mongodb-mongosh

The mongodb-org metapackage for MongoDB 5.0 and later already pulls this package in. On RHEL-based systems the command is sudo dnf install mongodb-mongosh, and the package name may carry an OpenSSL variant, such as mongodb-mongosh-shared-openssl11. Start the server with sudo systemctl start mongod.

Any system with Node.js

npm install -g mongosh

The npm package provides a mongosh command. If the server is not running, mongosh reports ECONNREFUSED 127.0.0.1:27017, which is a different problem from a missing command.

Step 3: Add the folder to your PATH in ~/.zshrc

If the program exists but zsh cannot find it, add its folder to PATH. First find the folder. For Homebrew:

brew --prefix
ls "$(brew --prefix)/bin" | grep -i mongo

The prefix is /opt/homebrew on Apple Silicon (M1, M2, M3 and later) and /usr/local on Intel Macs. Then append one line to your zsh profile:

# Apple Silicon Homebrew
echo 'export PATH="/opt/homebrew/bin:$PATH"' >> ~/.zshrc

# Manual tarball install in your home folder
echo 'export PATH="$HOME/mongodb/bin:$PATH"' >> ~/.zshrc

Three details matter here:

  • Use >>, not >. A single arrow overwrites the whole file and deletes your existing settings.
  • Use single quotes around the whole line. They write the text $PATH into the file, so it is expanded every time a shell starts and not once, now.
  • Write $HOME, not ~, inside double quotes. In my test, export PATH="~/fake/bin:$PATH" did not work, because the tilde is not expanded inside quotes, while "$HOME/fake/bin" worked.

For Homebrew itself on Apple Silicon, the Homebrew installer recommends a different line, which sets up the whole environment and not only PATH:

echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile

Run these commands once. Running them twice adds the line twice. Duplicates in PATH are harmless but untidy, and you can remove them by adding typeset -U path to ~/.zshrc, which makes zsh keep each folder only once.

Step 4: Reload the shell

source ~/.zshrc
command -v mongosh
mongosh --version

The source command re-reads the file in your current session. Opening a new Terminal tab has the same effect, and exec zsh replaces the current shell with a fresh one. In my test, command -v mongosh printed nothing before source and the full path afterwards.

Edge cases

  • Apple Silicon, Intel and Linux Homebrew use different folders. The prefix is /opt/homebrew on Apple Silicon, /usr/local on Intel Macs and /home/linuxbrew/.linuxbrew on Linux. If you copy a PATH line from a tutorial, check that the prefix matches yours. If even brew is not found, add the Homebrew line from step 3 to ~/.zprofile.
  • It works in Terminal but not in scripts, VS Code tasks or cron. Terminal opens an interactive login shell, which reads ~/.zshenv, ~/.zprofile and ~/.zshrc. A non-interactive shell reads only ~/.zshenv. In my test, a PATH set in ~/.zshrc was invisible to zsh -c and became visible once the line was in ~/.zshenv. Put the PATH for such tools in ~/.zshenv, or call the program with its full path.
  • Only mongod is missing, or only mongosh. They are separate packages. A machine that only needs to connect to a remote database needs just mongosh.
  • A command moved after a reinstall. If zsh still tries an old location, run rehash to clear its cache of command locations. In my test, a command installed after a failed lookup was found without it, so this is rarely needed.
  • A tutorial insists on the old mongo shell. Use mongosh instead. If you truly need the legacy shell, MongoDB staff suggest downloading a 5.0.x archive, which still contains mongo in its bin folder. That version is old, so use it only for legacy compatibility.

FAQs

What replaced the mongo command?

The MongoDB Shell, mongosh. The legacy mongo shell was deprecated and, as of MongoDB 6.0, it is no longer included in the server packages. The common commands, such as show dbs, use and db.collection.find(), work in the new shell.

How do I check which program zsh runs and where it looks?

Run command -v mongosh to see the path of the program, and print -l $path to list every folder in PATH, one per line. If the folder that contains your program is not in that list, add it as described in step 3.