M2Macの環境構築はHomebrewとasdfで
Homebrewとasdfとは
Macを使い始めるとさまざまなソフトウェアをインストールすることになります。Homebrewを使うとソフトウェアのインストールと管理を簡単に行うことができます。
asdfは、プログラミング言語やgitなどのツールを一括でバージョン管理できるコマンドラインツールです。異なるプロジェクトに対してそれぞれ異なるバージョンのプログラミング言語やツールを簡単に管理することができます。
Homebrewのインストール
Macのターミナルで以下のコマンドを実行してください。以下のコードはHomebrewの公式サイトからも取得することができます。
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
インストールが終わると、次のような画面が表示されます。
==> Next steps:
- Run these two commands in your terminal to add Homebrew to your PATH:
(echo; echo 'eval "$(/opt/homebrew/bin/brew shellenv)"') >> /Users/home/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"
- Run brew help to get started
- Further documentation:
https://docs.brew.sh
上記の指示にしたがって、パスの設定を行います。以下のコマンドを順番に実行してください。ただしhome
は自分のホームディレクトリの名前です。実際の画面ではあなたのホームディレクトリ名が表示されているはずです。
(echo; echo 'eval "$(/opt/homebrew/bin/brew shellenv)"') >> /Users/home/.zprofile
source ~/.zprofile
最後に、brew --version
と入力して、Homebrew 4.2.17
のように表示されていれば、正しくインストールすことができています。
asdfのインストール
asdfの公式ドキュメントにしたがってインストールしていきます。ここでは、Homebrewを利用したインストール方法を紹介します。
1.依存関係のインストール
以下のコマンドを実行してください。
brew install coreutils curl git
処理が終わると、一部省略していますが以下のような文章が表示されます。
==> coreutils
Commands also provided by macOS and the commands dir, dircolors, vdir have been installed with the prefix "g".
If you need to use these commands with their normal names, you can add a "gnubin" directory to your PATH with:
PATH="/opt/homebrew/opt/coreutils/libexec/gnubin:$PATH"
==> curl
curl is keg-only, which means it was not symlinked into /opt/homebrew,
because macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.
If you need to have curl first in your PATH, run:
echo 'export PATH="/opt/homebrew/opt/curl/bin:$PATH"' >> ~/.zshrc
For compilers to find curl you may need to set:
export LDFLAGS="-L/opt/homebrew/opt/curl/lib"
export CPPFLAGS="-I/opt/homebrew/opt/curl/include"
指示にしたがって、.zshrc
ファイルに環境変数を追加していきます。
echo 'export PATH="/opt/homebrew/opt/coreutils/libexec/gnubin:$PATH"' >> ~/.zshrc
echo 'export PATH="/opt/homebrew/opt/curl/bin:$PATH"' >> ~/.zshrc
echo 'export LDFLAGS="-L/opt/homebrew/opt/curl/lib"' >> ~/.zshrc
echo 'export CPPFLAGS="-I/opt/homebrew/opt/curl/include"' >> ~/.zshrc
source ~/.zshrc
2.asdfのダウンロード
以下のコマンドを実行します。1行目はasdfのダウンロード、2行目は環境変数の設定です。
brew install asdf
echo -e "\n. $(brew --prefix asdf)/libexec/asdf.sh" >> ${ZDOTDIR:-~}/.zshrc
source ~/.zshrc
asdfによるプログラミング言語のバージョン管理
3.pluginのインストール
asdfはプラグインで管理をします。インストール可能なプラグインの一覧は、以下のコマンドで確認できます。
asdf plugin list all
たとえば、Pythonをインストールする場合
asdf plugin add python {url}
とします。
4.特定のバージョンのインストール
Pythonの場合、インストール可能なバージョンの一覧は以下のコマンドで確認できます。
asdf list all python
バージョンを選び、それを指定してインストールします。
asdf install python {version}
5.バージョンのセット
インストールしたバージョンをグローバルに設定したい場合は、
asdf global python {version}
ローカルに設定したい場合には、設置したいディレクトリに移動して
asdf local python {version}
とします。
確認
asdf list
と入力すると、以下のように現在インストールされている言語とそのバージョンが表示されます。
python
3.10.14
Discussion