📦

サヨナラanyenv。コンニチハasdf。

2023/09/27に公開

今更ながらMac買い換えた勢いで、anyenvを卒業してasdfに乗り換えてみたのでメモ。お仕事用のUbuntu(WSL)も一気に乗り換え。

asdfの良い点

  • 使い方が統一されてる。anyenvは*envの使い方をそれぞれ覚えないといけないが、毎回忘れてググるハメに…。
  • プラグインがいっぱいある(aws-sam-cliとか、kubectlとか)&その気になれば自分でも簡単に作れそう。
  • .tool-versionsに書いておけばasdf install一発で簡単に同じ構成を再現できる。チームで共有すると便利そう。

asdfのインストール

macOS (Ventura)

  • 公式ガイドには"We highly recommend using the official git method."と書いてあるがHomebrewで入れてみた。

  • 自分はzshユーザなので、公式サイトでいうZSH & Homebrewパターン。

  • 補完用の関数のパスとかがgitで入れた場合と違うので、Ubuntuとシェルの設定を共通にしたい場合はgitで入れるのが無難かもしれない。

    brew install coreutils curl git #依存物
    brew install asdf
    

Ubuntu (WSL)

  • Gitでインストール。タグは公式ガイドか、GitHubを見て最新版を使う。

    apt install curl git #依存物
    git clone https://github.com/asdf-vm/asdf.git ~/.asdf --branch v0.12.0
    

.zshrc

  • 頑張ればもうちょい綺麗にかけるかもしれないが、素直にmacOSとそれ以外で分岐。

    ## Load asdf
    case "${OSTYPE}" in
    darwin*)
        [ -f /opt/homebrew/opt/asdf/libexec/asdf.sh ] && source /opt/homebrew/opt/asdf/libexec/asdf.sh
        ;;
    *)
        [ -f ~/.asdf/asdf.sh ] && source ~/.asdf/asdf.sh
    esac
    
    ## Load asdf completions
    case "${OSTYPE}" in
    darwin*)
        [ -d /opt/homebrew/opt/asdf/share/zsh/site-functions ] && fpath=( /opt/homebrew/opt/asdf/share/zsh/site-functions $fpath )
        ;;
    *)
        [ -d ${ASDF_DIR}/completions ] && fpath=( ${ASDF_DIR}/completions $fpath )
    esac
    
    autoload -Uz compinit && compinit
    

Node.jsのインストール

  • Node.jsのプラグインは公式が提供している。

  • 公式ガイドにある通りpost-installフックがある場合があるので、プラグインの依存物を入れる。

  • ちなみに、先にプラグイン入れちゃったけど問題なく動いている模様。

    brew install gpg gawk # macOS
    apt install dirmngr gpg curl gawk # Ubuntu
    
  • nodejsプラグインの追加

    • git-urlを指定して追加するのが推奨らしいが、未指定だとasdf plugin list allで確認できるurlが使用される模様。

      % asdf plugin add nodejs
      ...
      # Recommendation
      Prefer the longer git-url method as it is independent of the short-name repo.
      ...
      
  • バージョンを指定してインストール

    • とりあえず最新版。stableとか指定できたら良いんだが…。

      asdf install nodejs latest
      
  • バージョン指定

    • Global

      asdf global nodejs latest # $HOME/.tool-versionsに記録される
      
    • Local

      asdf local nodejs latest # $PWD/.tool-versionsに記録される
      

Pythonのインストール

  • Pythonプラグインの追加

  • バージョンを指定してインストール

    asdf install python 3.11.5
    
  • バージョン指定

    asdf global python 3.11.5
    

Golangのインストール

  • Golangのプラグインは公式コミュニティが提供している。

  • 依存物のインストール。

    brew install coreutils # macOS
    apt install coreutils curl # Ubuntu
    
  • golangプラグインの追加

    asdf plugin add golang https://github.com/asdf-community/asdf-golang.git
    
  • インストール&バージョン指定

    asdf install golang latest
    asdf global golang latest
    

その他

  • 指定中のバージョンを確認

    asdf current
    
  • 利用可能なバージョンを表示

    asdf list all <plugin-name>
    
  • rehash

    asdf reshim <plugin-name>
    
  • asdfの全プラグインのアップデート

    asdf plugin update --all
    

Discussion