The error "command not found" on Ubuntu means the system cannot locate the executable file for the program you are trying to run in the current shell session. This usually happens because the program is not installed, or it is installed in a directory that is not included in the system's PATH environment variable. To fix this, you must either install the missing software using your package manager (like apt) or modify your shell configuration to include the correct directory in your PATH.

What You'll Need

  • A computer running Ubuntu (Desktop or Server).
  • Access to a terminal or command prompt.
  • Administrator privileges (sudo) to install new software or edit system configuration files.
  • A text editor such as nano, vim, or gedit.

Step-by-Step

  1. Determine the missing command. When you type a command and receive the "command not found" error, note the exact name of the command you are trying to use. For example, if you type python3 and get an error, you know you need the Python interpreter.
  2. Check if the package is installed. Before changing system settings, verify that the software is actually present on your system but perhaps just not accessible. Run the following command to search for the package containing your command:
apt-cache search <command_name>
  1. Install the missing package. If the package is found in the search results, install it using the apt package manager. You will need to use sudo to gain the necessary permissions. Replace <package_name> with the name of the package you found in the previous step:
sudo apt update
sudo apt install <package_name>

This step resolves the issue for most users, as the command simply needs to be downloaded and installed from Ubuntu's repositories.

  1. Verify the installation. Once the installation is complete, type the command again to ensure it is now recognized by the shell:
<command_name>

If the command executes successfully, you are finished. If you still see the error, the command might be installed in a directory that is not in your current PATH.

  1. Locate the executable file. If the software is installed but still not found, you need to find where the executable file is located. Use the which command (if it exists) or the find command to search your system. For example:
find /usr -name "<command_name>"

This command searches the /usr directory and its subdirectories for a file named <command_name>. It is a good idea to also check the /bin and /sbin directories, which are standard locations for system binaries.

  1. Check your PATH variable. The PATH variable is a list of directories that the shell searches through, in order, when you type a command. If the directory containing your command is not in this list, the shell will not find it. View your current PATH by typing:
echo $PATH

This will output a colon-separated list of directories, such as /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin.

  1. Edit your shell configuration file. To permanently add a directory to your PATH, you need to edit your shell configuration file. The file you edit depends on which shell you are using. The most common shell is bash, and the configuration file is ~/.bashrc for users and /etc/bash.bashrc for the system-wide settings.

Open the file using a text editor. For example, to edit your user's .bashrc file with nano:

nano ~/.bashrc
  1. Add the directory to the PATH. Scroll to the end of the file and add a line that exports the PATH variable, including the directory you found in step 5. The syntax is export PATH="$PATH:/path/to/your/directory". For example, if you found your command in /opt/myapp/bin, you would add:
export PATH="$PATH:/opt/myapp/bin"
  1. Save and apply the changes. In nano, press Ctrl+O to save the file, then Enter to confirm. Press Ctrl+X to exit the editor. To apply the changes to your current session, run the following command:
source ~/.bashrc

This command reloads the configuration file without requiring you to restart your terminal or log out and back in.

  1. Test the command again. Type the command you were having trouble with. It should now execute successfully because the shell has been updated with the new location.

Common Mistakes

  • Typing the command incorrectly. The error message is clear, but users often mistakenly type pythpn instead of python or apt-get install instead of apt install. Always double-check the spelling and capitalization of the command.
  • Editing the wrong configuration file. If you are using a different shell, such as zsh or fish, editing ~/.bashrc will not affect your new shell. For zsh, the configuration file is typically ~/.zshrc. Check which shell you are using by typing echo $SHELL.
  • Adding the directory without a trailing slash. When adding a path, it is good practice to include a trailing slash to indicate it is a directory, though it is not strictly required by the shell. For example, /opt/myapp/bin/ is slightly more explicit than /opt/myapp/bin.

Tips

  • Use the type command. The type command provides information about a command. If you type type <command_name>, the output will tell you whether the command is built-in, an alias, a function, or an executable file located in a specific directory. This is a quick way to diagnose the problem.
  • Install software from the official repository first. Ubuntu's official repositories contain a vast amount of software. Before you download and install a program from a third-party website, check if it is available in the apt repositories. This ensures you get a secure and up-to-date version.
  • Check for typos in file paths. When manually editing the PATH variable, ensure you do not accidentally introduce a typo in the directory path. A single missing character will break the export and cause the command to not be found.

Troubleshooting

If you have followed the steps above and still cannot run the command, try the following troubleshooting steps in order.

If that did not work, verify that you are using the correct shell. If you have switched shells recently, the configuration file you edited might not be the one being used by your current shell session. To check, run echo $SHELL. If you intended to use zsh but are still in bash, you need to switch to zsh by typing zsh or by changing your default shell using the chsh command (which requires administrator privileges).

If that did not work, check for permission issues. Even if the file is in your PATH, the shell might not have permission to execute it. Check the file's permissions by typing ls -l /path/to/your/command. If the execute bit (the x) is missing, you can add it using chmod +x /path/to/your/command.

If that did not work, verify the full path. Sometimes, the command might be in a directory that is not intended to be in the user's PATH. In this case, you can run the command using its full path. Simply type the full path to the executable file, for example: /opt/myapp/bin/<command_name>. This confirms that the program is working but just needs to be in your PATH for convenience.

If that did not work, restart your terminal. In some cases, the shell configuration might not be applied correctly, or there might be a caching issue. Simply closing and reopening your terminal window or SSH session will force it to reload the configuration files from scratch.

Frequently Asked Questions

What does the "command not found" error mean?

It means the shell cannot find an executable file with that name in the directories listed in your PATH environment variable. The command might not be installed on your system, or it might be in a directory that is not included in your PATH.

Why is my PATH not working after I edit it?

The most common reason is that you did not source the configuration file. You must run source ~/.bashrc (or the equivalent for your shell) for the changes to take effect in your current session. You also need to make sure you are editing the correct configuration file for the shell you are using.

Can I add multiple directories to my PATH?

Yes. You can add as many directories as you like. Simply separate them with colons. For example: export PATH="$PATH:/opt/myapp/bin:/usr/local/custom/bin".

Should I edit the system-wide or user configuration file?

If you only want the change to apply to your user account, edit the user configuration file (e.g., ~/.bashrc). If you want the change to apply to all users on the system, edit the system-wide configuration file (e.g., /etc/bash.bashrc). Editing the system-wide file requires administrator privileges.

What is the difference between apt and apt-get?

Both are package managers used to install and manage software on Ubuntu. apt is the newer, more user-friendly interface that provides a better command-line experience. apt-get is the older, more traditional interface that is still used for many system administration tasks. For installing packages, apt install is generally recommended over apt-get install.

Is it safe to add third-party directories to my PATH?

Generally, it is not recommended to add third-party directories to your PATH unless you are certain of their safety and legitimacy. Adding a malicious directory to your PATH could allow an attacker to replace a system command with a malicious script. Always verify the source of the software and the directory you are adding.