[Solved] zsh command not found brew in macOS

[Solved] zsh command not found brew in macOS

In this tutorial, we will learn about “How to fix the error: zsh command not found brew in macOS “. In the macOS environment, users often rely on the package manager brew (Homebrew) to install, uninstall, and manage software packages. Homebrew simplifies the process of managing software on macOS, making it an essential tool for developers and power users. However, encountering the error message zsh: command not found:brew can be a stumbling block, preventing users from executing Homebrew commands in the Z shell (zsh), which is the default shell in macOS starting from Catalina (macOS 10.15).

 

Why the error occurred?

Also Read: [Solved] This error may indicate that the docker daemon is not running

There can be different reason because of which this issue is seen in macOS operating system. Few typically reasons for the arise error are below:

  1. Homebrew Not Installed: The most straightforward reason is that Homebrew might not be installed on your system. If Homebrew has never been installed, its commands won’t be recognized in the terminal.
  2. Incorrect PATH Configuration: The PATH environment variable tells the shell where to look for executable files (commands). If Homebrew is installed but its installation path is not included in your shell’s PATH, the brew command will not be found. Homebrew usually installs its binary files in /usr/local/bin or /opt/homebrew/bin, depending on the system architecture. If these directories are not in your PATH, the shell cannot locate the brew command.
  3. Shell Configuration File Not Sourced: When you install Homebrew, it modifies your shell’s configuration files (e.g., .zshrc for Zsh) to add its binary path to your PATH. If these configuration files are not sourced properly after installation or have been modified incorrectly, it can lead to the shell not recognizing the brew command.
  4. Incomplete or Corrupted Homebrew Installation: In some cases, an incomplete or corrupted Homebrew installation can cause this error. This might happen due to interrupted Homebrew installation processes, disk space issues, or unexpected system shutdowns during the installation.

Understanding the root cause is crucial for troubleshooting this error effectively and restoring access to Homebrew’s powerful package management capabilities on macOS.

 

[Solved] zsh command not found brew in macOS

Now that we have understood the possible root cause of the error, next we will discussion few solutions which helps to fix the error. I have provided two solutions which had worked for me to fix the given error. Let us discuss each solution one by one below.

Solution-1: Install Homebrew

If your MacBook is new, it is possible that by default Homebrew is not installed. In this case, when you try to install any package (say git in this case), you will witness the error as shown below.

[email protected] ~ % brew install git
zsh: command not found: brew

 

Next, let us install the brew using below command.

[email protected] ~ % /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

 

This will install the brew in your macOS machine and you will be now able to install any package using brew package manager. To verify the installation, execute the command brew --help. This command will show you the man page of brew package manager.

[email protected] ~ % brew --help 

 

Solution-2: Set brew in $PATH

If the first solution does not work for you, try this solution. Sometime, even after installing the brew, you get the same error as shown below.

[email protected] ~ % brew install git
zsh: command not found: brew

 

This is because, you have installed the brew but you have not set the brew binary path location in the system variable called PATH. If you switch to the path where brew binary is placed in your machine, you will be able to install any package by executing the binary along with the package you want to install. For example, by default Homebrew binary is placed at path /opt/homebrew/bin. Switch to this binary path as shown below.

[email protected] ~ % cd /opt/homebrew/bin

 

Next, list down all the files and directories in the current folder using command ls as shown below. You will find the Homebrew binary here as shown below.

[email protected] bin % ls
brew

 

Next, execute the binary in the below given format. You will now be able to see the man page of the Homebrew utility and also can install any package from this path.

[email protected] bin % ./brew
Example usage:
  brew search TEXT|/REGEX/
  brew info [FORMULA|CASK...]
......................................................
......................................................
Further help:
  brew commands
  brew help [COMMAND]
  man brew
  https://docs.brew.sh

 

But this is not the feasible solution as every time you want to install a new package, you first have to switch to the brew binary path and then install the Package. To avoid this Challenge, we will set the brew binary path in the system environment variable known as PATH. To set the binary path in PATH system variable, execute below two commands.

First, switch back to home directory using the command cd - as shown below.

[email protected] bin % cd -
[email protected] ~ % (echo; echo 'eval "$(/opt/homebrew/bin/brew shellenv)"') >> /Users/coder/.zprofile
[email protected] ~ % eval "$(/opt/homebrew/bin/brew shellenv)"

 

Next, verify the brew binary path in the system variable PATH by checking if the binary path is appended in the PATH variable value using below command.

[email protected] ~ % echo $PATH | grep -i brew
/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/usr/local/go/bin

 

If everything so far worked as expected, you can now run the brew utility from any path. Let us now execute the command brew --help from the home directory as shown below. You will see the man page of the Homebrew utility is returned in the command output.

[email protected] ~ % brew --help
Example usage:
  brew search TEXT|/REGEX/
  brew info [FORMULA|CASK...]
  brew install FORMULA|CASK...
  brew update
  brew upgrade [FORMULA|CASK...]
..................................................................
Further help:
  brew commands
  brew help [COMMAND]
  man brew
  https://docs.brew.sh

 

Let us now install git to verify if brew utility is able to install any package in the macOS machine. To do so, execute the command brew install git as shown below.

[email protected] ~ % brew install git
==> Downloading https://ghcr.io/v2/homebrew/core/git/manifests/2.44.0
................................................................................................................................................
................................................................................................................................................
==> Installing git
.....................................................................................................................................
==> git
The Tcl/Tk GUIs (e.g. gitk, git-gui) are now in the `git-gui` formula.
Subversion interoperability (git-svn) is now in the `git-svn` formula.
zsh completions and functions have been installed to:
  /opt/homebrew/share/zsh/site-functions

 

NOTE:

In macOS machines, git bash is not installed by default unlike Windows machine where got bash gets installed when git is installed. This is because macOS has native bash installed which supports all the git native commands.

 

Next, verify if the git is installed successfully by checking the installed git version using below command. If the output returns the git version, it means git is installed successfully in your system.

[email protected] ~ % git version
git version 2.39.3 (Apple Git-145)

 

Summary

We have successfully fixed the error using one of the given solution in this tutorial. We also installed git using brew utility to verify if brew utility works globally. To learn more about Homebrew, refer to the official Homebrew Documentation page.

 

Leave a Comment