【Linux】asdf ~ 入門編 ~

■ はじめに

Terraform の設定時に、asdf ってのを知ったので、
調べてみた

目次

【1】asdf
【2】使いどころ
【3】環境設定
 1)設定環境
 2)設定手順
【4】使い方
 例1:Terraform
 例2:Python
【5】トラブル
 1)Pythonインストール時のトラブル

【1】asdf

* バージョン管理ツール
 => ツールなどをバージョンごとに切り替えてくれるツール

cf. asdf = 空軍

【2】使いどころ

* Python、Node.js、Terraformなど バージョンごとに導入したい時
 => それぞれ pyenv、nvm、tfenv など個別に切り替えるツールはあるが
  それが一つのツールで管理できるって話

【3】環境設定

1)設定環境

* OS : Ubuntu (WSL2 on Windows11)

2)設定手順

* 公式サイトの「Getting Started」の通りに行ってみる

https://asdf-vm.com/guide/getting-started.html

[1] 依存関係インストール

# デフォルトで入っているかも、、、
sudo apt install curl git

[2] asdf のダウンロード
https://asdf-vm.com/guide/getting-started.html#official-download

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

[3] asdf のインストール
https://asdf-vm.com/guide/getting-started.html#_3-install-asdf

# 自分の環境のデフォルトシェルを確認
echo $SHELL

# 3-1) Bashの場合「Bash & Git」より
vi ~/.bashrc
~~~~~~~~~~~~~~~~
# Add for asdf
. $HOME/.asdf/asdf.sh
. $HOME/.asdf/completions/asdf.bash
~~~~~~~~~~~~~~~~
# 反映
source ~/.bashrc

# 3-2) ZCH(/bin/zsh)の場合、「ZSH & Git」より
vi ~/.bashrc
~~~~~~~~~~~~~~~~
# Add for asdf
. "$HOME/.asdf/asdf.sh"
~~~~~~~~~~~~~~~~
# 反映
source ~/.bashrc

# 確認
asdf --version

【4】使い方

例1:Terraform

[1] プラグインのインストール

asdf plugin add terraform

[2] インストール可能な全バージョンの一覧表示

asdf list all terraform

・・・
1.6.5
1.6.6
1.7.0-alpha20231025
1.7.0-alpha20231108
1.7.0-alpha20231130
1.7.0-beta1
1.7.0-beta2
1.7.0-rc1
1.7.0-rc2

[3] インストール

# Step1: 作業ディレクトリ作成 (以下例)
$ mkdir -p ~/work/terraform/v1.6.6
$ cd ~/work/terraform/v1.6.6

# Step2: ".tool-versions" の追加
$ vi .tool-versions
~~~~~~~~~~~~~~~~~~~
terraform 1.6.6
~~~~~~~~~~~~~~~~~~~

# Step3: インストール実行
$ asdf install

# Step4: 確認(意図したバージョンになっていること)
$ terraform -version
Terraform v1.6.6
on linux_amd64

例2:Python

# python プラグインの検索
$ asdf plugin list all | grep python
python                       *https://github.com/danhper/asdf-python.git

# Plugin の追加
$ asdf plugin add python https://github.com/danhper/asdf-python.git

# [外したかったら]
# asdf plugin remove python

# 確認
$ asdf plugin list
python
terraform

# インストール可能なPythonを調べる
$ asdf list all python
...

# 安定バージョンの確認
$ asdf latest python
3.12.1

$ asdf latest python 3.10
3.10.13

# インストール
mkdir ~/work/python/python3.10
cd ~/work/python/python3.10
# asdf install python <version>
# asdf install python latest
# asdf install python latest:<version>
asdf install python latest:3.10
# 結構時間が掛かる

# 確認
$ python -V

https://dev.classmethod.jp/articles/asdf-python-introduction/

【5】トラブル

1)Pythonインストール時のトラブル

「asdf install python latest:3.10」実行後、
結構な時間が経った後に、以下「エラー内容」が表示し
インストールに失敗する

エラー内容

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/user/.asdf/installs/python/3.10.13/lib/python3.10/bz2.py", line 17, in <module>
    from _bz2 import BZ2Compressor, BZ2Decompressor
ModuleNotFoundError: No module named '_bz2'
WARNING: The Python bz2 extension was not compiled. Missing the bzip2 lib?
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/user/.asdf/installs/python/3.10.13/lib/python3.10/curses/__init__.py", line 13, in <module>
    from _curses import *
ModuleNotFoundError: No module named '_curses'
WARNING: The Python curses extension was not compiled. Missing the ncurses lib?
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/user/.asdf/installs/python/3.10.13/lib/python3.10/ctypes/__init__.py", line 8, in <module>
    from _ctypes import Union, Structure, Array
ModuleNotFoundError: No module named '_ctypes'
WARNING: The Python ctypes extension was not compiled. Missing the libffi lib?
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'readline'
WARNING: The Python readline extension was not compiled. Missing the GNU readline lib?
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/user/.asdf/installs/python/3.10.13/lib/python3.10/ssl.py", line 99, in <module>
    import _ssl             # if we can't import it, let the error propagate
ModuleNotFoundError: No module named '_ssl'
ERROR: The Python ssl extension was not compiled. Missing the OpenSSL lib?

Please consult to the Wiki page to fix the problem.
https://github.com/pyenv/pyenv/wiki/Common-build-problems

BUILD FAILED (Ubuntu 22.04 using python-build 2.3.35-8-g46d3954b)

原因

* 実行環境に必要なライブラリが不足しているから

解決案

# Please consult to the Wiki page to fix the problem.
# https://github.com/pyenv/pyenv/wiki/Common-build-problems
ってあるので、上記のリンクに従い、以下を行った
====
Ubuntu/Debian:
  Alternative to libreadline-dev:
    sudo apt install libedit-dev << これ実行
  An inferior alternative to libncursesw5:
    sudo apt install libncurses5-dev << これも実行
====

参考文献

https://zenn.dev/sendo_tech/articles/d1639a6fe37f59
https://qiita.com/murakami-mm/items/2d63177dc8ea002a847b
https://zenn.dev/hongmuchan/articles/4cecb2e090947a

関連記事

Terraform ~ tfenv / バージョン管理 ~
https://dk521123.hatenablog.com/entry/2023/01/14/000000
Python ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2014/08/07/231242