The "brew: command not found" error occurs because the Homebrew installation directory is not included in your system's PATH environment variable. To fix this, you must add the Homebrew binary folder to your shell configuration file (such as .bashrc or .zshrc) so the terminal knows where to look for the brew executable. If you haven't installed Homebrew yet, you must run the official installation script first; however, most users encounter this error immediately after a successful installation because the final configuration steps were skipped.
What You'll Need
- A Linux distribution (Ubuntu, Debian, Fedora, CentOS, etc.) with a terminal open.
- Sudo or root privileges to install dependencies.
- An active internet connection to download the Homebrew scripts and packages.
- Knowledge of which shell you are using (Bash and Zsh are the most common).
Step-by-Step
- Verify Installation: First, check if Homebrew is actually installed on your system. By default, Homebrew on Linux installs to
/home/linuxbrew/.linuxbrew. Run the commandls /home/linuxbrew/.linuxbrew/bin/brew. If the terminal returns the path to the file, Homebrew is installed, and you only have a PATH issue. If it says "No such file or directory," you need to install Homebrew first using the official script from brew.sh. - Identify Your Shell: You need to know which configuration file to edit. Run
echo $SHELL. If the output is/bin/bash, you will edit~/.bashrcor~/.bash_profile. If the output is/bin/zsh, you will edit~/.zshrc. - Add Brew to the PATH (Temporary Fix): To test if this is the only problem, run the following command in your current terminal session:
eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)". This command sets up the environment variables for the current session only. Now, try typingbrew help. If it works, you have confirmed that the installation is healthy and only the permanent PATH configuration is missing. - Make the PATH Permanent: To ensure the brew command works every time you open a new terminal, you must add the configuration to your shell profile. For Bash users, run:
echo "eval "\$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"" >> ~/.bashrc. For Zsh users, run:echo "eval "\$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"" >> ~/.zshrc. This appends the necessary initialization code to the end of your configuration file. - Apply the Changes: The changes to your configuration file do not take effect until the file is reloaded or a new terminal is opened. To apply the changes immediately without restarting, run
source ~/.bashrc(for Bash) orsource ~/.zshrc(for Zsh). - Install Build Essentials: Homebrew often requires a compiler to install packages from source. To avoid future "command not found" errors related to
gccormake, install the development tools. On Ubuntu/Debian, runsudo apt-get updatefollowed bysudo apt-get install build-essential. On Fedora/CentOS, runsudo dnf groupinstall "Development Tools". - Final Verification: Run
brew doctor. This is a built-in diagnostic tool that checks your Homebrew installation for common issues. It will tell you if there are any remaining PATH problems or missing dependencies that could cause errors during package installation.
Common Mistakes
One of the most frequent mistakes is forgetting to run the eval command after the installation script finishes. The Homebrew installer prints the exact commands needed to add brew to your PATH at the very end of the installation process, but many users close the terminal window before reading these instructions. This leads to the immediate "command not found" error upon the first attempt to use the tool.
Another common error is using sudo brew. Homebrew is explicitly designed to run without root privileges for security reasons. If you attempt to run brew commands with sudo, you may encounter permission errors or break the directory structure of your Homebrew installation. If you have already run brew as root and are seeing permission errors, you may need to change the ownership of the /home/linuxbrew directory back to your local user using sudo chown -R $(whoami) /home/linuxbrew.
Users also frequently confuse the shell configuration files. Adding the PATH to .bashrc while using Zsh will have no effect, and vice versa. Always verify your active shell with echo $SHELL before appending lines to a configuration file.
Tips
To keep your Homebrew installation running smoothly, make it a habit to run brew update regularly. This ensures that the formulae (package definitions) are current and that you are installing the latest stable versions of your software. If you find that your terminal is starting slowly, check your configuration file to ensure you haven't accidentally added the eval command multiple times.
If you are using a non-standard installation path (which is rare for Linuxbrew but possible), replace /home/linuxbrew/.linuxbrew in the commands above with the actual path where you installed the software. You can find this path by using the find / -name brew 2>/dev/null command, though this may take several minutes to scan your entire drive.
For those managing multiple Linux environments, consider using a version manager for your shell or a dotfiles repository. This allows you to sync your PATH configurations across different machines, ensuring that Homebrew is always available without manual setup on every new server or workstation.
Troubleshooting
If you have followed the steps above and still see "brew: command not found," first check for typos in your configuration file. Open the file with a text editor (e.g., nano ~/.bashrc) and ensure the line eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)" is present and contains no extra characters or missing quotes. Save the file and run source again.
If the /home/linuxbrew/.linuxbrew/bin/brew file does not exist, the installation likely failed. This often happens due to a lack of initial dependencies. Ensure you have curl and git installed on your system before running the installation script. Run sudo apt install curl git on Debian-based systems and try the installation script again. Pay close attention to any error messages during the installation process, especially those regarding "Permission denied" or "Connection timed out."
In cases where the PATH is correct but the command still fails, check if there is a conflict with another software package. Run which -a brew. If it returns multiple paths, your shell might be trying to execute a different, broken version of a tool named brew. You can resolve this by ensuring the Homebrew path is listed first in your PATH variable.
If you encounter "Permission denied" when running brew, it is usually because the installation directory is owned by root. Fix this by running sudo chown -R $(whoami):$(id -gn) /home/linuxbrew/.linuxbrew. This gives your current user full ownership of the Homebrew directory, allowing the manager to install and update packages without requiring sudo.
Frequently Asked Questions
Do I need to use sudo to run brew commands?
No, you should never use sudo with brew. Homebrew is designed to operate in your user home directory or a dedicated non-root directory to avoid compromising system security and stability.
Why is Homebrew installed in /home/linuxbrew instead of /usr/local?
On Linux, Homebrew uses /home/linuxbrew to avoid requiring root access for most operations and to prevent conflicts with the system's own package manager (like APT or DNF).
Can I use brew and apt at the same time?
Yes, you can use both. Apt manages system-level packages and core OS components, while Homebrew is excellent for installing newer versions of developer tools and utilities that may not be available in the official OS repositories.
What is the difference between brew install and brew bundle?
brew install is used to install a single package. brew bundle uses a "Brewfile" to install a whole list of packages at once, which is useful for setting up a new machine with all your preferred tools.
How do I completely remove Homebrew from my Linux system?
You can use the official uninstall script provided by the Homebrew team. After running the script, remember to open your .bashrc or .zshrc file and remove the eval line you added to clean up your PATH configuration.
Does Homebrew work on all Linux distributions?
Yes, Homebrew (Linuxbrew) is designed to be cross-distribution. As long as you have the basic build tools (gcc, make) and a compatible shell, it will work on almost any modern Linux distro.