Homebrew is a popular package manager for UNIX-based operating systems like MacOS and Linux. Homebrew users find the most use out of it from a MacOS device, as by default, MacOS does not have a terminal-based package manager in and of itself. Nevertheless, homebrew is also quite likable when used from a Linux machine due to its simplicity, and not to mention, the subcommands it has to offer.
However, it seems to be that in certain cases, an error is outputted to the users when trying to run some commands in homebrew for things like updating the repository or installing some program. This is what the error message says:
$ Error: Git must be installed and in your PATH!
My task in this article would be to help you fix this error message, and get your way out to using Homebrew in its fullest!
Why am I getting the error message?
The exact problem causing this error message is not just for a single scenario. There are different reasons as to why it might happen. However, what you need to know is that Homebrew, by itself, uses git in order to execute certain commands. This is exactly why some users, despite not needing to use git for themselves, are having to face the issue as it is not found by homebrew during a command execution, such as when installing a given program. Other possibilities include the absence of git’s executable directory in the PATH variable of your system.
The following sections will discuss the various different methods you could try out to fix the issue. You may be required to follow more than one method in order to fully fix the error.
Using brew doctor to determine the issue
Before you even start trying any of the methods mentioned here, it’d be a good idea to run tune brew doctor command to see the current status of Homebrew:
$ brew doctor
If everything looks and sounds good, then you’ll get the following output:
Your system is ready to brew.
If, however, there’s a particular task you need to carry out, then the doctor will take you to fix that by giving you instructions to follow. If you’re unable to run even the brew doctor command, it’s time to go straight to the operating theatre.
Fixing the PATH Variable
If you’re using Linux, you need to ensure that two main directories are included in your PATH variable. They are ‘/usr/bin’ and ‘/usr/local/bin’. In order to achieve this, first open up the .bashrc file or the .zshrc file located in your home directory (hidden), based on the terminal shell you’re using, with your text editor of choice. I’d like to use nano:
$ nano ~/.bashrc
$ nano ~/.zshrc
Inside this file, append the following line of code:
Save and exit the file. Now restart the terminal for changes to take effect. If everything went well, you should be able to use homebrew without any error again.
Before you can go in to fix the PATH variable in MacOS, you first need to figure out the directory where homebrew has been installed. Under the homebrew installation directory, there’s a subdirectory called bin which contains the git executable file. In a MacOS system that does not use the M1 Series Chip, the installation directory for homebrew is /usr/local/bin, and so is the case for the git installed with Homebrew.
However, this directory changes to “/opt/homebrew/bin”, when using an M1 based MacOS device. So as you’d have already guessed, the problem really takes place with an M1 MacOS system. therefore, you need to append this directory under the PATH variable. Similar to that of Linux, append the following line of code in the .zshrc file or the .bashrc file placed under your home directory:
Save and exit the file, and try running brew again from your terminal:
$ brew update
Using git pull in Homebrew directory
A few users in Stackoverflow have reported that pulling the latest version of Homebrew from a git repository by using git pull would help fix the issue in certain cases. According to the answer posted by Lucian Irimie, you first need to enter the installation directory of Homebrew by running:
$ cd /opt/homebrew
and inside this directory, you need to run the following commands in order,
$ git pull origin master
$ brew update && brew upgrade
Install command line tools in MacOS
For a number of users who had reported online about their issue regarding the ‘Git must be installed and in your PATH!’ error, a very common fix is to install the xcode command line tools in MacOS. This particular fix is also suggested by the ‘brew doctor’ command if the problem exists in your system and if you were able to run that command successfully.
For now, run the following command in your terminal to install the command line tools:
$ xcode-select --install
If you haven’t been able to fix the problem by now, then I’d suggest you rather stop trying and start counting the number of times I used the word ‘if’ in the entire article (don’t cheat, okay?)