asdfのインストールメモなど

asdf

  • asdfというツールがあることを最近しって、それのメモ(terraform使いたかった)
  • anyenvみたいにツールのバージョンをasdfを使って自由に切り替えることができる

github.com

  • 非常に多くのツールがasdfで公開されており、今回の目的のterraformもある

install

公式のドキュメントの通りに実施

asdf-vm.com

今回はWSL上にインストールだったので、gitからcloneして配置

git clone https://github.com/asdf-vm/asdf.git ~/.asdf --branch v0.8.1

zsh plugin

zshを使っているので、それのプラグインもインストール

github.com

plugins=(asdf)

これを記載するだけ。ちなみに複数のplugin記載する場合には

plugins=(git asdf)

こんな感じでスペース区切りで書く模様

サポートしているソフトのリスト表示

$ asdf plugin list all
1password-cli                 https://github.com/NeoHsu/asdf-1password-cli.git
R                             https://github.com/asdf-community/asdf-r.git
act                           https://github.com/grimoh/asdf-act.git
action-validator              https://github.com/mpalmer/action-validator.git
adr-tools                     https://gitlab.com/td7x/asdf/adr-tools.git
ag                            https://github.com/koketani/asdf-ag.git
age                           https://github.com/threkk/asdf-age

terraformのインストール

これも公式のドキュメントを参照

github.com

asdf plugin-add terraform https://github.com/asdf-community/asdf-hashicorp.git

以下のコマンドで、インストール可能なterraformのバージョンが出てくる

# どっちでもOK
$ asdf list-all terraform 
$ asdf list all terraform

今回は1.0.10を選択したので、

$ asdf install terraform 1.0.10

# latestという指定も可能
$ asdf install terraform latest

terraform version確認(必要に応じてSHELL再起動など)するが、このままだと怒られる
globalでインストール、もしくはlocalでバージョン指定しないと怒られるらしい。
~/.tool-versionsのファイルにバージョンの記載がないとだめのよう

このようなエラーが出る

$ terraform --version
No version set for command terraform
Consider adding one of the following versions in your config file at 
terraform 1.0.10

asdf-vm.com

解決方法

globalでバージョンを指定する

$ asdf global terraform 1.0.10

上記コマンドを入力することにより、~/.tool-versionsにバージョンが記載される

$ terraform --version
Terraform v1.0.10
on linux_amd64

version切り替え

terraform 1.0.9に入れ替えてみる

まずはインストール

$ asdf install terraform 1.0.9

切り替え(localでの切り替え)

$ asdf current terraform 
terraform       1.0.10          /home/yasuhiro/.tool-versions
$ asdf local terraform 1.0.9
$ terraform --version
Terraform v1.0.9
on linux_amd64

このasdf local terraform 1.0.9のコマンドによりローカルフォルダに.tool-versionsというファイルが作られる

terraform 1.0.9

このようにしてバージョン管理されている。
これは.gitignoreせずにチーム内で共有してもいいのかも?チーム内でasdf入れるなら、みんなで勝手にバージョンが合うのではないか

バージョンの不一致の確認

かりに.tool-versionsにterraform 1.0.8が登録されており、それがインストールされていない場合、ちゃんとエラー出してくれる

.tool-versionsの内容をいかに変更(わざと入っていない1.0.8を指定してみる)

terraform 1.0.8

然るべきバージョンが見つからないのでエラーが出る

$ terraform --version
No preset version installed for command terraform
Please install a version by running one of the following:

asdf install terraform 1.0.8

or add one of the following versions in your config file at /home/yasuhiro/work/projects/others/ecs-test/.tool-versions
terraform 1.0.10
terraform 1.0.9