Rubyの設定



●Ruby 2.5をインストールする

 参考URL:Ruby 2.5 : インストール

 Ruby 2.5をインストールします。
 現在、有効になっているRubyのバージョンを確認してからインストールします。
# dnf module list ruby
milter-manager_repos                                                                                422  B/s | 833  B     00:01    
milter-manager_repos-source                                                                         530  B/s | 833  B     00:01    
Rocky Linux 8 - AppStream
Name               Stream                  Profiles                Summary                                                          
ruby               2.5 [d]                 common [d]              An interpreter of object-oriented scripting language             
ruby               2.6                     common [d]              An interpreter of object-oriented scripting language             
ruby               2.7                     common [d]              An interpreter of object-oriented scripting language             
ruby               3.0                     common [d]              An interpreter of object-oriented scripting language             

ヒント: [d]efault, [e]nabled, [x]disabled, [i]nstalled
 Ruby 2.5 を指定してインストールします。
# dnf module -y install ruby:2.5/common

# ruby -v
ruby 2.5.9p229 (2021-04-05 revision 67939) [x86_64-linux]
 正常にインストールできたか確認するため、試験用スクリプトを作成し、動作確認します。
# cat > ruby_test.rb <<'EOF' 
msg = Class.send(:new, String);
mymsg = msg.send(:new, "Hello Ruby World !\n");
STDOUT.send(:write, mymsg)
EOF

# ruby ruby_test.rb
Hello Ruby World !

# rm -f ruby_test.rb


●Rubyのインストール(tar.gz)

 RubyのWebサイト(http://www.ruby-lang.org/)では、最新版のソースファイルが.gz形式で配布されているので、適当なディレクトリ(今回は「/usr/local/src」)にダウンロードします。
 インストールの手順は、READMEなどのファイルに記述されていることが多いので、必ず目を通す必要があります。
 Rubyの場合は、以下のような手順でインストールを行いますが、プログラムによってはオプションが必要なケースもあります(例えば、日本語版のsambaの場合は./configureにオプションが必要)。
$ cd /usr/local/src
$ tar zxf ruby-1.8.7-p160.tar.gz
$ cd ruby-1.8.7-p160
$ ./configure
$ make
$ su
# make install
# ruby -v
ruby 1.8.7 (2009-04-08 patchlevel 160) [i686-linux]


●CentOS 7.5にRuby 2.4.4をインストールする

 参考URL:インストールしたはずのrubyやgemが見つからなかったり古かったりした場合の確認事項
 参考URL:CentOS 7.3にRuby 2.4.1をインストールする

 CentOSのバージョンを確認します。
$ cat /etc/redhat-release 
CentOS Linux release 7.5.1804 (Core) 
 rbenvをインストールする事前準備をします。
 ※この作業の場合、Rubyは作業ユーザのホームディレクトリにインストールされますでのご注意下さい。
$ git clone https://github.com/rbenv/rbenv.git ~/.rbenv
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
$ ~/.rbenv/bin/rbenv init
$ source ~/.bash_profile
 rbenvがインストールされたことを確認します。
$ rbenv -v
rbenv 1.1.1-37-g1c772d5
 ruby-buildをインストールします。
$ git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
$ sudo ~/.rbenv/plugins/ruby-build/install.sh
$ rbenv install -l
 Rubyをインストールします。
$ sudo yum -y install openssl-devel readline-devel zlib-devel
$ rbenv install 2.4.4
$ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
$ source ~/.bash_profile
$ rbenv global 2.4.4 または $ rbenv local 2.4.4
 Rubyのバージョンを確認します。
$ ruby -v
ruby 2.4.4p296 (2018-03-28 revision 63013) [x86_64-linux]
 もし最新のバージョンが表示されない場合、一度ログアウト・ログインしてください。
 rbenv localを実行した場所では.ruby-versionというファイルが作成され、その中身はrbenv localで指定したバージョンになっています。
$ cd ~
$ cat .ruby-version 
2.4.4


●Ruby 2.7.6を指定したディレクトリにインストールし、Ruby環境を構築する

 参考URL:rbenvとruby環境の構築手順
 参考URL:rbenv+ruby-buildをインストールする手順 (CentOS/RedHat)
 参考URL:rbenvをインストールする-Ruby
 参考URL:rbenvでディレクトリ毎にRubyのバージョンを指定する

 OSはRocky Linux 8.7です。
 セットアップ当時、何も考えずRubyをインストールしたため、バージョン2.5がインストールされていました。
 新しいバージョンのRubyを利用しないと動作しないソフトが出てきたため、元のRubyはそのままで、新バージョンをインストールする必要がありました。

 rbenvを特段意識せずにインストールした場合、作業ユーザのホームディレクトリにインストールされます。
 その場合、他のユーザがRubyを利用出来ないなどの問題が発生します。
 その問題を回避するため、rbenvを/optにインストールするようにします。

 下記は、rbenvを/optにインストールする手順です。
 GitHubからrbenvを/opt/rbenvにダウンロード (clone) します。
# cd /opt
# git clone https://github.com/rbenv/rbenv.git ./rbenv
Cloning into 'rbenv'...
remote: Enumerating objects: 3154, done.
remote: Counting objects: 100% (304/304), done.
remote: Compressing objects: 100% (170/170), done.
remote: Total 3154 (delta 172), reused 232 (delta 122), pack-reused 2850
Receiving objects: 100% (3154/3154), 634.20 KiB | 1.41 MiB/s, done.
Resolving deltas: 100% (1962/1962), done.
 /etc/profileにrbenvへパスを通す設定と初期化を行うための行を追記します。
# echo 'export RBENV_ROOT="/opt/rbenv"' >> /etc/profile
# echo 'export PATH="${RBENV_ROOT}/bin:${PATH}"' >> /etc/profile
# echo 'eval "$(rbenv init -)"' >> /etc/profile
 ruby-buildは、rbenv installコマンドを提供するrbenvのプラグインです。
 ruby-buildプラグインをrbenvをインストールしたディレクトリ内のpluginsフォルダにインストールします。
# mkdir /opt/rbenv/plugins
# cd /opt/rbenv/plugins
# git clone https://github.com/rbenv/ruby-build.git ./ruby-build
Cloning into 'ruby-build'...
remote: Enumerating objects: 13568, done.
remote: Counting objects: 100% (2255/2255), done.
remote: Compressing objects: 100% (341/341), done.
remote: Total 13568 (delta 2018), reused 2031 (delta 1890), pack-reused 11313
Receiving objects: 100% (13568/13568), 2.71 MiB | 2.43 MiB/s, done.
Resolving deltas: 100% (9268/9268), done.
 これでrbenv installが使えるようになりました
 環境を確認するにはrbenv-doctorを利用します。
$ curl -fsSL https://github.com/rbenv/rbenv-installer/raw/main/bin/rbenv-doctor | bash
Checking for `rbenv' in PATH: multiple
  You seem to have multiple rbenv installs in the following locations.
  Please pick just one installation and remove the others.
  
  /opt/rbenv/bin/rbenv
  /usr/bin/rbenv

Checking for rbenv shims in PATH: OK
Checking `rbenv install' support: /opt/rbenv/plugins/ruby-build/bin/rbenv-install (ruby-build 20221225-9-g697bcff)
Counting installed Ruby versions: 1 versions
Auditing installed plugins: OK
 rbenvのバージョンを確認するには、下記コマンドを実行します。
$ rbenv -v
rbenv 1.2.0-52-g61747c0

 インストールできるrubyのバージョンを確認します。
# rbenv install -l
2.6.10
2.7.6
3.0.4
3.1.2
jruby-9.3.8.0
mruby-3.1.0
picoruby-3.0.0
rbx-5.0
truffleruby-22.2.0
truffleruby+graalvm-22.2.0

Only latest stable releases for each Ruby implementation are shown.
Use 'rbenv install --list-all / -L' to show all local versions.
 今回は2.7.6を選択します。
# rbenv install 2.7.6
-> https://cache.ruby-lang.org/pub/ruby/2.7/ruby-2.7.6.tar.bz2
Installing ruby-2.7.6...

WARNING: ruby-2.7.6 is nearing its end of life.
It only receives critical security updates, no bug fixes.

Installed ruby-2.7.6 to /opt/rbenv/versions/2.7.6


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

# rbenv global 2.7.6
# rbenv versions
  system
* 2.7.6 (set by /opt/rbenv/version)
 これで、Ruby 2.7.6を利用できる環境になりました。