LoginSignup
2

posted at

rbenvをシステム全体で利用できるようにする + Ruby 3をruby-buildでビルド on AlmaLinux 9

rbenvをユーザー毎ではなく、システム全体で共有して利用できるようにします。

前提

  • 事前にgitを使用可能にしておく
  • rbenv関連の書き込み権限を持つグループとしてrbnevグループを作成、必要なユーザーを所属させる

環境

  • AlmaLinux 9で確認しています

rbenvインストール

rbenvを操作できるグループを追加

$ sudo groupadd rbenv

追加したグループにユーザを追加

$ sudo gpasswd -a USERNAME rbenv

rbenv用のディレクトリを作成

$ sudo mkdir /usr/local/rbenv

ディレクトリの所有者(グループ)を変更

$ sudo chown .rbenv /usr/local/rbenv

ディレクトリにパーミッションを設定

$ sudo chmod 2775 /usr/local/rbenv

rbenvをインストール

Basic Git Checkoutを参考に。
以降、前述のrbenvグループにユーザーが属していることを前提にしています。
idコマンドで表示されるgroupsにrbenvが含まれない場合、再ログインする等してから実行します。

$ git clone https://github.com/rbenv/rbenv.git /usr/local/rbenv

環境変数等設定

/etc/profile.d下にrbenv.sh等の名前のファイルに以下を記述して保存、rbenvコマンドを利用できるようにします。

/etc/profile.d/rbenv.sh
export RBENV_ROOT="/usr/local/rbenv"
export PATH="$RBENV_ROOT/bin:$PATH"
eval "$(rbenv init --no-rehash -)"

sourceコマンドでこのファイルを読み込み、初期化します。

$ source /etc/profile.d/rbenv.sh

sudoで使用可能にする

env_keepsecure_pathに設定を追記します。

$ sudo visudo
# 既存の Defaults env_keep の下に追加
Defaults    env_keep += "RBENV_ROOT"

# 既存の Defaults secure_path の末尾に :/usr/local/rbenv/bin:/usr/local/rbenv/shims を追記
Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/usr/local/rbenv/bin:/usr/local/rbenv/shims

確認

一度ログアウト、ログインしコマンドを利用できることを確認します。

$ rbenv --version
rbenv 1.2.0-52-g61747c0

rbenv-doctor

更に念を押して確認するには、rbenv-doctorを利用します。

$ curl -fsSL https://github.com/rbenv/rbenv-installer/raw/HEAD/bin/rbenv-doctor | bash
Checking for `rbenv' in PATH: /usr/local/rbenv/bin/rbenv
Checking for rbenv shims in PATH: OK
Checking `rbenv install' support: not found
Unless you plan to add Ruby versions manually, you should install ruby-build.
Please refer to https://github.com/rbenv/ruby-build#installation

Counting installed Ruby versions: none
  There aren't any Ruby versions installed under `/usr/local/rbenv/versions'.
  You can install Ruby versions like so: rbenv install <version>
Auditing installed plugins: OK

この時点ではruby-buildを導入していないため、当該箇所はnot foundとなっています。

ruby-buildインストール

本体

gitでcloneします。

$ git clone https://github.com/rbenv/ruby-build.git "$(rbenv root)"/plugins/ruby-build

Rubyビルド用パッケージ

Suggested build environmentを参考に、必要なパッケージをインストールします。

AlmaLinux 9
$ sudo dnf config-manager --set-enabled crb
$ sudo dnf install -y epel-release
$ sudo dnf install gcc patch bzip2 openssl-devel libyaml-devel libffi-devel readline-devel zlib-devel gdbm-devel ncurses-devel rust

確認

例えば、rbenv install --listでインストール可能なRuby(のうち、各実装毎の最新)一覧が出てきます。

$ rbenv install --list
2.7.7
3.0.5
3.1.3
3.2.0
jruby-9.4.0.0
mruby-3.1.0
picoruby-3.0.0
truffleruby-22.3.1
truffleruby+graalvm-22.3.1

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

rbenv-doctor

rbenv-doctorも利用できます。

$ curl -fsChecking for `rbenv' in PATH: /usr/local/rbenv/bin/rbenv
Checking for rbenv shims in PATH: OK
Checking `rbenv install' support: /usr/local/rbenv/plugins/ruby-build/bin/rbenv-install (ruby-build 20230202)
Counting installed Ruby versions: none
  There aren't any Ruby versions installed under `/usr/local/rbenv/versions'.
  You can install Ruby versions like so: rbenv install 3.2.0
Auditing installed plugins: OK
SL https://github.com/rbenv/rbenv-installer/raw/HEAD/bin/rbenv-doctor | bash

Rubyインストール

RUBY_CONFIGURE_OPTS、進捗が見える-vオプションはお好みで。

$ RUBY_CONFIGURE_OPTS="--disable-install-doc --disable-install-rdoc" rbenv install 3.2.0 -v
$ rbenv install 3.2.0 -v
...
~
Installed ruby-3.2.0 to /usr/local/rbenv/versions/3.2.0

インストールしたRubyをデフォルトにする

$ rbenv global 3.2.0
$ ruby -v
ruby 3.2.0 (2022-12-25 revision a528908271) [x86_64-linux]

rubygems、bundlerを最新にする

-Nオプションはお好みで。

$ gem update --system -N
$ gem update bundler -N

全部最新にして良いのであれば以下を実行します。

$ gem update -N

RubyGemsを操作した後は、rehashする習慣を!

環境変数等設定時に--no-rehashを追記しているため、エラー発生の原因になります。

$ rbenv rehash

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
What you can do with signing up
2