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 (5)

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!!

Collapse
 
danielkehoe profile image
Daniel Kehoe

For your future reference, I've written an in-depth article zsh: command not found: brew on my mac.install.guide website that explains the error and how to fix it.

Some things to note. First, git is installed automatically when you install Xcode Command Line Tools and the Xcode CLT version of git is fine for development use. You'll still want to install Homebrew for other command line software tools. Following Homebrew's onscreen instructions, there's a reminder to add Homebrew to your $PATH which you can either do manually by updating the ~/.zprofile file or following Homebrew's instructions. Yes, it's easy to overlook Homebrew's instructions to update the PATH.

The instructions you provide in your article here are only sufficient to update the PATH for a single Terminal session. Any changes made using the export command last only until you quit your Terminal. To make lasting changes to the PATH, you need to edit the ~/.zprofile or ~/.zshrc files. See my article about Mac PATH and Shell Configuration.

It's great to see you are helping yourself and other developers with problems setting up the development environment! Please reach out to me on LinkedIn or the mac.install.guide website if you want more guidance.

Collapse
 
ahmedfakharabbas profile image
Ahmed Fakhar Abbas

if you're hitting a wall with the 'brew command not found' error on your Mac, I just watched a great video that sorts it all out. Super easy to follow and gets Homebrew working smoothly. Definitely check it out if you’re stuck!
youtu.be/saRXmzo7v8c?si=O6-jL_YK0M...

Collapse
 
vansh_malik_5f6043f508808 profile image
Vansh Malik

It works. Thanks