How to Add Homebrew to PATH on macOS

Step-by-Step Guide to Add Homebrew to PATH
Step 1: Verify Homebrew Installation
First, check if Homebrew is installed correctly by running:
1 | brew --version |
If you see the version number, Homebrew is installed. If you get a “command not found” error, you may need to reinstall Homebrew.
Step 2: Determine Your Shell
Check which shell you are using by running:
1 | echo $SHELL |
Common shells include /bin/zsh
(default on newer macOS versions) and /bin/bash
.
Step 3: Update Shell Configuration File
Depending on your shell, you need to update the appropriate configuration file:
For Zsh (default on macOS Catalina and later):
- Open your
~/.zshrc
file in a text editor:1
nano ~/.zshrc
- Add the following lines to the file:
1
2
3eval "$(/opt/homebrew/bin/brew shellenv)"
export PATH="/opt/homebrew/bin:$PATH"
export PATH="/opt/homebrew/sbin:$PATH" - Save the file and reload the configuration:
1
source ~/.zshrc
- Open your
For Bash:
- Open your
~/.bash_profile
or~/.bashrc
file:1
nano ~/.bash_profile
- Add the following lines:
1
export PATH="/usr/local/bin:$PATH"
- Save the file and reload the configuration:
1
source ~/.bash_profile
- Open your
Step 4: Verify the PATH Update
After updating the configuration file, verify that Homebrew is accessible by running:
1 | brew --version |
You should see the version number of Homebrew. If not, double-check the paths and ensure you sourced the correct file.
Step 5: Run brew doctor
To ensure everything is set up correctly, run:
1 | brew doctor |
This command will check for common issues and provide suggestions for resolving them.
Additional Tips
Check Homebrew Installation Directory: If you installed Homebrew in a custom directory (e.g.,
/opt/homebrew
for M1 Macs), ensure you use the correct path in your configuration file.Use
brew shellenv
: For a more robust setup, use thebrew shellenv
command to dynamically set the PATH. This ensures that your PATH is always up-to-date with Homebrew’s configuration.Troubleshooting: If you still encounter issues, try running the Homebrew installation script again or manually add the paths to your configuration file.
Conclusion
Adding Homebrew to your system’s PATH
is a crucial step to ensure seamless access to the package manager. By following these steps, you can easily integrate Homebrew into your command-line workflow and avoid common issues like “command not found” errors. Happy brewing!