MacにTerraformとtfenvをインストールする

Terraformのインストール

brew installするだけ

$brew install terraform

# インストールできたらバージョンを確認
$ terraform --version
Terraform v0.12.25

tfenvのインストール

tfenvはTerraformのバージョンマネージャ。
tfenvを使うと、Terraformのバージョンを簡単に切り替えることができるようになる。

これもbrew installするだけ、、だと思ったらエラーになった。

$ brew install tfenv
Updating Homebrew...
Error: Cannot install tfenv because conflicting formulae are installed.
  terraform: because tfenv symlinks terraform binaries

Please `brew unlink terraform` before continuing.

Unlinking removes a formula's symlinks from /usr/local. You can
link the formula again after the install finishes. You can --force this
install, but the build may fail or cause obscure side effects in the
resulting software.

すでにシンボリックシンクが貼られているとダメみたいなので、先にunlinkする

$ brew unlink terraform
Unlinking /usr/local/Cellar/terraform/0.12.25... 1 symlinks removed

$ brew install tfenv
Updating Homebrew...
==> Downloading https://github.com/tfutils/tfenv/archive/v1.0.2.tar.gz
==> Downloading from https://codeload.github.com/tfutils/tfenv/tar.gz/v1.0.2
######################################################################## 100.0%
🍺  /usr/local/Cellar/tfenv/1.0.2: 20 files, 29.2KB, built in 4 seconds

# バージョン確認
$ tfenv --version
tfenv 1.0.2

インストールできた!

tfenvの使い方

インストールされているバージョンの確認

$ tfenv list-remote
0.12.25
0.12.24
0.12.23
…

特定バージョンのインストール

$ tfenv install 0.12.25
[INFO] Installing Terraform v0.12.25
[INFO] Downloading release tarball from https://releases.hashicorp.com/terraform/0.12.25/terraform_0.12.25_darwin_amd64.zip
######################################################################## 100.0%
[INFO] Downloading SHA hash file from https://releases.hashicorp.com/terraform/0.12.25/terraform_0.12.25_SHA256SUMS
tfenv: tfenv-install: [WARN] No keybase install found, skipping OpenPGP signature verification
Archive:  tfenv_download.qmW5u9/terraform_0.12.25_darwin_amd64.zip
  inflating: /usr/local/Cellar/tfenv/1.0.2/versions/0.12.25/terraform
[INFO] Installation of terraform v0.12.25 successful
[INFO] Switching to v0.12.25
[INFO] Switching completed

インストール済みのバージョン確認

$ tfenv list
* 0.12.25 (set by /usr/local/Cellar/tfenv/1.0.2/version)

Terraformのバージョン切り替え

$ tfenv list
* 0.12.25 (set by /usr/local/Cellar/tfenv/1.0.2/version)
   0.12.24

$ tfenv use 0.12.24
[INFO] Switching to v0.12.24
[INFO] Switching completed

$ tfenv list
  0.12.25
* 0.12.24 (set by /usr/local/Cellar/tfenv/1.0.2/version)

$ terraform --version
Terraform v0.12.24

Your version of Terraform is out of date! The latest version
is 0.12.25. You can update by downloading from https://www.terraform.io/downloads.html

めっちゃ便利!