keepcalculator

06 Numbers and Powers

Zsh Command Not Found: Fixes & Causes

Card No. 06·64151 · Updated · Standard formula, shown below

If you see “zsh: command not found” in your terminal, it means the shell cannot locate the executable you tried to run. The fix is usually to check your PATH variable, install the missing program, or use the correct command name. Below is a complete guide to understanding and resolving this error on macOS and Linux.

What Causes “Command Not Found” in Zsh

Zsh (Z shell) is the default shell on macOS since Catalina and is widely used on Linux. When you type a command, zsh searches through a list of directories stored in the PATH environment variable. If the command’s executable is not in any of those directories, zsh responds with “command not found.” This can happen for several reasons:

  • The program is not installed.
  • The executable is installed but its directory is not in your PATH.
  • You mistyped the command name (e.g., phthon instead of python).
  • The command is a shell builtin or alias that is not available in the current context.
  • The file exists but lacks execute permissions.

Step-by-Step Troubleshooting

Follow these steps in order. Most cases are resolved by step 1 or 2.

  1. Check for typos. Re-type the command slowly. Common mistakes include transposed letters, wrong case, or an extra space. Use Tab completion to let zsh suggest the correct name.
  2. Verify the program is installed. Use which commandname or type commandname. If nothing is returned, the program is not in your PATH. To see if it exists elsewhere, try find /usr -name commandname (may require sudo).
  3. Install the missing program. On macOS with Homebrew: brew install packagename. On Debian/Ubuntu: sudo apt install packagename. On Fedora: sudo dnf install packagename. If you don’t know the package name, search with brew search or apt search.
  4. Check your PATH. Run echo $PATH. It should show a colon-separated list of directories like /usr/local/bin:/usr/bin:/bin. If the directory containing the command is missing, add it to your ~/.zshrc file: export PATH="$PATH:/path/to/directory". Then run source ~/.zshrc.
  5. Check file permissions. If the command exists but is not executable, use chmod +x /full/path/to/command.

Understanding PATH and How Zsh Finds Commands

Your PATH is an environment variable that tells the shell which directories to search for executables. When you type a command without a slash, zsh looks through each directory in PATH from left to right. The first match wins. If no directory contains an executable with that name, you get “command not found.”

You can see your current PATH with echo $PATH. To temporarily add a directory for the current session only, use export PATH="$PATH:/new/dir". To make it permanent, add that line to your ~/.zshrc file. After editing, run source ~/.zshrc or open a new terminal window.

Common directories that should be in your PATH include /usr/local/bin (Homebrew on Apple Silicon), /opt/homebrew/bin (Homebrew on Intel), /usr/bin, and /bin. If you use custom scripts in ~/bin or ~/.local/bin, add those too.

Common Problem Areas and Their Fixes

Some commands are notorious for causing “not found” errors due to version differences or shell configuration.

  • Python vs. Python3. On many systems, python is not installed, only python3. Use python3 or install the python-is-python3 package on Debian/Ubuntu.
  • Node.js and npm. If you installed Node via nvm, you need to source nvm’s script in your ~/.zshrc. Otherwise, npm might not be found.
  • Homebrew on Apple Silicon. If you installed Homebrew on an M1/M2 Mac, the binaries are in /opt/homebrew/bin. Add this to your PATH if it’s missing.
  • Commands run with sudo. When you use sudo, the PATH is often reset to a secure default. If you get “command not found” with sudo but not without, use the full path to the command, e.g., sudo /usr/local/bin/mycommand.
  • Aliases and functions. If you defined an alias in ~/.zshrc but it’s not working, make sure the alias is defined before you try to use it in the same session.

Zsh vs. Bash: Why the Error Message Differs

Bash says “command not found” as well, but zsh’s message is often more helpful because it suggests similar commands. For example, if you type grep but it’s not installed, zsh might say “zsh: command not found: grep” and then “Did you mean: egrep?” This is due to zsh’s correct and correct_all options. You can enable these with setopt correct in your ~/.zshrc to get suggestions for mistyped commands. The underlying cause and fix are the same as in bash, but zsh has a few extra features like hash tables and rehash to refresh the command cache.

Preventing Future “Not Found” Errors

You can reduce the frequency of this error with a few habits:

  • Always use Tab completion to avoid typos.
  • Install software via package managers so they are added to PATH automatically.
  • Keep your ~/.zshrc organized and avoid duplicate PATH entries.
  • After installing a new program, run rehash if you’re using an older zsh version (this is more relevant in bash, but harmless).
  • If you frequently use a custom script, place it in a directory that is already in your PATH, like ~/bin.

When to Use the Full Path

If you know exactly where an executable is, you can run it by providing the full path. For example, if you installed a program to /opt/myapp/bin/runme, you can run /opt/myapp/bin/runme instead of just runme. This bypasses PATH entirely. However, this is inconvenient for regular use. Better to add the directory to your PATH. To find the full path of a command that works, use which commandname.

Frequently Asked Questions

Why does zsh say “command not found” even though I installed the program? The most common reason is that the installation directory is not in your PATH. For example, Homebrew on Apple Silicon installs to /opt/homebrew/bin, which is not always added automatically. Add it to your PATH as described above.

How do I permanently add a directory to my PATH in zsh? Open your ~/.zshrc file in a text editor, add a line like export PATH="$PATH:/your/directory", save, and then run source ~/.zshrc or open a new terminal.

What does “command not found” mean when using sudo? When you run sudo, the system often resets PATH to a minimal set. If the command works without sudo but not with it, use the full path to the executable, e.g., sudo /usr/local/bin/mycommand.

Is “command not found” the same as “permission denied”? No. “Command not found” means the shell cannot find the executable. “Permission denied” means the shell found it but the file is not executable or you lack permission. Check with ls -l and use chmod +x if needed.

Why does zsh suggest similar commands when I type a wrong command? Zsh has a feature called “correct” that you can enable with setopt correct. It suggests alternatives when a command is not found. It’s not enabled by default on all systems.

Can I make zsh automatically find commands in the current directory? By default, zsh does not search the current directory unless you add . to your PATH. This is a security risk, so it is not recommended. Instead, use ./commandname to run a file in the current directory.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →