はじめに
Linux環境にrubyをインストールする場合、手っ取り早いのはディストリビューションが提供しているリポジトリからrubyのパッケージを導入する方法です。ですが、リポジトリに入っているrubyのバージョンは必ずしも最新レベルのものとは限らないですし、より古いレベルのものを入手したい場合もあります。そんな時には複数のrubyのバージョンを管理できるrbenvというサードパーティーツールと、rubyのインストールをサポートするruby-buildというプラグインを使用すれば、任意のバージョンのrubyをLinux環境に導入して使用することが可能です。そこで、今回はRed Hat Enterprise Linux 8.6にrbenv、ruby-buildを入れて、ディストリビューションのリポジトリが提供しているrubyよりも上位レベルのrubyをインストールしてみました。
Homebrew、rbenv、ruby-buildをインストール
rbenvを見ると、rbenvをパッケージマネージャーを使用したインストールを行う場合、macOSもしくはLinux環境であればHomebrewを使って下記のコマンドを実行するようにと記載されています。
$ brew install rbenv ruby-build
試しにwhich brewコマンドを実行してみましたが、brewコマンドは見つかりませんでした。そのため、下記のコマンドを実行して最初にHomebrewをLinuxにインストールします。
$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
インストールが完了すると、以下のようなアクションを促すメッセージが出力されるので、後は指示通りにコマンドを実行していきます。
==> Next steps:
- Run these two commands in your terminal to add Homebrew to your PATH:
echo 'eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"' >> /home/testuser/.bash_profile
eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
- Install Homebrew's dependencies if you have sudo access:
sudo yum groupinstall 'Development Tools'
For more information, see:
https://docs.brew.sh/Homebrew-on-Linux
- We recommend that you install GCC:
brew install gcc
- Run brew help to get started
- Further documentation:
https://docs.brew.sh
それが終われば、後はrbenvに記載の手順に従い、rbenv、ruby-buildをインストールすれば準備完了です。
ruby 3.1.2をインストール
現在リリースされている安定版rubyのバージョンを知りたい場合には、下記のコマンドを実行します。
$ rbenv install -l
2.6.10
2.7.6
3.0.4
3.1.2
jruby-9.3.6.0
mruby-3.1.0
picoruby-3.0.0
rbx-5.0
truffleruby-22.1.0
truffleruby+graalvm-22.1.0
Only latest stable releases for each Ruby implementation are shown.
Use 'rbenv install --list-all / -L' to show all local versions.
今回は、ruby 3.1.2をインストールすることにします。インストールには下記コマンドを実行します。
$ rbenv install 3.1.2
しかし、何故か下記のようにruby 3.1.2のインストールが失敗してしまいます。
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...
ruby-build: using readline from homebrew
BUILD FAILED (Red Hat Enterprise Linux 8.6 using ruby-build 20220630)
Inspect or clean up the working tree at /tmp/ruby-build.20220702181202.22088.U2e503
Results logged to /tmp/ruby-build.20220702181202.22088.log
Last 10 log lines:
The Ruby openssl extension was not compiled.
ERROR: Ruby install aborted due to missing extensions
Try running `yum install -y openssl-devel` to fetch missing dependencies.
Configure options used:
--prefix=/home/testuser/.rbenv/versions/3.1.2
--enable-shared
--with-readline-dir=/home/linuxbrew/.linuxbrew/opt/readline
LDFLAGS=-L/home/testuser/.rbenv/versions/3.1.2/lib
CPPFLAGS=-I/home/testuser/.rbenv/versions/3.1.2/include
The Ruby openssl extension was not compiled.という内容でエラーになっており、ruby 3.1.2のインストールがFailしたとあります。その直下に、yum install -y openssl-develを試してみるようにと出力されていますので、openssl-develをインストール後にリトライしてみましたが、結果は変わりません。そこで、色々ググって調べたところ、下記リンク先のサイトに記載されている方法でruby 3.1.2のインスト-ルを無事成功することができました。
上記を参考に試してみたことは以下になります。
- RUBY_CONFIGURE_OPTSオプションを使用したインストール
$ RUBY_CONFIGURE_OPTS="--with-readline-dir=`brew --prefix readline` --with-openssll-dir=`brew --prefix openssl`" rbenv install 3.1.2
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/testuser/.rbenv/versions/3.1.2
後は以下のコマンドを順に実行した後、.bashrcにruby 3.1.2がインストールされたディレクトリのPATHをexportコマンドでPATH追記します。
$ rbenv rehash
$ rbenv global 3.1.2
ruby -vコマンドで、rubyのバージョンが3.1.2になっていることが確認できれば完了です。
$ ruby -v
ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [x86_64-linux]
これでruby 3.1.2を使用することができるようになりました。