The error "command not found" in WSL (Windows Subsystem for Linux) means your terminal cannot locate the command you’re trying to execute. This usually happens when the command isn’t installed in your WSL distro, isn’t in your `PATH`, or has typos. Here’s how to resolve it quickly—whether it’s a simple missing package, misconfiguration, or corrupted setup.
What You'll Need
- Access to your WSL distro (Ubuntu, Debian, etc.) running in Windows Terminal or Command Prompt.
- Administrator rights for Windows to install updates or extensions (like WSLg).
- A command-line editor for troubleshooting (e.g., `sudo`, `which`, or `where`).
- If the command is a custom script, its source files in the correct directory.
Step-by-Step Fixes
- Check for Typos:
Run `echo $PATH` to confirm the command’s directory is in your `PATH`. If it isn’t, add it manually:
export PATH=$PATH:/your/custom/pathSave the new `PATH` by adding the command to your shell’s startup file (e.g., ~/.bashrc or ~/.zshrc). Example for Bash:echo 'export PATH=$PATH:/custom/path' >> ~/.bashrcThen reload with `source ~/.bashrc`. - Install the Command (if missing):
Use your Linux package manager to install the command. For example:
sudo apt update && sudo apt install git(Ubuntu/Debian). For Alpine Linux, use `apk add git`. Note: If you get permission errors, ensure you’re using `sudo` correctly (e.g., `sudo apt install`). - Verify `sudo` Permissions:
Run `which sudo` to check if it exists. If missing, reinstall:
sudo apt install sudoIf the issue persists, try running commands without `sudo` first; it may be a permission issue. - Test Basic Commands:
Ensure core Linux commands like `ls`, `cat`, or `grep` work. If they don’t, your WSL distro may be corrupted. Reset with:
wsl --unregister(Replace `&& wsl --install -d ` with your distro, like `Ubuntu-22.04`.) Restart Windows Terminal afterward. - Check WSLg (GUI Support):
If you’re using WSLg for a graphical shell, ensure it’s enabled:
- Open Windows Terminal and set the profile to use WSLg.
- Run `wsl --shutdown` and restart Windows Terminal.
- Verify with `wsl -l -v` (should show "WSLG enabled").
- Manual Path Addition:
If the command isn’t in `PATH` but exists in a directory, add it manually:
export PATH=$PATH:/path/to/commandThen reload the shell. Example:export PATH=$PATH:/usr/local/bin
Common Mistakes
- Forgetting to Reload the Shell: After modifying `PATH`, `~/.bashrc`, or `~/.zshrc`, always run `source ~/.bashrc` or restart the terminal. Without this, changes won’t apply.
- Using Incorrect Package Names: Check command names in Linux docs (e.g., `man git` or `apt show git`). A typo like `gitl` won’t install or run.
- WSL Distro Corruption: If the distro itself is broken, resetting it (Step 5) fixes most issues, but back up data first if needed.
- WSLg Conflicts: If you switch between WSLg and text mode, commands may behave inconsistently. Stick to one mode for stability.
Tips
- Use `whereis` or `which`: Run `which
` (e.g., `which python`) to find the command’s location. If it returns nothing, the command isn’t installed. - Debug with Verbose Mode: Run `command --help` (e.g., `git --help`) to check for typos or hidden flags.
- Check WSL Status: Run `wsl --status` to confirm the distro is active and check for version issues.
- Virtual Environments: If the command is Python-related, use a virtual environment:
python3 -m venv myenv && source myenv/bin/activate
Troubleshooting
- If the Command Still Fails: Reinstall the WSL distro via the Microsoft Store (Settings > Apps > Windows Subsystem for Linux > Uninstall). This clears corruptions without losing user data.
- Permissions Issues:
If you get `permission denied` errors, use `sudo` sparingly. Check `~/.bashrc` or `~/.zshrc` for misconfigured permissions:
chmod +x ~/.bashrc(Only do this if you’re certain the file isn’t critical.) - Windows Terminal vs. CMD:
- If using Command Prompt instead of Windows Terminal, ensure it’s running WSL 2. Launch CMD as admin and run:
wsl --installThen restart CMD. - For older WSL versions (1.x), use `wsl` in CMD (not Windows Terminal) and avoid WSLg.
- If using Command Prompt instead of Windows Terminal, ensure it’s running WSL 2. Launch CMD as admin and run:
- Custom Installations:
If you compiled a command from source, ensure it’s in your `PATH` and the build directory is accessible. Run:
export PATH=$PATH:/path/to/buildbefore using it.
Frequently Asked Questions
Why does "command not found" happen in WSL?
It usually means the command isn’t installed, isn’t in your `PATH`, or you mistyped it. WSL doesn’t include all Windows commands by default—only Linux-native utilities.
How do I add a missing command permanently?
Install it via your Linux package manager (e.g., `apt install
Why can’t I run Python in WSL?
Python may not be installed by default. Install it with `sudo apt install python3` (Ubuntu/Debian). If you’re using a virtual environment, activate it first (`source myenv/bin/activate`).
Is WSLg necessary for running commands?
No, but WSLg offers a graphical terminal. Commands work in text mode (WSL 2) or GUI mode (WSLG). Switching modes can cause inconsistencies.
How do I reset WSL without losing my data?
Run `wsl --shutdown` in Windows Terminal, then reinstall the distro via the Microsoft Store (Settings > Apps). User files like home directories persist unless you unregister the distro.
Why do I get "command not found" after updating WSL?
Updates may break `PATH` or install commands in unexpected locations. Reinstall the command or run `sudo apt autoremove` to clean up dependencies.