9
6

Homebrewインストール後に「zsh: command not found: brew」でコマンドを受け付けなかった場合

Last updated at Posted at 2023-06-11

経緯

MacをM2に新調したためHomebrewをインストールした
OSはVentura 13.4
brewの公式サイトに沿って以下コマンドでインストール
使用シェルはzsh

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

インストール後に無事インストールできていたかの確認のためbrew -vを実行したところ以下エラー

zsh: command not found: brew

解決方法

以下コマンドを実行して解決

(echo; echo 'eval "$(/opt/homebrew/bin/brew shellenv)"') >> /Users/user/.zprofile
    eval "$(/opt/homebrew/bin/brew shellenv)"

userの箇所は自PCのユーザー名が入る

brew -vwhichどちらもコマンドが通るようになり無事パスが通った模様

Homebrew 4.0.21
/opt/homebrew/bin/brew

補足

brewインストール時に以下のwarningとNext stepsに解決方法が出ていた。それをそのまま実行しただけ

Warning: /opt/homebrew/bin is not in your PATH.
  Instructions on how to configure your shell for Homebrew
  can be found in the 'Next steps' section below.
==> Next steps:
- Run these two commands in your terminal to add Homebrew to your PATH:
    (echo; echo 'eval "$(/opt/homebrew/bin/brew shellenv)"') >> /Users/user/.zprofile
    eval "$(/opt/homebrew/bin/brew shellenv)"
- Run brew help to get started
- Further documentation:
    https://docs.brew.sh

後日症状再発とその解決方法

後日、nodebrewをインストールしようとbrewコマンドを実行したところまたcommand not foundとなってしまっていた
source ~/.zshrcを打った時があったのだが、それがきっかけっぽい
再度、先に挙げたコマンドを実行すればbrewコマンドが使用できるようになるがsourceコマンドを使用するたびこれになるのは困ると思った

解決方法

.zshrcにパスを追記して解決

vi ~/.zshrc

で.zshrc内に入り
以下追記

export PATH=/opt/homebrew/bin:$PATH

これでsourceコマンドを使用してもbrewコマンドが使用できるようになった
はじめからパスに追記するだけが一番の早道だったかもしれない

9
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
6