🎉
【Terraform】Ubuntuにインストールしよう
IaC(Infrastructure as Code)のツールとして人気のあるTerraform
今回は、そんなTerraform を Ubuntu にインストールしてみたいと思います
インストール方法
公式 HPより以下3つのコマンドを実行します
$ wget -O- https://apt.releases.hashicorp.com/gpg | gpg --dearmor | sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg
$ echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
$ sudo apt update && sudo apt install terraform
実行してみる
// インストール前なのでコマンドがみつからない
$ terraform --version
terraform: command not found
// インストールしていく
$ wget -O- https://apt.releases.hashicorp.com/gpg | gpg --dearmor | sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg
--2023-03-26 23:41:29-- https://apt.releases.hashicorp.com/gpg
Resolving apt.releases.hashicorp.com (apt.releases.hashicorp.com)... [sudo] password for ******: ************************************, ...
Connecting to apt.releases.hashicorp.com (apt.releases.hashicorp.com)|******************************|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 3980 (3.9K) [binary/octet-stream]
Saving to: ‘STDOUT’
- 100%[========================================>] 3.89K --.-KB/s in 0s
2023-03-26 23:41:29 (67.7 MB/s) - written to stdout [3980/3980]
// ここで止まったら、 sudo コマンドに対するパスワード入力待ち
// パスワードを入力すると進む
$ echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com jammy main
$ sudo apt update && sudo apt install terraform
Get:1 https://apt.releases.hashicorp.com jammy InRelease [12.9 kB]
Get:2 https://apt.releases.hashicorp.com jammy/main amd64 Packages [80.5 kB]
Hit:3 http://archive.ubuntu.com/ubuntu jammy InRelease
Get:4 http://security.ubuntu.com/ubuntu jammy-security InRelease [110 kB]
Get:5 http://archive.ubuntu.com/ubuntu jammy-updates InRelease [119 kB]
Get:6 http://archive.ubuntu.com/ubuntu jammy-backports InRelease [107 kB]
Get:7 http://security.ubuntu.com/ubuntu jammy-security/main amd64 Packages [693 kB]
Get:8 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 Packages [949 kB]
...(略)
// インストールしたので、バージョンが表示される
$ terraform --version
Terraform v1.4.2
on linux_amd64
バージョン管理ソフトも入れておく
Terraform の バージョン管理として、tfenvをインストールしておきます
// gitからソースを持ってくる
$ git clone https://github.com/tfutils/tfenv.git ~/.tfenv
Cloning into '/home/ksugioka/.tfenv'...
remote: Enumerating objects: 1959, done.
remote: Counting objects: 100% (564/564), done.
remote: Compressing objects: 100% (147/147), done.
remote: Total 1959 (delta 464), reused 444 (delta 413), pack-reused 1395
Receiving objects: 100% (1959/1959), 414.93 KiB | 8.14 MiB/s, done.
Resolving deltas: 100% (1268/1268), done.
// PATHにtfenvのソースのパスをたす
$ echo 'export PATH="$HOME/.tfenv/bin:$PATH"' >> ~/.bash_profile
// .bash_profileの再読み込み
$ source ~/.bash_profile
// tfenvコマンドが使えるか確認
$ tfenv --version
tfenv 3.0.0-18-g1ccfddb
tfenv の基本的な使い方
コマンド | 用途 |
---|---|
tfenv list-remote | インストール可能なTerraformのバージョンを確認 |
tfenv list | インストール済みのTerraformのバージョンを確認 |
tfenv install | バージョンを指定してTerraformをインストール |
tfenv use | インストール済みのバージョンを使用 |
試しに1.4.0 をインストールして使えるようにします
// インストールできるか確認
$ tfenv list-remote | grep 1.4.0
1.4.0
1.4.0-rc1
1.4.0-beta2
1.4.0-beta1
1.4.0-alpha20221207
1.4.0-alpha20221109
// インストールする
$ tfenv install 1.4.0
Installing Terraform v1.4.0
Downloading release tarball from https://releases.hashicorp.com/terraform/1.4.0/terraform_1.4.0_linux_amd64.zip
##################################################################################################### 100.0%
Downloading SHA hash file from https://releases.hashicorp.com/terraform/1.4.0/terraform_1.4.0_SHA256SUMS
Not instructed to use Local PGP (/home/*****/.tfenv/use-{gpgv,gnupg}) & No keybase install found, skipping OpenPGP signature verification
/home/*****/.tfenv/libexec/tfenv-install: line 297: unzip: command not found
Tarball unzip failed
// unzip コマンドが入ってなく、失敗した
// unzipコマンドをインストールする
$ apt list unzip
unzip/jammy-updates,jammy-security 6.0-26ubuntu3.1 amd64
N: There is 1 additional version. Please use the '-a' switch to see it
$ sudo apt install unzip
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Suggested packages:
zip
The following NEW packages will be installed:
unzip
0 upgraded, 1 newly installed, 0 to remove and 166 not upgraded.
Need to get 174 kB of archives.
...(略)
$ unzip -v
UnZip 6.00 of 20 April 2009, by Debian. Original by Info-ZIP.
// 再度、インストールしてみる
$ tfenv install 1.4.0
Installing Terraform v1.4.0
Downloading release tarball from https://releases.hashicorp.com/terraform/1.4.0/terraform_1.4.0_linux_amd64.zip
##################################################################################################### 100.0%
Downloading SHA hash file from https://releases.hashicorp.com/terraform/1.4.0/terraform_1.4.0_SHA256SUMS
Not instructed to use Local PGP (/home/*****/.tfenv/use-{gpgv,gnupg}) & No keybase install found, skipping OpenPGP signature verification
Archive: /tmp/tfenv_download.gpeahY/terraform_1.4.0_linux_amd64.zip
inflating: /home/*****/.tfenv/versions/1.4.0/terraform
Installation of terraform v1.4.0 successful. To make this your default version, run 'tfenv use 1.4.0'
// インストールでいるか確認
$ tfenv list
1.4.0
No default set. Set with 'tfenv use <version>'
// 1.4.0を使うように指定
$ tfenv use 1.4.0
Switching default version to v1.4.0
Default version (when not overridden by .terraform-version or TFENV_TERRAFORM_VERSION) is now: 1.4.0
// バージョン確認
$ terraform --version
Terraform v1.4.0
on linux_amd64
Your version of Terraform is out of date! The latest version
is 1.4.2. You can update by downloading from https://www.terraform.io/downloads.html
他のバージョンを使う場合にも、
list-remote -> install -> list -> use
の順番で実行していけば問題ないです
参考
Discussion