Quick answer: Run type command to see what the shell thinks, then check your PATH with echo $PATH. If the binary's directory isn't there, add it to ~/.bashrc. If it is, run hash -r to clear the shell's command cache.
I know this error is infuriating. You just installed something, or maybe you've used it for months, and suddenly bash: command not found pops up. The program is sitting right there in /usr/bin, but the shell swears it doesn't exist. This tripped me up the first time too, back when I was messing with a custom Python install on Ubuntu 16.04. The fix is usually boring and quick, but it's worth understanding what's going on so you can solve it in seconds next time.
The shell finds commands by searching a list of directories stored in the PATH environment variable. If the directory containing the executable isn't in that list, the shell won't look there. Also, bash keeps a hash table of recently executed commands for speed — if you moved or deleted a file, the hash can point to the wrong place. Both issues produce the same confusing message.
Step-by-step Fix
- Find the actual path to the command. Use
whichortype. For example:
If that returns nothing, the tool might be in a non-standard location. Trywhich python3find / -name python3 -type f 2>/dev/null— this searches the whole system, but give it a minute. - Check your PATH variable. Run:
Look for the directory from step 1. If it's missing, that's your problem. For instance, if the binary lives inecho $PATH/usr/local/binand that directory isn't listed, the shell can't find it. - Add the directory to PATH. Open your
~/.bashrcfile and add this line (adjust the path to match your situation):
Save the file, then runexport PATH="/usr/local/bin:$PATH"source ~/.bashrcto apply the change immediately. For system-wide changes, edit/etc/environmentinstead, but that's overkill for most cases. - Clear the bash hash table. If the command was in PATH and still fails, the hash might be stale. Run:
This wipes the cached locations. Try the command again.hash -r - Test it. Run your command. If it works, you're done. If not, move to the alternatives below.
If That Doesn't Work
Sometimes the issue is more specific. Here are a few other culprits I've seen:
- Wrong shell. If you're using
zshorfish, your bashrc edits won't apply. Check withecho $SHELLand edit the right config file (~/.zshrcfor zsh,~/.config/fish/config.fishfor fish). - Executable permission missing. The file exists, but it's not marked as executable. Fix with
chmod +x /path/to/command. - Symbolic link broken. The command is a symlink pointing to a non-existent file. Use
ls -l $(which command)to check. If you see a red flashing link, recreate it withln -s /real/path /usr/local/bin/command. - Environment override. Some scripts set PATH to a minimal value. If this happens inside a specific script, run
source ~/.bashrcafter that script runs, or export the full PATH at the top of your script.
Prevention Tips
To stop this from happening again, keep a few habits:
- Always install programs to standard locations like
/usr/local/binand make sure that's in your PATH. - After installing via source or a custom script, run
hash -rbefore freaking out. - If you edit
~/.bashrc, runsource ~/.bashrcinstead of opening a new terminal — it's faster and catches typos immediately.
This error is rarely serious, but it can stall you. Now you've got the tools to squash it in under a minute. The next time you see "command not found," you'll know exactly where to look.