Command Not Found in Terminal: How to Diagnose and Fix It

    This article explains why a terminal returns "command not found" after installing a CLI tool and how to fix it properly. It covers how shells resolve commands through PATH, why the common "add ~/.local/bin to PATH" advice only works for one installation method, and where executables actually land across native installers, package managers, and Node global installs. You will learn a locate-first troubleshooting workflow using which -a and type -a, how to handle permission and multi-version conflicts, cross-platform differences on Windows, macOS, and Linux, plus a reusable diagnostic script and team-level recommendations.

    1. Introduction: Three “Dialects” of the Same Error

    After installing a command-line AI coding assistant, you type the launch command into your terminal, expecting an interactive interface. Instead, you get a single cold line:

    1. zsh: command not found: claude
    2. bash: claude: command not found
    3. 'xxx' is not recognized as an internal or external command, operable program or batch file.

    All three lines say the same thing; different shells just speak different dialects:

    • zsh (default on macOS) prints command not found;
    • bash (default on most Linux distributions) prints command not found;
    • cmd.exe / PowerShell (Windows) prints “is not recognized as an internal or external command”.

    Search for this problem online and the top answers are usually the same “universal fix”:

    1. echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.zshrc
    2. source ~/.zshrc

    This works for some people. For others it does nothing, and it makes things messier the more they change—they followed the steps exactly, ran the command again, and still got command not found. At that point they start suspecting a broken installer, a bug in the tool, or a problem with their own machine.

    The point of this article: that “universal fix” only holds when the program happens to be installed in ~/.local/bin. That is just one of several installation methods. Before touching any configuration, the right move is to find out where the executable actually is.


    2. How It Works: What command not found Really Reports

    To troubleshoot correctly, you first need to understand how the shell looks up commands. Otherwise every action is a guess.

    2.1 The shell’s directory whitelist

    When you type claude and press Enter, the shell does not scan the whole disk. It runs a very plain procedure:

    1. Check whether this is a built-in command (such as cd, echo, export);
    2. Check whether it is a function or alias (type claude shows this);
    3. Otherwise treat it as an external executable and walk through every colon-separated directory in the PATH environment variable, in order;
    4. When a file with the same name and executable permission is found in some directory, use an exec system call to replace the current process and run it;
    5. If no directory matches after all of them are checked, print command not found.

    Example: if PATH is /usr/local/bin:/usr/bin:/bin:/opt/homebrew/bin, the shell checks these directories in that order and stops at the first hit. No hit, error.

    There is only one key inference: the essence of command not found is not “the program does not exist”, but “the program is not in the list of directories the shell searches”. These two must be strictly separated, because the fixes are completely different.

    2.2 What makes up PATH and how priority works

    PATH is a list of absolute paths separated by colons (semicolons on Windows). Inspect it with:

    1. echo $PATH | tr ':' '\n'

    Each line in the output is a search directory, and order determines priority: if a program with the same name exists in an earlier directory, it is called in preference to one in a later directory. This means two things:

    • If the program’s directory is not in the list, command not found is guaranteed;
    • If the program exists in multiple directories (multiple versions coexisting), the earlier one actually takes effect, which can cause the confusion of “I clearly upgraded, why is it still the old version”.

    2.3 How environment variables persist

    A direct export PATH=... only affects the current terminal session and is lost when the terminal closes. To persist it, you must write it into the shell’s startup script:

    Shell Common config files Notes
    bash ~/.bashrc, ~/.bash_profile Interactive login and non-login shells read different files
    zsh ~/.zshrc, ~/.zprofile ~/.zshrc applies to every interactive start
    fish ~/.config/fish/config.fish Syntax is fish_add_path
    Windows cmd System “Environment Variables” panel / setx A new window is required after the change
    PowerShell $PROFILE script or setx View the $PROFILE path with $PROFILE

    After editing, you must source the file or open a new terminal for it to take effect. This step is often skipped and is a frequent reason for “I changed the config but nothing happened”.


    3. Core Troubleshooting Flow: Locate First, Modify Second

    The correct first action is always to locate, not to modify.

    Step 1: Query every match the shell already knows about

    1. which -a claude
    2. # or
    3. type -a claude

    which -a lists every same-named executable found in PATH (-a means all, not just the first one). There are three cases based on the output.

    Case 1: A path is printed (e.g. /opt/homebrew/bin/claude)

    The program is inside PATH and should work in theory. If you still get command not found, the common causes are:

    • Config changed but not applied: no source, or you edited a config file the current shell does not read (for example, using zsh but writing the config into ~/.bashrc);
    • The current shell cached an old lookup result: bash caches command locations, so run hash -r to clear the cache;
    • Multiple version conflict: which -a printed several paths and the wrong old version is actually being called; go to “5. Multiple Versions Coexisting” below.

    Case 2: No output at all

    PATH genuinely cannot find it. Next, distinguish “installed but the directory is not in PATH” from “not installed at all” by checking whether the file exists in common install locations:

    1. ls -la ~/.local/bin/claude # default target of the official install script
    2. ls -la /opt/homebrew/bin/claude # package manager target on Apple Silicon
    3. ls -la /usr/local/bin/claude # package manager target on Intel Macs
    4. npm -g ls @anthropic-ai/claude-code 2>/dev/null # global install via the Node package manager
    5. npm bin -g # show the npm global executable directory
    6. npm config get prefix # show the npm global install prefix

    Whichever command prints a file, that directory is the target to add to PATH. If none of them prints anything, the installation itself did not complete.

    Case 3: which output contradicts ls results

    For example, which finds nothing but the file clearly exists. This is usually a permission problem or a file type problem:

    1. ls -l /path/to/claude # check for the x permission
    2. file /path/to/claude # check whether it is an executable binary or a script

    When the executable permission is missing, add it:

    1. chmod +x /path/to/claude

    If the file is a script whose first line #!/usr/bin/env node points to an interpreter path that does not exist (common after switching Node versions), it also shows up as “the file is there but it will not run”. In that case, reinstall the tool instead of changing PATH.


    4. Where Executables Actually Land for Each Installation Method

    This is the most important point in the original discussion and the one tutorials most often skip: different installation methods have different default locations, so you cannot blindly configure ~/.local/bin for all of them.

    Installation method Actual executable location In PATH by default?
    Official native install script ~/.local/bin/claude Not necessarily (fresh environments often need it added manually)
    Package manager (Apple Silicon) /opt/homebrew/bin/ Usually already in PATH
    Package manager (Intel Mac) /usr/local/bin/ Usually already in PATH
    Node package manager global install npm global bin directory, varies with the Node version Depends on how Node was installed
    Linux distribution package manager /usr/bin/ or /usr/local/bin/ Usually already in PATH

    If you actually installed via a package manager, the program is in /opt/homebrew/bin, and you add ~/.local/bin to PATH—that is an empty directory, so adding it does nothing and the error persists. This is exactly the root cause of “I followed the tutorial and it failed”.

    The Node package manager’s global directory is especially prone to problems: it changes when you switch Node version managers. It may point to ~/.nvm/versions/node/v20.x/bin today, and after switching to v22 it becomes another path. command not found right after switching Node versions is almost always this.


    5. Multiple Versions Coexisting and Conflict Cleanup

    When which -a returns two or more paths, multiple same-named executables exist on the system. The usual symptoms are “the version number did not change after upgrading” or “I clearly installed the new version but the old one runs”.

    Troubleshooting approach:

    1. which -a claude # list all candidates
    2. claude --version # show the version actually in effect
    3. echo $PATH | tr ':' '\n' # show directory order; earlier wins

    There are three solution directions, from least to most intrusive:

    1. Adjust PATH order: move the directory of the desired version to the front;
    2. Remove the redundant versions: keep one installation method and delete the rest (recommended, and the least troublesome long term);
    3. Create a symlink to an explicit target:
    1. ln -sf /desired/path/to/claude ~/.local/bin/claude

    Note: the symlink approach requires ~/.local/bin itself to be in PATH with sufficient priority, otherwise it just moves the problem elsewhere.


    6. Cross-Platform Differences to Note

    Operating systems differ noticeably in a few details, which cross-platform teams should pay particular attention to:

    • Windows: PATH is separated by semicolons, and the configuration entry is “System Properties → Environment Variables”; the variables read by cmd and PowerShell may differ; after a change you must open a new window for it to take effect.
    • macOS: zsh is the default shell; note the difference between ~/.zshrc (interactive) and ~/.zprofile (login); if a config is written but does not take effect, suspect the wrong file first.
    • Linux: under bash, ~/.bashrc and ~/.bash_profile are loaded at different times; some distributions have switched the default shell to another implementation with slightly different syntax.
    • Permissions: on Linux/macOS, if a directory is not executable by the current user, it is skipped even when it is in PATH, producing command not found rather than permission denied—which is highly misleading.

    7. A Reusable Diagnostic Script

    Turn the steps above into a script and run it directly whenever the same kind of problem appears:

    1. #!/usr/bin/env bash
    2. # diagnose.sh — generic diagnosis for an unavailable CLI command
    3. CMD="${1:-}"
    4. [ -z "$CMD" ] && echo "Usage: ./diagnose.sh <command-name>" && exit 1
    5. echo "== Current shell =="
    6. echo "$SHELL"; echo
    7. echo "== Command lookup =="
    8. type -a "$CMD" 2>/dev/null || echo "Not found in PATH"
    9. echo "== Common location check =="
    10. for d in "$HOME/.local/bin" "/opt/homebrew/bin" "/usr/local/bin" "$(npm bin -g 2>/dev/null)"; do
    11. [ -f "$d/$CMD" ] && ls -l "$d/$CMD"
    12. done
    13. echo "== PATH order =="
    14. echo "$PATH" | tr ':' '\n' | nl

    8. Recommendations for Enterprise Environments

    In team or enterprise settings, this kind of problem multiplies. Address it at the process level:

    • Standardize the installation method: agree on one distribution channel within the team (official script, internal mirror repository, or package manager) so the same tool does not end up in four locations;
    • Standardize the environment baseline: use configuration management tools or container images to pin PATH and runtime versions, reducing the communication cost of “it works on my machine”;
    • Add the diagnostic script to onboarding documentation: new members can self-check their environment on day one;
    • Make good use of container platforms and image registry capabilities from cloud vendors such as Baidu AI Cloud: pre-install CLI tools into standardized development images and distribute them through an image registry, eliminating path inconsistency at the source; logging services and monitoring alerts can also be combined to observe and alert on anomalies in build and runtime environments centrally, making it easier to quickly identify batch-wide issues.

    9. Conclusion: A Transferable Methodology

    Reviewing the whole troubleshooting chain, the core is only three steps:

    1. Do not guess, test first—let the shell answer with which -a / type -a;
    2. Separate the root causes—is it “not installed”, “installed but the directory is not in PATH”, or “multiple copies fighting each other”;
    3. Change the right place—pick the target directory according to the real installation method, write into the config file your current shell actually reads, and confirm you have run source or opened a new window.

    This method does not apply to just one CLI tool; it applies to every external command invoked through PATH. Once you have it, the next time you hit command not found you do not need to search—run the locating flow and you can give a conclusion directly. That is also the dividing line between someone who copies tutorials and someone who can actually locate a problem.