LoginSignup
2
0

More than 1 year has passed since last update.

Ubuntu (WSL) に asdf をインストールして Ruby と Crystal をそれぞれ複数バージョンインストール

Last updated at Posted at 2022-08-24

asdf とは

プログラミング言語などのバージョンを管理するツールです。
Ruby なら rbenv が有名ですが、asdf は様々なプログラミング言語やツールについて複数のバージョンを自在に切り替えながら使うことを可能にしてくれます。

なぜ asdf を使いたいのか

「複数の言語を」「単一のしくみ」でバージョン管理できる点が嬉しいです。

私は Ruby と Crystal を使って競技プログラミングをやっているのですが、コンテストでは必ずしも各言語の最新バージョンを使えるわけではありません。かつ、ときどきコンテストサイトで言語アップデートがあってバージョンが変わるのに追随する必要があります。
また、使っている言語の最新バージョンが出るとやっぱりさわってみたくなったりもします。

asdf なら、Ruby も Crystal も(新しく増えたらその言語も)同じしくみで、コンテスト用のバージョンから最新バージョンまで簡単に切り替えながら使えていろいろ捗ります。

環境、前提条件など

Ubuntu 20.04.4 LTS (Windows 11 上の WSL2)
2022年8月21日に sudo apt update && sudo apt upgrade した直後の状態からそのまま下記を行っていきました。

asdf (のコア)のインストール

公式ガイド に沿って。
まずは git clone。

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

~/.bashrc 編集。下記を追記。

.bashrc
. $HOME/.asdf/asdf.sh
. $HOME/.asdf/completions/asdf.bash

ここで .bashrc を効かせるためにいったんログアウトしてログインし直し。

Ruby のインストール

まずは asdf の Ruby 用 plugin をインストール。

asdf plugin add ruby https://github.com/asdf-vm/asdf-ruby.git

いよいよ Ruby (バージョンは競技プログラミング用に 2.7.1)をインストール、しようとしたらこけた。

naokid@myhost:~$ asdf install ruby 2.7.1
No system openssl version was found, ensure openssl headers are installed (https://github.com/rbenv/ruby-build/wiki#suggested-build-environment)Downloading openssl-1.1.1q.tar.gz...
-> https://dqw8nmjcqpjn7.cloudfront.net/d7939ce614029cdff0b6c20f0e2e5703158a489a72b2507b8bd51bf8c8fd10ca
Installing openssl-1.1.1q...
patching file test/v3ext.c

BUILD FAILED (Ubuntu 20.04 using ruby-build 20220726)

Inspect or clean up the working tree at /tmp/ruby-build.20220821011037.396.xnGcMh
Results logged to /tmp/ruby-build.20220821011037.396.log

Last 10 log lines:
***   issue on GitHub <https://github.com/openssl/openssl/issues>  ***
***   and include the output from the following command:           ***
***                                                                ***
***       perl configdata.pm --dump                                ***
***                                                                ***
***   (If you are new to OpenSSL, you might want to consult the    ***
***   'Troubleshooting' section in the INSTALL file first)         ***
***                                                                ***
**********************************************************************
/home/naokid/.asdf/plugins/ruby/ruby-build/bin/ruby-build: line 595: make: command not found

冒頭に openssl 関連の記述がありますが、そこより末尾のほうで make が無いぞって怒られてるらしいのがダメそうです。

https://github.com/asdf-vm/asdf-ruby#install を見るとこう書いてありました。

Please make sure you have the required system dependencies installed before trying to install Ruby.

あっはいすみません。

https://github.com/rbenv/ruby-build/wiki#suggested-build-environment を見て、下記を実行しました。

sudo apt-get install autoconf bison build-essential libssl-dev libyaml-dev libreadline6-
dev zlib1g-dev libncurses5-dev libffi-dev libgdbm6 libgdbm-dev libdb-dev uuid-dev

あらためて Ruby 2.7.1 のインストール、今度は成功。

naokid@myhost:~$ asdf install ruby 2.7.1
Downloading ruby-2.7.1.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.7/ruby-2.7.1.tar.bz2
Installing ruby-2.7.1...
Installed ruby-2.7.1 to /home/naokid/.asdf/installs/ruby/2.7.1

せっかくなので最新版も入れときます。

naokid@myhost:~$ asdf install ruby latest
Downloading ruby-3.1.2.tar.gz...
-> https://cache.ruby-lang.org/pub/ruby/3.1/ruby-3.1.2.tar.gz
Installing ruby-3.1.2...
Installed ruby-3.1.2 to /home/naokid/.asdf/installs/ruby/3.1.2

使うバージョンは 2.7.1 に設定。

naokid@myhost:~$ asdf global ruby 2.7.1
naokid@myhost:~$ ruby --version
ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-linux]

Crystal のインストール

Crystal の公式サイトに超簡潔な インストラクション がありました。

asdf の Crystal 用 plugin をインストール。

asdf plugin add crystal

続いて Crystal をインストール。
やはり競技プログラミング用に 0.33.0 と、最新バージョン(1.5.0 でした)もインストールしておきます。使うバージョンは 0.33.0 で。

naokid@myhost:~$ asdf install crystal 0.33.0
∗ Downloading and installing Crystal...
The installation was successful!
naokid@myhost:~$ 
naokid@myhost:~$ asdf install crystal latest
∗ Downloading and installing Crystal...
The installation was successful!
naokid@myhost:~$ asdf list crystal
  0.33.0
  1.5.0
naokid@myhost:~$ 
naokid@myhost:~$ asdf global crystal 0.33.0
naokid@myhost:~$ crystal --version
Crystal 0.33.0 [612825a53] (2020-02-14)

LLVM: 8.0.0
Default target: x86_64-unknown-linux-gnu

すんなり成功!
…と思ったのですが、その後、簡素なテストプログラムを書いてテストしてみたらエラーに。

test.cr
puts "Hello, world!!"
naokid@myhost:~$ crystal ./test.cr 
/usr/bin/ld: cannot find -levent (this usually means you need to install the development package for libevent)
collect2: error: ld returned 1 exit status
Error: execution of command failed with code: 1: `cc "${@}" -o '/home/naokid/.cache/crystal/crystal-run-test.tmp'  -rdynamic  -lpcre -lm /home/naokid/.asdf/installs/crystal/0.33.0/bin/../lib/crystal/lib/libgc.a -lpthread /home/naokid/.asdf/installs/crystal/0.33.0/share/crystal/src/ext/libcrystal.a -levent -lrt -ldl -L/home/naokid/.asdf/installs/crystal/0.33.0/bin/../lib/crystal/lib -L/usr/lib -L/usr/local/lib`

また何か無いぞって怒られてますね。学習能力…
https://github.com/crystal-lang/crystal/wiki/All-required-libraries を見つけて、下記を実行しました。

sudo apt-get install -y \
  automake \
  build-essential \
  git \
  libbsd-dev \
  libedit-dev \
  libevent-dev \
  libgmp-dev \
  libgmpxx4ldbl \
  libpcre3-dev \
  libssl-dev \
  libtool \
  libxml2-dev \
  libyaml-dev \
  lld \
  llvm \
  llvm-dev\
  libz-dev

あらためて。

naokid@myhost:~$ crystal ./test.cr
Hello, world!!

めでたしめでたし。

参考

この記事はインストール(トラブル)の記録って感じでした(になってしまいました)。
asdf や Ruby、Crystal について私より詳しい方々が書いた記事として以下を紹介しておきます。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0