If you're seeing "zsh: command not found" when trying to use a custom command in your Zsh shell, it means the system can't locate the executable for that command. This is usually a simple fix that involves either installing the missing package or correcting your shell configuration. The quickest solution is to install the tool or package that provides the command using your package manager—if you're missing a command like `ffmpeg`, `curl`, or a custom script, installing the corresponding package will resolve it.

What You'll Need

  • The package manager for your system (e.g., `brew`, `apt`, `dnf`, or `pacman` depending on OS)
  • Admin or sudo privileges to install packages (or your user's package manager credentials if configured)
  • A working internet connection to download the package
  • If the command is a custom script, the full path to its source file

Step-by-Step

  1. Check if the command exists in your PATH. Run this command to verify where the shell looks for executables: echo $PATH If your command isn't listed in the output, it means the shell can't find the executable. Next, find where it *should* be installed.
  2. Search for the package that provides your missing command. The command's name might not directly match the package name. Use your package manager to find the correct package:
    • Homebrew (macOS/Linux): Run brew search (e.g., brew search ffmpeg)
    • APT (Debian/Ubuntu): Run apt search or apt-file list
    • DNFLinux/DNF (Fedora/RHEL): Run dnf provides
    • Pacman (Arch Linux): Run pacman -Qn | grep
    Example output: If you're missing `git`, search for git to find the package name (usually git itself).
  3. Install the package using your package manager. Once you have the package name, install it with:
    • brew install (Homebrew)
    • apt install (APT)
    • dnf install (DNFLinux/DNF)
    • pacman -S (Pacman)
    If the package name doesn't match your command (e.g., nmap is installed as nmap but you might have net-tools for `netstat`), install the correct one.
  4. Verify the command works. After installation, test if the command is now found by typing it directly or running: which If it returns a path (e.g., /usr/local/bin/git), the command is available. If not, proceed to the next step.
  5. If the command is custom (e.g., a script), add its path to your PATH. If you created or downloaded a script (e.g., /home/user/my_script.sh) and it’s not in your PATH, add its directory to your shell's environment variable:
    1. Open your shell configuration file (e.g., ~/.zshrc, ~/.bashrc, or ~/.config/fish/config.fish—use echo $SHELL to find it).
    2. Add this line to append the script's directory to PATH: export PATH="$PATH:/path/to/script/directory" (Replace /path/to/script/directory with the actual path, e.g., /home/user/scripts.)
    3. Save the file and reload your shell: source ~/.zshrc (or the correct config file for your shell).

If That Did Not Work

Try these additional steps:
  1. Check for typos in the command name. Run: type This tells you if the command exists but has a different name (e.g., type git might return git is aliased to git ls-files --ignored --).
  2. Search for the executable directly. Use a file search tool like: find / -type f -name "**"> (Replace ** with the command name. This may take a few minutes and shows all files matching the name. If the executable is in a non-standard location, you’ll need to add that path to PATH.)
  3. Ensure the script is executable. If you’re using a custom script, make sure it has the correct permissions: chmod +x /path/to/script.sh Then add its directory to PATH as shown above.
  4. Reinstall the package. If the package was installed incorrectly, uninstall it and reinstall: brew uninstall # Homebrew apt remove # APT Then reinstall it.
  5. Restart your terminal or shell. Sometimes, changes to PATH or shell config files require a terminal restart to take effect.

Common Mistakes

  • Using the wrong package. The package name for curl is sometimes libcurl or curl, depending on your system. Run which curl to check which package installed it.
  • Missing dependencies. If you installed a package but the command still isn’t found, it might require dependencies. On Homebrew, run: brew install --with-deps (APT/DNF/Pacman do this automatically, but check if the package provides a flag like -dev for library files.)
  • Not updating your shell config. If you added a path to PATH but didn’t reload your shell, changes won’t take effect until you restart or run source ~/.zshrc.
  • Running the command incorrectly. If you used ./script.sh instead of script.sh, the shell won’t find it even if it’s in PATH. Always run the name as it appears in the PATH.
  • Permissions issues. If you’re running a script as another user (e.g., via sudo), ensure the script has execute permissions for all users: chmod +x /path/to/script.sh (If it’s owned by root, you may need sudo to run it.)

Tips

  • Use type and which to troubleshoot. type tells you if the command is a built-in, alias, or executable. which shows the full path to the executable. If neither works, the command is either missing or misconfigured.
  • Avoid overwriting system tools. If you’re installing packages from source or modifying scripts, ensure you don’t replace system executables (e.g., /usr/bin/python vs. /usr/local/bin/python). Use which python to check the correct path.
  • Check for syntax errors in custom scripts. If you created a script, run a shell checker like shellcheck (install via Homebrew: brew install shellcheck) to find errors before running it.
  • Use aliases for frequently used commands. If you frequently need a command that’s not installed, add an alias to your shell config (e.g., alias myalias="command") instead of manually installing it.
  • Update your system regularly. Outdated packages can cause compatibility issues. Run: brew update && brew upgrade # Homebrew apt update && apt upgrade # APT dnf upgrade # DNF to keep your tools up-to-date.

Troubleshooting

  1. The command exists but isn’t found. Run find / -name "** 2>/dev/null to locate the executable. If it’s in a non-standard location (e.g., /opt/local/bin), add that path to PATH: export PATH="$PATH:/opt/local/bin" Then reload your shell.
  2. I installed the package but the command still isn’t found. Check which package installed it: which || dpkg -L # APT brew which # Homebrew If the executable isn’t listed, the package might not have been installed correctly. Reinstall the package.
  3. I’m missing system dependencies. For Homebrew packages, use: brew install --with-deps For APT/DNF/Pacman, check if the package includes a -dev flag: apt install -dev If you’re unsure, search for the package’s documentation online.
  4. I’m using a custom script and it still fails. Run ls -la /path/to/script.sh to check for execution permissions. If the script is in a non-standard location (e.g., ~/.local/bin), ensure ~/.local/bin is in your PATH: mkdir -p ~/.local/bin && export PATH="$HOME/.local/bin:$PATH" Then reload your shell.
  5. My shell won’t load changes. If you added a path or alias to your shell config, restart your terminal or run: exec zsh (or source ~/.zshrc if your shell supports it).
  6. The command is a built-in or alias but not recognized. Verify with type . If it’s an alias, edit your shell config (e.g., ~/.zshrc) and remove or correct the alias: unalias or alias ="correct-command" Then reload your shell.

Frequently Asked Questions

Why does "zsh: command not found" happen?

This error occurs because the shell (Zsh) cannot locate the executable for the command you’re trying to run. This happens if the command is missing, misnamed, or not in your PATH environment variable.

How do I permanently fix this?

Install the correct package for the command, add its directory to PATH (if it’s a custom script), or create an alias in your shell config. Always check with which or type before proceeding.

What if I don’t know the package name for the command?

Use your package manager’s search tool (e.g., brew search, apt search) or search online for the command’s package name. For example, if you want ffmpeg, search for ffmpeg to find the correct package.

Why can’t I find my command in PATH?

The command might be in a non-standard location (e.g., /usr/local/bin or a user-specific directory like ~/.local/bin). Run find / -name "** 2>/dev/null to locate it, then add its directory to PATH.

How do I add a script to my PATH?

Add a line to your shell config (e.g., ~/.zshrc) like: export PATH="$PATH:/path/to/script/directory" Then reload your shell with source ~/.zshrc. Ensure the script has execute permissions with chmod +x /path/to/script.sh.

What should I do if I installed the package but the command still doesn’t work?

Check which package installed it: which || dpkg -L # APT brew which # Homebrew If the executable isn’t found, reinstall the package or check for missing dependencies.