AR ホームベーカリー

オイラのアウトプット用ホームベーカリー!

AmazonLinux2023 で雑に anyenv (rbenv) 環境を作る

asdf とか、 **env を個別にインストールするとか、今は選択肢色々あるけど、 env 系が慣れているのと anyenv だと **-build を別途導入しなくても一緒に用意してくれて楽、という向きがあるので anyenv を導入しています。

バイナリへのパスとかインストール先に依存して変更されてしまうので、雑に本番環境などで採用すると事故の元なんですが、それでも Docker 未満の環境ではベストプラクティスに近い動きをできるので、そういう感じでいきます。

事前準備

必要になるパッケージ (ライブラリ) をインストールする。

[ec2-user@localhost ~]$ sudo dnf groupinstall "Development Tools"
[ec2-user@localhost ~]$ sudo dnf -y install zlib-devel bzip2 bzip2-devel readline-devel openssl-devel xz xz-devel libyaml libyaml-devel
[ec2-user@localhost ~]$ sudo dnf swap libcurl-minimal libcurl-full
[ec2-user@localhost ~]$ sudo dnf swap curl-minimal curl-full
[ec2-user@localhost ~]$ sudo dnf install libcurl-devel.x86_64
[ec2-user@localhost ~]$ sudo dnf -y install jemalloc*

anyenv

anyenv をインストールします。

[ec2-user@localhost ~]$ git clone https://github.com/anyenv/anyenv ~/.anyenv
[ec2-user@localhost ~]$ echo 'export PATH="$HOME/.anyenv/bin:$PATH"' >> ~/.bash_profile
[ec2-user@localhost ~]$ ~/.anyenv/bin/anyenv init

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

eval "$(anyenv init -)"

[ec2-user@localhost ~]$ vi ~/.bash_profile

.bash_profile に以下の内容を追記する。

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

eval "$(anyenv init -)"
[ec2-user@localhost ~]$ exec $SHELL -l

ANYENV_DEFINITION_ROOT(/home/ec2-user/.config/anyenv/anyenv-install) doesn't exist. You can initialize it by:
> anyenv install --init

[ec2-user@localhost ~]$ anyenv install --init

Manifest directory doesn't exist: /home/ec2-user/.config/anyenv/anyenv-install
Do you want to checkout https://github.com/anyenv/anyenv-install.git? [y/N]: y
Cloning https://github.com/anyenv/anyenv-install.git master to /home/ec2-user/.config/anyenv/anyenv-install...
Cloning into '/home/ec2-user/.config/anyenv/anyenv-install'...
remote: Enumerating objects: 71, done.
remote: Counting objects: 100% (14/14), done.
remote: Compressing objects: 100% (13/13), done.
remote: Total 71 (delta 4), reused 4 (delta 1), pack-reused 57 (from 1)
Receiving objects: 100% (71/71), 13.15 KiB | 4.38 MiB/s, done.
Resolving deltas: 100% (11/11), done.

Completed!

anyenv plugin のインストール

いい感じに anyenv を利用するために、プラグインをインストールしておきます。

[ec2-user@localhost ~]$ mkdir -p ~/.anyenv/plugins;cd $_
[ec2-user@localhost plugins]$ git clone https://github.com/znz/anyenv-update.git ./anyenv-update

Cloning into './anyenv-update'...
remote: Enumerating objects: 87, done.
remote: Total 87 (delta 0), reused 0 (delta 0), pack-reused 87 (from 1)
Receiving objects: 100% (87/87), 13.33 KiB | 6.67 MiB/s, done.
Resolving deltas: 100% (33/33), done.

[ec2-user@localhost plugins]$ git clone https://github.com/znz/anyenv-git.git ./anyenv-git

Cloning into './anyenv-git'...
remote: Enumerating objects: 48, done.
remote: Total 48 (delta 0), reused 0 (delta 0), pack-reused 48 (from 1)
Receiving objects: 100% (48/48), 7.61 KiB | 7.61 MiB/s, done.
Resolving deltas: 100% (15/15), done.

はいできた。

この後はシェル再起動でそのまま使えるんですが、環境構築という意味では .bash_profile 読み直しで問題おきないか、というトコを確認する意味もこめて reboot してみるとよいでしょう。

[ec2-user@localhost plugins]$ sudo reboot

**env のインストール

今回は例として rbenv と nodenv をインストールします。

[ec2-user@localhost ~]$ anyenv install rbenv

/tmp/rbenv.20241202222214.2838 ~
Cloning https://github.com/rbenv/rbenv.git master to rbenv...
Cloning into 'rbenv'...
remote: Enumerating objects: 3340, done.
remote: Counting objects: 100% (490/490), done.
remote: Compressing objects: 100% (267/267), done.
remote: Total 3340 (delta 278), reused 376 (delta 207), pack-reused 2850 (from 1)
Receiving objects: 100% (3340/3340), 685.53 KiB | 10.39 MiB/s, done.
Resolving deltas: 100% (2068/2068), done.
~
~/.anyenv/envs/rbenv/plugins ~
Cloning https://github.com/rbenv/ruby-build.git master to ruby-build...
Cloning into 'ruby-build'...
remote: Enumerating objects: 16582, done.
remote: Counting objects: 100% (4298/4298), done.
remote: Compressing objects: 100% (401/401), done.
remote: Total 16582 (delta 4079), reused 4023 (delta 3885), pack-reused 12284 (from 1)
Receiving objects: 100% (16582/16582), 3.20 MiB | 12.12 MiB/s, done.
Resolving deltas: 100% (11783/11783), done.
~

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

[ec2-user@localhost ~]$ exec $SHELL -l
[ec2-user@localhost ~]$ anyenv install nodenv

/tmp/nodenv.20241202222229.2993 ~
Cloning https://github.com/nodenv/nodenv.git master to nodenv...
Cloning into 'nodenv'...
remote: Enumerating objects: 4519, done.
remote: Counting objects: 100% (516/516), done.
remote: Compressing objects: 100% (268/268), done.
remote: Total 4519 (delta 264), reused 473 (delta 237), pack-reused 4003 (from 1)
Receiving objects: 100% (4519/4519), 885.40 KiB | 11.96 MiB/s, done.
Resolving deltas: 100% (2895/2895), done.
~
~/.anyenv/envs/nodenv/plugins ~
Cloning https://github.com/nodenv/node-build.git master to node-build...
Cloning into 'node-build'...
remote: Enumerating objects: 28219, done.
remote: Counting objects: 100% (3685/3685), done.
remote: Compressing objects: 100% (1374/1374), done.
remote: Total 28219 (delta 2313), reused 2793 (delta 2233), pack-reused 24534 (from 1)
Receiving objects: 100% (28219/28219), 5.23 MiB | 16.88 MiB/s, done.
Resolving deltas: 100% (18053/18053), done.
~
~/.anyenv/envs/nodenv/plugins ~
Cloning https://github.com/nodenv/nodenv-vars.git master to nodenv-vars...
Cloning into 'nodenv-vars'...
remote: Enumerating objects: 211, done.
remote: Total 211 (delta 0), reused 0 (delta 0), pack-reused 211 (from 1)
Receiving objects: 100% (211/211), 31.82 KiB | 7.96 MiB/s, done.
Resolving deltas: 100% (76/76), done.
~

Install nodenv succeeded!
Please reload your profile (exec $SHELL -l) or open a new session.
[ec2-user@localhost ~]$ exec $SHELL -l

できた。 **-build などのプラグインも一緒にインストールされているので大丈夫そうですね。

rbenv で ruby をインストール (jemalloc 有効化して)

せっかくなので jemalloc 有効化した ruby 環境も用意してみましょう。 env 系使いたくて Docker 移行していない、という想定だと Ruby 2.x 系がモチベーションとして一番多そうので、そのあたりで適当に。

[ec2-user@localhost ~]$ RUBY_CONFIGURE_OPTS=--with-jemalloc rbenv install 2.7.5

==> Downloading openssl-1.1.1w.tar.gz...
-> curl -q -fL -o openssl-1.1.1w.tar.gz https://dqw8nmjcqpjn7.cloudfront.net/cf3098950cb4d853ad95c0841f1f9c6d3dc102dccfcacd521d93925208b76ac8
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 9661k  100 9661k    0     0  34.0M      0 --:--:-- --:--:-- --:--:-- 34.0M
==> Installing openssl-1.1.1w...
-> ./config "--prefix=$HOME/.anyenv/envs/rbenv/versions/2.7.5/openssl" "--openssldir=$HOME/.anyenv/envs/rbenv/versions/2.7.5/openssl/ssl" --libdir=lib zlib-dynamic no-ssl3 shared
-> make -j 2
-> make install_sw install_ssldirs
==> Installed openssl-1.1.1w to /home/ec2-user/.anyenv/envs/rbenv/versions/2.7.5
==> Downloading ruby-2.7.5.tar.bz2...
-> curl -q -fL -o ruby-2.7.5.tar.bz2 https://cache.ruby-lang.org/pub/ruby/2.7/ruby-2.7.5.tar.bz2
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 14.1M  100 14.1M    0     0  15.8M      0 --:--:-- --:--:-- --:--:-- 15.8M
==> Installing ruby-2.7.5...

WARNING: ruby-2.7.5 is past its end of life and is now unsupported.
It no longer receives bug fixes or critical security updates.

-> ./configure "--prefix=$HOME/.anyenv/envs/rbenv/versions/2.7.5" "--with-openssl-dir=$HOME/.anyenv/envs/rbenv/versions/2.7.5/openssl" --enable-shared --with-ext=openssl,psych,+ --with-jemalloc
-> make -j 2
-> make install
==> Installed ruby-2.7.5 to /home/ec2-user/.anyenv/envs/rbenv/versions/2.7.5

NOTE: to activate this Ruby version as the new default, run: rbenv global 2.7.5

[ec2-user@localhost ~]$ rbenv global 2.7.5
[ec2-user@localhost ~]$ ruby -v

ruby 2.7.5p203 (2021-11-24 revision f69aeb8314) [x86_64-linux]

無事にインストールできましたね。 なるべくはやめに Docker に引っ越せるようにしましょう。

備考

通常 /usr/local/bin 以下などに配置される gem は、概ね以下に配置されるので、 capistrano-** など使っておりなんらかのコマンドをフルパスで書いている、などの場合はフォローが必要なので注意しましょう (一敗) 。

[ec2-user@localhost ~]$ ls -lha ~/.anyenv/envs/rbenv/shims/
total 68K
drwxr-xr-x.  2 ec2-user ec2-user 168 Dec  5 21:05 .
drwxr-xr-x. 14 ec2-user ec2-user 16K Dec  2 23:05 ..
-rwxr-xr-x.  1 ec2-user ec2-user 423 Dec  2 22:43 bundle
-rwxr-xr-x.  1 ec2-user ec2-user 423 Dec  2 22:43 bundler
-rwxr-xr-x.  1 ec2-user ec2-user 423 Dec  2 22:43 erb
-rwxr-xr-x.  1 ec2-user ec2-user 423 Dec  2 22:43 gem
-rwxr-xr-x.  1 ec2-user ec2-user 423 Dec  2 22:43 irb
-rwxr-xr-x.  1 ec2-user ec2-user 423 Dec  2 22:43 racc
-rwxr-xr-x.  1 ec2-user ec2-user 423 Dec  2 22:43 racc2y
-rwxr-xr-x.  1 ec2-user ec2-user 423 Dec  2 23:17 rackup
-rwxr-xr-x.  1 ec2-user ec2-user 423 Dec  2 22:43 rake
-rwxr-xr-x.  1 ec2-user ec2-user 423 Dec  2 22:43 rdoc
-rwxr-xr-x.  1 ec2-user ec2-user 423 Dec  2 22:43 ri
-rwxr-xr-x.  1 ec2-user ec2-user 423 Dec  2 22:43 ruby
-rwxr-xr-x.  1 ec2-user ec2-user 423 Dec  2 22:43 y2racc

#参考

scrapbox.io