asdf で Python や Ruby をインストールする

asdf のインストール

Ubuntu 22.04 の Zsh で asdf を使用して Ruby、Python、R をインストールした。

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

でダウンロードする。 ~/.zshrc に以下を追加する。

. $HOME/.asdf/asdf.sh
fpath=(${ASDF_DIR}/completions $fpath)
autoload -U compinit
compinit

新しい端末を開くか

source ~/.zshrc

とする。

Ruby

Ruby のインストールには

asdf plugin add ruby

でプラグインを追加する。

asdf list all ruby

でインストールできる Ruby のバージョンの一覧を確認する。

asdf install ruby 3.1.0

で現時点で最新のバージョン 3.1.0 をインストールする。

asdf global ruby 3.1.0
rehash

で使用する ruby のバージョンを設定する。 私の環境では rehash を実行しないとruby コマンドがシステムの ruby のままだった。

Ruby のコンパイルに必要な OpenSSL のコンパイル

Ubuntu 22.04 だと OpenSSL のバージョンが上がって、Ruby 3.0.3 などがコンパイルできなくなった。 openssl 1.1.1 を ~/.asdf/installs/openssl/1.1.1 にインストールすることにした。

wget https://www.openssl.org/source/openssl-1.1.1g.tar.gz
tar xvf openssl-1.1.1g.tar.gz
cd openssl-1.1.1g
./config --prefix=~/.asdf/installs/openssl/1.1.1 --openssldir=~/.asdf/installs/openssl/1.1.1 no-ssl2
make
make install

インストールした OpenSSL を使って、asdf で Ruby をインストールするには

RUBY_CONFIGURE_OPTS="--with-openssl-dir=$HOME/.asdf/installs/openssl/1.1.1/" asdf install ruby 3.0.3

とする。

Ruby のプログラムを実行していたら SSL 関連のエラーが出た。正しい対処方法はよくわからなかったが、システムの証明書を読むように

rmdir ~/.asdf/installs/openssl/1.1.1/certs
ln -s /usr/lib/ssl/certs ~/.asdf/installs/openssl/1.1.1/certs

とした。

Python & R

Python や R もインストールする。

asdf plugin add python
asdf install python 3.10.2
asdf global python 3.10.2
asdf plugin add R
asdf install R 4.1.2
asdf global R 4.1.2

python の pip でインストールしたコマンドがパスに登録されていないことがあったが

asdf reshim python

とすれば直った。

Emacs

Emacs で Python などを使うときに asdf でインストールしたバージョンを使用するには https://github.com/tabfugnic/asdf.el を使う。 asdf.el をダウンロードして適当なディレクトリに保存する。

~/.emacs.d/init.el に

(when (file-exists-p (expand-file-name "~/.asdf"))
  (add-to-list 'load-path "[PATH TO]/asdf.el")
  (require 'asdf)
  (asdf-enable))

などと書く。

参考

Tags of current page

, , ,