やってみる

アウトプットすべく己を導くためのブログ。その試行錯誤すらたれ流す。

Rubyをインストールした(anyenv>rbenv>ruby)

 執筆時点での最新3.0.2を入れた。

前回まで

anyenv

 anyenvはpyenvやrbenvを一元管理するツール。rbenvはrubyをインストールしてバージョン管理するツール。rubyRuby言語で書かれたコードを実行するインタプリタ

インストール方法

 方法は2つある。Macユーザ以外はソースコードからビルドするしかなさそう。

手順

  1. ソースコードを入手する
  2. 環境変数PATHにセットする
  3. シェルに設定する
  4. 初期化する
  5. rbenvをインストールする
  6. rubyをインストールする

1. ソースコードを入手する

git clone https://github.com/anyenv/anyenv ~/.anyenv

2. 環境変数PATHにセットする

echo 'export PATH="$HOME/.anyenv/bin:$PATH"' >> ~/.bash_profile

3. シェルに設定する

 端末を起動するたびにanyenvを初期化する。

echo 'eval "$(anyenv init -)"' >> ~/.bash_profile

 ここでハマった。私は最初、上記とは違うコマンドにしてしまった。するとrbenvからinstallコマンドが消えて、rubyがインストールできなくなった。詳細は失敗した方法を参照。

4. 初期化する

anyenv install --init
Manifest directory doesn't exist: /home/pi/.config/anyenv/anyenv-install
Do you want to checkout https://github.com/anyenv/anyenv-install.git? [y/N]: 

 yを入力しEnterキー押下。すると以下のように出力された。

Cloning https://github.com/anyenv/anyenv-install.git master to /home/pi/.config/anyenv/anyenv-install...
Cloning into '/home/pi/.config/anyenv/anyenv-install'...
remote: Enumerating objects: 62, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 62 (delta 1), reused 1 (delta 0), pack-reused 57
Unpacking objects: 100% (62/62), done.

Completed!

5. rbenvをインストールする

anyenv install rbenv
exec $SHELL -l

6. rubyをインストールする

 最新安定版をインストールするワンライナーは以下。

rbenv install $(rbenv install -l  2>/dev/null | grep -v - | tail -1)
rbenv install $(rbenv install -l 2>/dev/null | grep '^[0-9]+.[0-9]+.[0-9]+$' | tail -1)

6-1. バージョン一覧

 インストールできるバージョン一覧を取得する。

$ rbenv install -l
2.6.8
2.7.4
3.0.2
jruby-9.3.1.0
mruby-3.0.0
rbx-5.0
truffleruby-21.2.0.1
truffleruby+graalvm-21.2.0

Only latest stable releases for each Ruby implementation are shown.
Use 'rbenv install --list-all / -L' to show all local versions.

 最新は3.0.2。ネットで調べたのと同じ。aptでインストールできた最新の2.5系はない。たぶんサポート期限切れなのだろう。相変わらずaptのバージョンの古さは酷すぎて笑えない。

6-1. 指定バージョンをインストールする

time rbenv install 3.0.2

ログ

Downloading ruby-3.0.2.tar.gz...
-> https://cache.ruby-lang.org/pub/ruby/3.0/ruby-3.0.2.tar.gz
Installing ruby-3.0.2...
Installed ruby-3.0.2 to /home/pi/.anyenv/envs/rbenv/versions/3.0.2

real    15m4.371s
user    26m51.224s
sys 4m39.414s

 30分かけてインストール完了。

6-2. バージョン確認

$ ruby --version
ruby 2.5.5p157 (2019-03-15 revision 67260) [arm-linux-gnueabihf]

 あれ、2.5になってるんだけど。

$ rbenv versions
* system
  3.0.2

 ていうかruby2.5ってOSにインストールされていたんだ。知らんかった。

 3.0.2をデフォルトにセットしよう。

rbenv global 3.0.2
$ rbenv versions
  system
* 3.0.2 (set by /home/pi/.anyenv/envs/rbenv/version)
$ ruby --version
ruby 3.0.2p107 (2021-07-07 revision 0db68f0233) [armv7l-linux-eabihf]

 OK!

6-3. ヘルプ

ruby -h

ログ

Usage: ruby [switches] [--] [programfile] [arguments]
  -0[octal]       specify record separator (\0, if no argument)
  -a              autosplit mode with -n or -p (splits $_ into $F)
  -c              check syntax only
  -Cdirectory     cd to directory before executing your script
  -d              set debugging flags (set $DEBUG to true)
  -e 'command'    one line of script. Several -e's allowed. Omit [programfile]
  -Eex[:in]       specify the default external and internal character encodings
  -Fpattern       split() pattern for autosplit (-a)
  -i[extension]   edit ARGV files in place (make backup if extension supplied)
  -Idirectory     specify $LOAD_PATH directory (may be used more than once)
  -l              enable line ending processing
  -n              assume 'while gets(); ... end' loop around your script
  -p              assume loop like -n but print line also like sed
  -rlibrary       require the library before executing your script
  -s              enable some switch parsing for switches after script name
  -S              look for the script using PATH environment variable
  -v              print the version number, then turn on verbose mode
  -w              turn warnings on for your script
  -W[level=2|:category]     set warning level; 0=silence, 1=medium, 2=verbose
  -x[directory]   strip off text before #!ruby line and perhaps cd to directory
  --jit           enable JIT with default options (experimental)
  --jit-[option]  enable JIT with an option (experimental)
  -h              show this message, --help for more info

 OK! Rubyインストール完了!

 おつかれさまでした!

 以下、蛇足。

失敗した方法

 3. シェルに設定するとき、以下のように設定した。これはanyenv initコマンドを実行したときの出力ログにて明らかになったコマンドである。

echo 'eval "$(anyenv init -)"' >> ~/.bash_profile

 上記のようにせず、ドキュメントを参考に$ ~/.anyenv/bin/anyenv initする。これを自動化するために以下のコマンドにしてしまったら、rbenvからinstallコマンドが消えた。なにをいっているかわからないと思うが、私もわからない。

echo '$HOME/.anyenv/bin/anyenv init  &>/dev/null' >> ~/.bash_profile

上記コマンドに至った経緯

 最初はドキュメントにあるとおり端末で$HOME/.anyenv/bin/anyenv initコマンドを叩いた。すると以下のようなメッセージが出た。なので最初から上記のようにした。evalで書く必要性がわからないので直に叩いた。

# Load anyenv automatically by adding
# the following to ~/.bash_profile:

eval "$(anyenv init -)"

 成功すると端末を起動するたびに自動でanyenv initされる。ただ、毎回上記が出力されてウザい。なので&>/dev/nullをつけて出力を消した。

4. 初期化する

anyenv install --init
Manifest directory doesn't exist: /home/pi/.config/anyenv/anyenv-install
Do you want to checkout https://github.com/anyenv/anyenv-install.git? [y/N]: 

 yを入力しEnterキー押下。すると以下のように出力された。

Cloning https://github.com/anyenv/anyenv-install.git master to /home/pi/.config/anyenv/anyenv-install...
Cloning into '/home/pi/.config/anyenv/anyenv-install'...
remote: Enumerating objects: 62, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 62 (delta 1), reused 1 (delta 0), pack-reused 57
Unpacking objects: 100% (62/62), done.

Completed!

ヘルプ

anyenv --help

ログ

anyenv 1.1.4
Usage: anyenv <command> [<args>]

Some useful anyenv commands are:
   commands            List all available anyenv commands
   local               Show the local application-specific Any version
   global              Show the global Any version
   install             Install a **env
   uninstall           Uninstall a specific **env
   version             Show the current Any version and its origin
   versions            List all Any versions available to **env

See `anyenv help <command>' for information on a specific command.
For full documentation, see: https://github.com/anyenv/anyenv#readme

5. rbenvをインストールする

anyenv install rbenv

ログ

/tmp/rbenv.20211019102537.3991 /tmp/work
Cloning https://github.com/rbenv/rbenv.git master to rbenv...
Cloning into 'rbenv'...
remote: Enumerating objects: 2977, done.
remote: Counting objects: 100% (81/81), done.
remote: Compressing objects: 100% (60/60), done.
remote: Total 2977 (delta 41), reused 41 (delta 21), pack-reused 2896
Receiving objects: 100% (2977/2977), 597.82 KiB | 1.82 MiB/s, done.
Resolving deltas: 100% (1850/1850), done.
/tmp/work
~/.anyenv/envs/rbenv/plugins /tmp/work
Cloning https://github.com/rbenv/ruby-build.git master to ruby-build...
Cloning into 'ruby-build'...
remote: Enumerating objects: 11849, done.
remote: Counting objects: 100% (542/542), done.
remote: Compressing objects: 100% (182/182), done.
remote: Total 11849 (delta 362), reused 478 (delta 325), pack-reused 11307
Receiving objects: 100% (11849/11849), 2.49 MiB | 1.89 MiB/s, done.
Resolving deltas: 100% (7829/7829), done.
/tmp/work

Install rbenv succeeded!
Please reload your profile (exec $SHELL -l) or open a new session.

 え、もうインストールされたの?

 exec $SHELL -lを実行しろとあるが、やらないとどうなるの?

$ rbenv --version
bash: rbenv: コマンドが見つかりません

 ないってよ。じゃあやるか。

exec $SHELL -l

 再試行。したけどダメだった。はぁ? インストールされてないじゃん。

$ rbenv --version
bash: rbenv: コマンドが見つかりません

 どういうことだよ。

 探してみたら以下のパスに実体があった。ということはパスが通っていないだけっぽい。いやいや、そこも面倒みてくれるんじゃないのかよanyenv。使えねーな。これじゃanyenv使っている意味ないじゃん。ちゃんとパス通してくれよ。

$HOME/.anyenv/envs/rbenv/bin/rbenv

 しょうがない。とりあえずパスを通さずフルパスでコマンドを叩こう。┐(´д`)┌ヤレヤレ

もしパスを通すなら

 たぶんパスを通すとなると、以下のようなことが必要になる。あのさぁ、これってpyenvとか追加したらそのたびに追加しないとダメなの? マジでanyenvの存在意義がない。そういう面倒なのをまとめてやってくれるんじゃなかったのかよ。

echo 'export PATH="$HOME/.anyenv/envs/rbenv/bin:$PATH"' >> ~/.bash_profile
echo '$HOME/.anyenv/envs/rbenv/bin/rbenv init  &>/dev/null' >> ~/.bash_profile

6. rubyをインストールする

$HOME/.anyenv/envs/rbenv/bin/rbenv install
rbenv: no such command `install'

 は? いやおまえ、ドキュメントにそう書いてあったやん。コマンドがないってどういうことだ。仕方ないからヘルプをみよう。

$ $HOME/.anyenv/envs/rbenv/bin/rbenv --help
Usage: rbenv <command> [<args>]

Some useful rbenv commands are:
   commands    List all available rbenv commands
   local       Set or show the local application-specific Ruby version
   global      Set or show the global Ruby version
   shell       Set or show the shell-specific Ruby version
   rehash      Rehash rbenv shims (run this after installing executables)
   version     Show the current Ruby version and its origin
   versions    List installed Ruby versions
   which       Display the full path to an executable
   whence      List all Ruby versions that contain the given executable

See `rbenv help <command>' for information on a specific command.
For full documentation, see: https://github.com/rbenv/rbenv#readme

 ないwww 本当にinstallがないよ。ヘルプにすらない。

 そんなバカな。rbenvはrubyをインストールするツールだよね? バージョン管理するんだよね? なのにインストールできないとかwww

 一応rbenvでググってみた。やっぱりinstallコマンドはあるはず。当たり前だ。

 もしかしてanyenvと同じで初期化が必要なのでは? 以下みたいなコマンドを最初にせねばならないとか?

$HOME/.anyenv/envs/rbenv/bin/rbenv init

 再試行。ダメだ。やっぱりない。

$ $HOME/.anyenv/envs/rbenv/bin/rbenv install
rbenv: no such command `install'

 なにこれクソすぎるんですけど。

 というわけでハマった。

所感

 やはり環境構築でハマる。困ったもんだ。まあPythonのときの苦しみから学んだこともあるし、あのときに比べればマシ。結局、ちゃんとドキュメントやコマンドの結果を読んで理解しなかった私が悪いのだろう。

 でも未だにわからない。以下の何が違うというのか。

eval "$(anyenv init -)
anyenv init

 まあいいや。環境構築できたし。

対象環境

$ uname -a
Linux raspberrypi 5.10.52-v7l+ #1441 SMP Tue Aug 3 18:11:56 BST 2021 armv7l GNU/Linux