Zsh command not found

If you came across the annoying “zsh command not found” error then worry not, we got you covered. In this article, we are going to see how you can resolve this error in multiple ways.

Before we start, know that we will be discussing how to fix the error on macOS and Linux. So if you are getting errors on other platforms or languages like python, please check other discussion blogs.

How to Fix “Zsh command not found” error in Linux or macOS

If you are getting a “zsh command not found” error on your macOS or Linux machine, then it can be hard going through all the discussion blogs to find the solution. So we went through all those decisions and got the tried and tested solutions.

Now these are the most common reasons for getting the error and you can save time by identifying it first:

  • The command you are trying to execute may be invalid.
  • Maybe the path environment is not setup for the package
  • The command is valid but the package is not installed on your device.
  • The command may have typo or maybe misspelled
  • The command path is not set up on your device (.zshrc file)

If none of the above-mentioned reasons ring a bell, then check these errors you can get when using the Z shell (zsh) on Linux, macOS, or Unix:

  • zsh command not found docker
  • zsh command not found npm
  • zsh command not found node
  • And more

Now in the solutions, we are going to see, it will not matter which of the above-mentioned error you got. We will see all the possible solutions for “zsh: command not found”. We will suggest you go through all the solutions one by one since we will see easy to hard difficulties.

zsh command not found

Why you should use “zsh” instead of “bash” shell?

You need to change the default “bash” to “zsh” if you want to use use features like automatic CD, theme correction, recursive path, and more. Here, zsh stands for “Z shell” and bash stand for “Bourne Again Shell”.

NOTE: If you are using the Linux distro, then bash will be installed by default.

Now, if you want to install zsh on your Ubuntu or macOS, then use the following command or use an online guide:

$ sudo apt install zsh

If you want to download package information, then you can use the update command. “Sudo apt-get update” will download package info for all the configured sources.

Now, when you will run the above mentioned command, the package information will be downloaded. This is very useful when you want to get the information on packages or their dependencies (updated version).

sudo apt-get update

sudo apt-get install zsh

After using the above mentioned commands, the ZSH shell will be installed on your device. But if you want to check if ZSH is working, then you can use the below mentioned command.

zsh –version

Check whether “command is valid” to avoid zsh command not found error

If you are getting the error “command not found” on macOS, Unix, or Linux. Then you should first check if the entered command is valid and does not contain any typo. In most cases, there may be a typo which is causing the error. You can check if the command is valid or not by using below mentioned command.

dev-ubuntu-2104% cls (trying to run this cls command)

Zsh: command not found: cls (getting error output)

We are getting errors here because the screen in Linux is clear and NOT cls. This is why it’s very important to check if the syntax and the command are correct before we look for solutions everywhere.

Check if the command is not misspelled (if you are getting the zsh command not found error)

Another very common mistake people generally make is not checking spelling and spending hours looking for a solution. And finding out later that you made a simple typo is very frustrating. For example, if you try executing “nestat” then you will get the error “zsh command not found”. Here you should have been using “netstat” in the first place and check if it’s a typo.

So, before you look for the solution everywhere, check if it’s a typo or not.

“Package is installed” can help in fixing the zsh command and not found error

Sometimes we can run commands and not check if the package is installed on our device or not. This can happen if you regularly use a different device and try to run commands on a new device.

Now, if you are sure that it’s not a typo issue then for example we can run the “npm” command in Ubuntu.

dev-ubuntu-2104% npm

zsh: command not found: npm

This error came because the npm package was not installed on the device with Ubuntu. Now to solve this, we need to install the npm package on our device and run the command again. Use the command below to install npm:

dev-ubuntu-2104% sudo apt install npm

Check “$PATH” Environment variable is correctly set to fix the “zsh: command was not found error

If you have checked that package is installed and there is no typo and error is still coming, then we can move forward.

Another reason for the error can be the invalid or missing PATH variable on your device. For any package, the PATH variable should be set up since many time the PATH may not set up automatically.

Now, for most packages, the path variable is set automatically during the initial installation. But in some cases, maybe due to custom installation, the path variable did not set up and not causing issues. We can take example of maven package, you can run the “mvn” command to check if it’s installed.

dev-ubuntu-2104% maven (command)

zsh: command not found: mvp (output)

This error came because the $PATH in the environment variable or Linux needs to be set up manually. You can try to install the Maven or another package again and see if it solved the issue. But settings the path variable manually is more effective.

Use the following steps for your Linux system, we will be taking examples of Maven commands. Now, the maven commands are in “/opt/apache-maven-3.0.0/bin”.

Replace it with, dev-ubuntu-2104% export

PATH=$PATH:/opt/apache-maven-3.2.0/bin

Use the following steps for the Windows system, add the path of your package (maven) in the bin directory to the system variable path. For example, the system path variable for Apache software will be:

C:\Program Files (x86)\Apache Software Foundation\apache-maven-3.0.0\bin.

Check the path variable is set up correctly, in most cases it’s causing the issue. And it can save hours of reading discussion blogs and constant reinstallation.

Setup the command path in the .zshrc file to fix the “zsh: command not found” error

Now, if you checked that there’s no typo, the package is also installed and the path variable is set. Then the only other cause of this error is the terminal itself. Try exiting and re-login the terminal, you should try again. If you again get “zsh command not found”, then it means that  for .zshrc file the command path is not set up properly. In this case, you should set up the command path.

If the file does not exist, then we need to create a new “.zshrc” file, we can do this by the “touch ~/.zshrc” command. Now, exit and reopen the terminal and the file will execute and set up automatically. Follow these steps to edit the created file:

  1. On your macOS or Linux device, open the terminal with Commad + space for macOS, and Ctrl + Alt + T for Linux.
  2. Now check if you are on zsh shell or not, if not then change it. If you are on zsh shell, type “zsh” and hit enter.
  3. Once done, type “nano ~/.zshrc” and now add the $PATH variable details for your device like “export PATH=$PATH:/opt/apache-maven-3.0.0/bin/”.
  4. Now press “Ctrl + X” or “Control + X”, followed by “Y” and this will save the file. You can press enter to exit the nano editor.
  5. Once you have completed all the previous steps, then type “sourse ~/.zshrc” to commit all the changes.

Read Also: How to Fix Kmode Exception Not Handled Error

Summary

In the article, we saw why you may be getting the error and what are the most common sources of error. Along with it, we also saw some of the tried and tested solutions that will most definitely solve your issue. But if it’s still not solved then try checking StackOverflow or other discussion blogs which may contain solutions that work for you.

The best way to fix any error is to trace it back to its source and isolate the source. We would suggest you check if you have everything needed to run the command and if the command is correct. Sometimes, we can miss little things like typos and then scratch our heads all day.

But if you want help with any of your tech issues, then feel free to drop it down in the comment section below. We are more than happy to help out our readers.

Now, your issue should be solved and now you can sit back and get your work done. But if your issue is not solved, check the FAQ section since it can help you get a better idea. Or you can go thought discussion blogs or StackOverflow and see if anyone got an answer that works for you.

FAQ – zsh: command not found error

In this section, we will answer some of the commonly asked questions which are related to the zsh command not found error.

How do I fix the “zsh: command not found brew” error on macOS

Now, we need to check if brew is installed on your device along with the PATH variable set up. If you have already installed brew, then you can skip initial steps.
Now, follow the steps to fix the error:
Open the terminal on your macOS device and make sure that you are using the zsh prompt. If not then type “zsh” and hit enter.
Once done, type this command “/bin/bash -c “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)”, this will install brew.
Now, set the path variable by using the following command, this will be suggested by the installation wizard also. Type,
echo ‘eval $(/opt/homebrew/bin/brew shellenv)’ >> /Users/$USER/.zprofile
Once done, run the following command by hitting enter on your keyboard.
eval $(/opt/homebrew/bin/brew shellenv)
Now, run the brew help command to check if the installation was successful and validate it.
If you find that the brew which you installed is not working, use the “.zshrc” file and run the below mentioned command in it.
export PATH=/opt/homebrew/bin:$PATH
Now run this command to implement the changes.
% source ~/.zshrc

How to fix the “zsh command not found code” error in macOS?

Before we can start, make sure that you have already installed Visual Studio Code on your macOS device as per the instructions provided here. Now, if you are getting error when laughing code, follow these steps:
For bash shell users, copy and paste the command mentioned below in the terminal:
cat << EOF >> ~/.bash_profile
# Add Visual Studio Code (code)
export PATH=”\$PATH:/Applications/Visual Studio Code.app/Contents/Resources/app/bin” EOF
For macOS devices, make sure you are using zsh and enter the following command in the terminal:
cat << EOF >> ~/.zprofile
# Add Visual Studio Code (code)
export PATH=”\$PATH:/Applications/Visual Studio Code.app/Contents/Resources/app/bin” EOF

How do I solve the “zsh command not found docker” error on macOS?

If you are facing this issue, then it occurs if you do not follow the right instructions while docker installation. You need to install it again and follow all the steps mentioned on docker.com. Check the site once and validate all the installation steps you took.