DEV Community

Cover image for zsh: command not found: brew
Dorcas Adjeley Laryea
Dorcas Adjeley Laryea

Posted on

zsh: command not found: brew

My first time setting up git on my Macbook was a few years ago. My second time was this week, and I faced the same error I faced the first time, except I didn't take note of how I fixed it. So I am writing this short article, hoping that the next time I face this issue again, I know where to find a possible solution.

So basically, I'm writing this for me. However, if anyone else faces this error and this helps them too, then that is a bonus.


Setting up git on a new laptop

1. Go to the git download page here.

Download the appropriate version according to your device.

Since I am using a Macbook, I download the version for Mac.

To download, first install homebrew by running in your
terminal:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Enter fullscreen mode Exit fullscreen mode

2. According to the documentation, after installing homebrew, in your terminal, run:

brew install git
Enter fullscreen mode Exit fullscreen mode

Except, once I run this, it throws an error:
zsh: command not found: brew

However, running git --version confirmed I had git installed:

git --version
Enter fullscreen mode Exit fullscreen mode

git version 2.39.3 (Apple Git-146)

It became clear that homebrew was not added to the PATH variable during installation, therefore shell could not locate the homebrew executable.

To rectify this, add homebrew to the PATH variable by doing the following:

  • In your terminal, run:
export PATH="/opt/homebrew/bin:$PATH"
Enter fullscreen mode Exit fullscreen mode

Hit the Enter key.

  • Run:
echo $PATH
Enter fullscreen mode Exit fullscreen mode

in your terminal to confirm if homebrew was successfully added to the PATH variable.

This returns a list of all the executables found on your laptop. Confirm that homebrew is present in the list. If it is, your terminal will now be able to locate homebrew.

3. Now, in your terminal, run brew install git again and hit Enter

brew install git
Enter fullscreen mode Exit fullscreen mode

Wait patiently for the download to complete.

You have successfully installed git on your laptop. You can now continue on to set up git in VS Code. Check out this documentation on how to do that.

Top comments (2)

Collapse
 
moopet profile image
Ben Sinclair

The fact that you saw git version 2.39.3 (Apple Git-146) in response to your command tells you that git was already installed. You didn't need to install another version with homebrew.

The issue where brew wasn't found in your path was likely because the installer added that line to your shell's (in this case zsh) startup files, but doesn't run them for you. If you exit the shell and start a new one then it would run.

In short: restart your terminal after installing homebrew, and don't install git with homebrew unless you have a specific requirement to do so.

Collapse
 
naalaryea profile image
Dorcas Adjeley Laryea

Yes, that is true, git was already installed it turned out, except that restarting my terminal did not rectify the issue, so I went through the whole process of installing homebrew myself and then set the path.

I will try this suggestion on an old macbook and hopefully it's easier there. Thank you so much for this insight!!