M2 MacでRubyのバージョンアップ

ruby on rails の環境構築をしようとした際に、
## BUILD FAILED (macOS 14.3 on arm64 using ruby-build )
エラーに当たった。

原因は結局不明のままですが、とりあえず動かしたい人向けに記事を残します。


```bash

(base) % brew -v
Homebrew 4.2.16
(base) % rbenv
rbenv 1.2.0
Usage: rbenv <command> [<args>]

Some useful rbenv commands are:
   commands    List all available rbenv commands
   local       Set or show the local application-specific Ruby version
   global      Set or show the global Ruby version
   shell       Set or show the shell-specific Ruby version
   install     Install a Ruby version using ruby-build
   uninstall   Uninstall a specific Ruby version
   rehash      Rehash rbenv shims (run this after installing executables)
   version     Show the current Ruby version and its origin
   versions    List installed Ruby versions
   which       Display the full path to an executable
   whence      List all Ruby versions that contain the given executable

See `rbenv help <command>' for information on a specific command.
For full documentation, see: https://github.com/rbenv/rbenv#readme'
 % echo 'export RBENV_ROOT="$HOME/.rbenv"'            
export RBENV_ROOT="$HOME/.rbenv"
 % echo 'export RBENV_ROOT="$HOME/.rbenv"' >> ~/.zshrc
 % echo 'export PATH="$RBENV_ROOT/bin:$PATH"' >> ~/.zshrc

 % echo 'eval "$(rbenv init -)"' >>  ~/.zshrc
 % source ~/.zshrc

 % rbenv install --list

3.0.6
3.1.4
3.2.3
3.3.0
jruby-9.4.6.0
mruby-3.3.0
picoruby-3.0.0
truffleruby-24.0.0
truffleruby+graalvm-24.0.0

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

ruby-build: using openssl@3 from homebrew
==> Downloading ruby-3.2.3.tar.gz...
-> curl -q -fL -o ruby-3.2.3.tar.gz https://cache.ruby-lang.org/pub/ruby/3.2/ruby-3.2.3.tar.gz
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 19.6M  100 19.6M    0     0   427k      0  0:00:47  0:00:47 --:--:--  653k
==> Installing ruby-3.2.3...
ruby-build: using readline from homebrew
ruby-build: using libyaml from homebrew
ruby-build: using gmp from homebrew
-> ./configure "--prefix=$HOME/.rbenv/versions/3.2.3" --with-openssl-dir=/opt/homebrew/opt/openssl@3 --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,+

BUILD FAILED (macOS 14.3 on arm64 using ruby-build 20240319)

You can inspect the build directory at /var/folders/6d/736ycnrx2mnfrtcx0zmn3x2w0000gn/T/ruby-build.20240403143205.83749.ExMA19
See the full build log at /var/folders/6d/736ycnrx2mnfrtcx0zmn3x2w0000gn/T/ruby-build.20240403143205.83749.log
 ```
普通にinstallしようとしたらエラーを吐かれた。
さらに詳しく見てみる。

```bash

% cat /var/folders/6d/736ycnrx2mnfrtcx0zmn3x2w0000gn/T/ruby-build.20240403143205.83749.log
cd /var/folders/6d/736ycnrx2mnfrtcx0zmn3x2w0000gn/T/ruby-build.20240403143205.83749.ExMA19
==> Downloading ruby-3.2.3.tar.gz...
-> curl -q -fL -o ruby-3.2.3.tar.gz https://cache.ruby-lang.org/pub/ruby/3.2/ruby-3.2.3.tar.gz
-> tar xzf ruby-3.2.3.tar.gz
cd /var/folders/6d/736ycnrx2mnfrtcx0zmn3x2w0000gn/T/ruby-build.20240403143205.83749.ExMA19/ruby-3.2.3
==> Installing ruby-3.2.3...
-> ./configure "--prefix=$HOME/.rbenv/versions/3.2.3" --with-openssl-dir=/opt/homebrew/opt/openssl@3 --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,+
checking for ruby... /usr/bin/ruby
tool/config.guess already exists
tool/config.sub already exists
checking build system type... aarch64-apple-darwin23.3.0
checking host system type... aarch64-apple-darwin23.3.0
checking target system type... aarch64-apple-darwin23.3.0
checking for llvm-ar... llvm-ar
checking for clang++... clang++
checking for llvm-nm... llvm-nm
checking for llvm-objcopy... llvm-objcopy
checking for llvm-objdump... llvm-objdump
checking for llvm-ranlib... llvm-ranlib
checking for llvm-strip... llvm-strip
checking for gcc... clang
checking whether the C compiler works... no
configure: error: in `/var/folders/6d/736ycnrx2mnfrtcx0zmn3x2w0000gn/T/ruby-build.20240403143205.83749.ExMA19/ruby-3.2.3':
configure: error: C compiler cannot create executables
See `config.log' for more details
external command failed with status 77
% rbenv versions
* system

```


catでログを見てみるとどうやらcのコンパイルエラーが起きて実行ファイルが作れなかった。
config.logを一応見てみたが、exit 77とよくわからんのがいっぱいあっただけだった。

コンパイルにエラーがあるのが明らかなので、

[configure: error: C compiler cannot create executables ruby]

で検索をかけたところ、
CCのコンパイル方法を/usr/bin/gccに変えたら上手くできたとの記事があった。

```bash
[bash-3.2$ CC=/usr/bin/gcc rbenv install 3.3.0
ruby-build: using openssl@3 from homebrew
==> 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   633k      0  0:00:34  0:00:34 --:--:--  219k
==> Installing ruby-3.3.0...
ruby-build: using libyaml from homebrew
ruby-build: using gmp from homebrew
-> ./configure "--prefix=$HOME/.rbenv/versions/3.3.0" --with-openssl-dir=/opt/homebrew/opt/openssl@3 --enable-shared --with-libyaml-dir=/opt/homebrew/opt/libyaml --with-gmp-dir=/opt/homebrew/opt/gmp --with-ext=openssl,psych,+
-> make -j 8
-> make install
==> Installed ruby-3.3.0 to /Users/shucream/.rbenv/versions/3.3.0

bash-3.2$ ruby -v
ruby 3.1.4p223 (2023-03-30 revision 957bb7cb81) [arm64-darwin23]
bash-3.2$ rbenv global 3.3.0
bash-3.2$ ruby -v
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [arm64-darwin23]
bash-3.2$ 
```


cのbuildのMakefileがこれで動いて無事にrubyのバージョンアップが完了した。