気ままに気ままのエンジニアブログ

定期的に得た知見を気ままに発信中

【Rails/環境構築(M1)】HomebrewインストールでInstallation successful!になったのにbrewコマンドがエラーになるときの対処方法

こんにちは。

最近徐々に天敵を感じるようになってきました。そう花粉です。

どうもハチマキです。

はじめに

最近MacBook Air(M1,2020)を購入したため、開発環境の構築を行いました。その時に発生したエラーの対処方法について書いていきます。

環境

やりたいこと

Homebrewをインストールして、brewコマンドを使えるようにしたい

問題

Homebrewからインストールコマンドをコピペして、ターミナルで実行しました。
Installation successful!と無事にインストールできたと思いきや、brewコマンドを実行するとzsh: command not found: brewとコマンドが見つからない。。

% /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Password:
==> This script will install:
/opt/homebrew/bin/brew
/opt/homebrew/share/doc/homebrew
/opt/homebrew/share/man/man1/brew.1
/opt/homebrew/share/zsh/site-functions/_brew
/opt/homebrew/etc/bash_completion.d/brew
/opt/homebrew

Press RETURN to continue or any other key to abort
==> /usr/bin/sudo /usr/sbin/chown -R user-name:admin /opt/homebrew
==> Downloading and installing Homebrew...
HEAD is now at 0d1aa33f8 Merge pull request #10525 from samford/livecheck-check-stable-before-head
Warning: /opt/homebrew/bin is not in your PATH.
==> Installation successful!

==> Homebrew has enabled anonymous aggregate formulae and cask analytics.
Read the analytics documentation (and how to opt-out) here:
  https://docs.brew.sh/Analytics
No analytics data has been sent yet (or will be during this `install` run).

==> Homebrew is run entirely by unpaid volunteers. Please consider donating:
  https://github.com/Homebrew/brew#donations

==> Next steps:
- Add Homebrew to your PATH in /Users/user-name/.zprofile:
    echo 'eval $(/opt/homebrew/bin/brew shellenv)' >> /Users/user-name/.zprofile
    eval $(/opt/homebrew/bin/brew shellenv)
- Run `brew help` to get started
- Further documentation: 
    https://docs.brew.sh

これで簡単にインストール完了!と思ったのもつかの間、% brew -vを実行しても下記のようにbrewコマンドが見つからない。。
今インストールしたのに?と思いながら調査を進めました。

% brew -v
 zsh: command not found: brew

原因

単純にPATHの通し忘れでした。
インストールコマンドを実行した際に、下記のようにNext steps:の記載がありましたね。。
ちゃんと読んでおけば解決できた内容でした、、

==> Next steps:

  • Add Homebrew to your PATH in /Users/user-name/.zprofile:

解決方法

下記を実行することで無事に解消されました。

% echo 'eval $(/opt/homebrew/bin/brew shellenv)' >> /Users/user-name/.zprofile 
% eval $(/opt/homebrew/bin/brew shellenv)

% brew helpなどを実行して、インストールが無事にできていることを確認してみましょう。

% brew help
Example usage:
  brew search [TEXT|/REGEX/]
  brew info [FORMULA...]
  brew install FORMULA...
  brew update
  brew upgrade [FORMULA...]
  brew uninstall FORMULA...
  brew list [FORMULA...]

Troubleshooting:
  brew config
  brew doctor
  brew install --verbose --debug FORMULA

Contributing:
  brew create [URL [--no-fetch]]
  brew edit [FORMULA...]

Further help:
  brew commands
  brew help [COMMAND]
  man brew
  https://docs.brew.sh