Your terminal fails to recognize commands like
git or
python on a MacBook Pro (M2) because the shell cannot locate their executable files in the system directories. This happens when the
$PATH environment variable is incomplete, missing critical entries such as
/usr/local/bin or
/opt/homebrew/bin. By correcting this variable in your shell configuration file, you restore immediate access to all installed tools without needing to reinstall macOS or Homebrew.
Diagnosing the Missing Path
Before modifying any files, verify that the issue is indeed related to the path. Open Terminal and type
echo $PATH. On an M2 Mac running Apple Silicon, a healthy output should include
/opt/homebrew/bin alongside standard system paths like
/usr/bin and
/bin. If these directories are absent, your shell does not know where to look for command-line utilities.
You can also test specific commands. If typing
which git returns nothing or an error, the system definitely cannot find it. This diagnostic step confirms that the problem is environmental rather than a broken installation of the software itself. For more context on how macOS manages binary locations, you might want to read about
Understanding lsbin and contents bindirectory in macOS with Oh My ZSH and tmux, which explains the underlying directory structure.
Identifying Your Shell Configuration File
macOS uses
zsh as the default shell since Catalina. Therefore, your settings are stored in either
.zshrc or
.zprofile located in your home directory. The
.zshrc file is executed for interactive shells, while
.zprofile runs at login. Modifying
.zshrc is usually sufficient for most users, but if you use
tmux or other multiplexers, changes in
.zprofile may be necessary to ensure the path is set before the session starts.
To edit this file, use a text editor like
nano or
vim. Run
nano ~/.zshrc from your terminal. If the file does not exist, the command will create a new empty one. Be careful not to overwrite existing configurations unless you have backed them up. Always keep a copy of your original file in case something goes wrong during the editing process.
Adding the Correct Paths
Once inside the editor, append the missing directories to your
PATH variable. Add the following line at the end of the file:
export PATH="/opt/homebrew/bin:$PATH"
This line prepends the Homebrew directory to your existing path, ensuring that Homebrew-installed tools take precedence over system defaults if there are conflicts. If you are using Intel-based Macs or have dual-booted with Rosetta, you might also need to add
/usr/local/bin:
export PATH="/usr/local/bin:/opt/homebrew/bin:$PATH"
After adding the lines, save the file (
Ctrl+O then
Enter in
nano) and exit (
Ctrl+X). To apply the changes immediately without restarting your computer, run
source ~/.zshrc. This reloads the configuration, and your terminal should now recognize previously missing commands.
Verifying the Fix
Run
echo $PATH again to confirm that
/opt/homebrew/bin appears in the string. Try executing a command that was previously failing, such as
git --version or
python3 --version. If the command returns its version number instead of "command not found," the issue is resolved.
If problems persist, check for syntax errors in your
.zshrc file. A single typo in an export statement can prevent the entire file from loading correctly. You can test your configuration by running
zsh -l to start a login shell and observe any error messages printed to the screen. For additional troubleshooting on specific command failures, see
Fixing zsh: command not found: ssh -Y on Mac Terminal.
Frequently Asked Questions
Why did my terminal stop working after a macOS update?
Updates sometimes reset shell configurations or change default permissions. Always back up your
.zshrc file before updating macOS to preserve your custom paths. If the update wiped your configuration, restoring from a backup or re-adding the export lines will fix the issue.
Should I edit .bash_profile instead of .zshrc?
Only if you explicitly switched your default shell back to Bash. Most modern MacBooks use Zsh by default, so editing
.bash_profile will have no effect unless you manually switch shells using
chsh -s /bin/bash. Check your current shell with
echo $SHELL to be sure.
What if I get a 'permission denied' error when saving .zshrc?
This occurs if you do not have write access to the home directory. Use
sudo nano ~/.zshrc to gain temporary root privileges, but be cautious as this can lead to accidental system modifications. It is safer to ensure your user account owns the file by checking ownership with
ls -la ~/.zshrc.
Can I remove old paths from the PATH variable?
Yes, but only if you are certain those programs are no longer needed. Removing valid paths can break essential system tools. It is better to leave unused paths in place; they rarely cause performance issues unless the directory contains thousands of scripts.