Ruby 3.3.0 のインストール : 小林研 Rails Tips (43)

はじめに

Rails Tips の 43 回目です。ここまでさまざまなヘルパーを紹介してきたので、実際にこれらを使って Rails をどう構築していくのかを説明していきます。せっかくなので、Rails ガイドにある「Rails をはじめよう」のブログアプリを同じように作ってみたいと思います。

railsguides.jp

3.1.1 Ruby をインストールする

この前までは説明文なのでここから入ります。macOS Sonama でもデフォルトで入っている Ruby は 2.6.10 でした。このバージョンでは最新の Rails は入りません。どちらにしても Ruby は別途インストールが必要です。

$ /usr/bin/ruby -v
ruby 2.6.10p210 (2022-04-12 revision 67958) [universal.arm64e-darwin23]

私の場合は、homebrew でインストールした rbenv でインストールします。

==> rbenv: stable 1.2.0 (bottled), HEAD
Ruby version manager
https://github.com/rbenv/rbenv#readme
/opt/homebrew/Cellar/rbenv/1.2.0 (35 files, 125.1KB) *
  Poured from bottle on 2021-11-28 at 16:32:29
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/r/rbenv.rb
License: MIT
==> Dependencies
Required: ruby-build ✔

rbenv は ruby-build に依存しています。こちらは Ruby のアップデートがあるごとに頻繁に更新されます。毎年クリスマスにメジャーアップデートされます。ちょうど12/25 に 3.3.0 が出たばかりなので、ruby-build のバージョンも 20231225 となっています。ビルドに必要なものは依存関係でちゃんとインストールされています。

==> ruby-build: stable 20231225 (bottled), HEAD
Install various Ruby versions and implementations
https://github.com/rbenv/ruby-build
/opt/homebrew/Cellar/ruby-build/20231225 (595 files, 313.4KB) *
  Poured from bottle using the formulae.brew.sh API on 2023-12-27 at 08:03:41
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/r/ruby-build.rb
License: MIT
==> Dependencies
Required: autoconf ✔, libyaml ✔, pkg-config ✔, readline ✔, openssl@3 ✔

rbenv install -l とするとインストールできる候補が表示されます。以前は大量に表示されていたのですが、最近は最近のもののみが表示されるようになっています。 --list-all にすると以前のような全リストが表示されます。

rbenv install -l
3.0.6
3.1.4
3.2.2
3.3.0
jruby-9.4.5.0
mruby-3.2.0
picoruby-3.0.0
truffleruby-23.1.1
truffleruby+graalvm-23.1.1

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

すでに入っていたのですが、試しに上書きでインストールしてみました。

env RUBY_CONFIGURE_OPTS="--with-openssl-dir=$(brew --prefix openssl@3)" rbenv install 3.3.0
rbenv: /Users/hkob/.rbenv/versions/3.3.0 already exists
continue with installation? (y/N) y
==> Downloading ruby-3.3.0.tar.gz...
-> curl -q -fL -o ruby-3.3.0.tar.gz https://cache.ruby-lang.org/pub/ruby/3.3/ruby-3.3.0.tar.gz
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 21.0M  100 21.0M    0     0  31.6M      0 --:--:-- --:--:-- --:--:-- 31.5M
==> Installing ruby-3.3.0...
ruby-build: using readline from homebrew
ruby-build: using libyaml from homebrew
ruby-build: using gmp from homebrew
-> ./configure "--prefix=$HOME/.rbenv/versions/3.3.0" --enable-shared --with-readline-dir=/opt/homebrew/opt/readline --with-libyaml-dir=/opt/homebrew/opt/libyaml --with-gmp-dir=/opt/homebrew/opt/gmp --with-ext=openssl,psych,+ --with-openssl-dir=/opt/homebrew/opt/openssl@3
-> make -j 8
-> make install
==> Installed ruby-3.3.0 to /Users/hkob/.rbenv/versions/3.3.0

以下のようにすると 3.3.0 がグローバルに利用可能になります。

rbenv global 3.3.0

確認するとちゃんと 3.3.0 になっていました。

ruby -v
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [arm64-darwin23]

おわりに

実際には ruby は dotfiles の bootstrap.sh でインストールしているので、手動でインストールすることはないです。このスクリプト内の Ruby の バージョンだけを書き換え、このスクリプトを実行しています。Rails と言いながら Ruby のインストールでページが埋まってしまいました。明日は Rails をインストールするところに入りたいと思います。