M1 の MacBook で rbenv + Ruby 2.6.6 をビルドする

rbenv で Ruby 2.7 や 3.0 のビルドは問題なかったんですが 2.6 で失敗したのでその対処方法を書いておきます。

rbenv で 2.6.6 をインストールする

rbenv で 2.6.6 をインストールしようとしたらエラーになります。

$ rbenv install 2.6.6
Downloading openssl-1.1.1i.tar.gz...
-> https://dqw8nmjcqpjn7.cloudfront.net/e8be6a35fe41d10603c3cc635e93289ed00bf34b79671a3a4de64fcee00d5242
Installing openssl-1.1.1i...
Installed openssl-1.1.1i to /Users/anzu/.rbenv/versions/2.6.6

Downloading ruby-2.6.6.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.6/ruby-2.6.6.tar.bz2
Installing ruby-2.6.6...
ruby-build: using readline from homebrew

BUILD FAILED (macOS 11.1 using ruby-build 20201225-2-g5de6d5f)

Inspect or clean up the working tree at /var/folders/j_/4snw29l52s5g_hwy5fxbvqsw0000gp/T/ruby-build.20210102215022.60171.Wp1e2j
Results logged to /var/folders/j_/4snw29l52s5g_hwy5fxbvqsw0000gp/T/ruby-build.20210102215022.60171.log

Last 10 log lines:
compiling zlib.c
compiling normalize.c
compiling modify.c
compiling set_len.c
compiling enc_str_buf_cat.c
compiling new.c
linking shared-object -test-/string.bundle
linking shared-object date_core.bundle
linking shared-object zlib.bundle
make: *** [build-ext] Error 2

実際のコンパイルエラーは以下な感じです。

compiling nofree.c
linking shared-object -test-/wait_for_single_fd.bundle
linking shared-object -test-/vm/at_exit.bundle
compiling closure.c
compiling zlib.c
compiling ellipsize.c
installing default libraries
compiling conversions.c
closure.c:264:14: error: implicit declaration of function 'ffi_prep_closure' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
    result = ffi_prep_closure(pcl, cif, callback, (void *)self);
             ^
compiling normalize.c
1 error generated.
make[2]: *** [closure.o] Error 1
make[2]: *** Waiting for unfinished jobs....
linking shared-object date_core.bundle
compiling psych_to_ruby.c
compiling escape.c
compiling stringio.c
compiling modify.c
make[1]: *** [ext/fiddle/all] Error 2
make[1]: *** Waiting for unfinished jobs....

ffi 周りでコケてるっぽい…。

RUBY_CFLAGS=-DUSE_FFI_CLOSURE_ALLOC rbenv install 2.6.6 でインストールする

困ってたら以下の issues を教えてもらってそこに書いてあった RUBY_CFLAGS=-DUSE_FFI_CLOSURE_ALLOC rbenv install 2.6.6 で無事にインストールすることができました。

$ RUBY_CFLAGS=-DUSE_FFI_CLOSURE_ALLOC rbenv install 2.6.6
rbenv: /Users/anzu/.rbenv/versions/2.6.6 already exists
continue with installation? (y/N) y
Downloading openssl-1.1.1i.tar.gz...
-> https://dqw8nmjcqpjn7.cloudfront.net/e8be6a35fe41d10603c3cc635e93289ed00bf34b79671a3a4de64fcee00d5242
Installing openssl-1.1.1i...
Installed openssl-1.1.1i to /Users/anzu/.rbenv/versions/2.6.6

Downloading ruby-2.6.6.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.6/ruby-2.6.6.tar.bz2
Installing ruby-2.6.6...
ruby-build: using readline from homebrew
Installed ruby-2.6.6 to /Users/anzu/.rbenv/versions/2.6.6

ありがてえありがてえ…。