Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

Typing mongo on Ubuntu gives command not found after installing MongoDB

Errormongo: command not found

Short answer

The current MongoDB documentation connects with mongosh, and the old mongo shell is called legacy. So first run mongosh instead of mongo. If mongosh is also not found, it is simply not installed: it is a separate package, and a tgz install of the server does not include it. Add mongodb-mongosh.

  • Applies to MongoDB 9.0 Community, mongosh, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    See which of the two shells exists on the machine.

    command -v mongosh mongo
    mongosh --version
  2. 2Match what you see, then jump to its fix

    1. 1mongo: command not found, but mongosh --version prints a versionYou are typing the legacy command nameFix
    2. 2mongosh: command not found as wellmongosh is not installedFix

What does the error look like?

  • mongo: command not found
  • mongosh: command not found

Cause 1 of 2

You are typing the legacy command name

1Check

The MongoDB install guide connects to a deployment with mongosh, and the production notes call mongo the legacy shell.

command -v mongosh

2Fix

Use mongosh. It connects to mongodb://localhost:27017 by default.

mongosh

3Verify

The prompt opens and a server status command answers.

mongosh --eval 'db.runCommand({ ping: 1 })'

Confirmed by mongodb.com

Did this fix it?

Cause 2 of 2

mongosh is not installed

1Check

mongosh ships separately from the server. The install guide says that with the tgz package you must install mongosh yourself. On Ubuntu, the mongosh page installs it from MongoDB's apt repository as mongodb-mongosh.

apt list --installed 2>/dev/null | grep -i mongosh

2Fix

With MongoDB's repository already added, install the package. For a tgz install, follow the mongosh install page instead.

sudo apt-get update
sudo apt-get install -y mongodb-mongosh

3Verify

Confirm the version and connect.

mongosh --version
mongosh

Confirmed by mongodb.com

Did this fix it?

How do I stop it coming back?

  • Use mongosh in scripts and notes from the start, not mongo.
  • After a tgz install, install mongosh as its own step.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Related fixes

Servers

dnf cannot install MySQL on Amazon Linux 2023

dnf cannot find a mysql-server package on Amazon Linux 2023

Amazon Linux 2023 documents MariaDB, not MySQL, as its database server, with packages named mariadb105 and mariadb105-server.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.