私物のmacをIntel製からM1に変えた環境にRubyをインストールするのにめちゃくちゃハマったので、メモしていきたいと思います。
環境
インストールしている環境はこちらです。
- Mac OS Monterey(12.0.1) (M1チップ)
- rbenv 1.2.0
- ruby-build 20211227
- インストールが成功したバージョン(2.6.7, 3.1.0, 3.0.2)
インストールする方法
Intel製のMacとM1ではインストールされるhomebrewが違うので、一度アンイストールして再インストールする必要がありました。
homebrewを再インストールしたらRubyのインストールに必要なものをインストールしていきます。僕の場合はこれだけあれば大丈夫でした。
- rbenv
- ruby-build
- openssl@1.1
- libffi
- readline
- zlib(必要だったかは不明)
これらをインストールしたあと、以下コマンドでインストール成功しました。
$ RUBY_CONFIGURE_OPTS="--with-openssl-dir=`brew --prefix openssl@1.1`" rbenv install 3.0.2
一応、インストールがうまくいった.bash_profileを晒しておきます。
eval "$(rbenv init -)"
# homebrewのパスを通す
eval "$(/opt/homebrew/bin/brew shellenv)"
# For compilers to find libffi you may need to set:
export LDFLAGS="-L/opt/homebrew/opt/libffi/lib"
export CPPFLAGS="-I/opt/homebrew/opt/libffi/include"
# For pkg-config to find libffi you may need to set:
export PKG_CONFIG_PATH="/opt/homebrew/opt/libffi/lib/pkgconfig"
# export PATH="/opt/homebrew/opt/openssl@3/bin:$PATH"
export PATH="/opt/homebrew/opt/openssl@1.1/bin:$PATH"
僕の場合はopenssl@3だとRuby3.1.0はインストールできたのですが、Ruby3.0.0以下だとインストールできなかったです。なので、openssl@1.1をインストールしてパスを通したところインストールができました。
また、このようなエラー文が出ている場合はライブラリのバージョンが古かったり、インストールされていないのが問題のことが多かったです。ログファイルが保存されているので、どのライブラリで落ちているのかを調べてそれでググったり、再インストールしながら解決していきました。
BUILD FAILED (macOS 12.0.1 using ruby-build 20211227)
Inspect or clean up the working tree at /var/folders/bh/qpjwyh5s3qd18bxhbd4sz_zh0000gn/T/ruby-build.20220101121429.5549.p0IER8
Results logged to /var/folders/bh/qpjwyh5s3qd18bxhbd4sz_zh0000gn/T/ruby-build.20220101121429.5549.log
Last 10 log lines:
^~~~~~~~~~~~~~~~~~~~~~~~
../.././include/ruby/ruby.h:1826:56: note: expanded from macro 'rb_intern'
__extension__ (RUBY_CONST_ID_CACHE((ID), (str))) : \
^
18 warnings generated.
30 warnings generated.
linking shared-object socket.bundle
12 warnings generated.
linking shared-object ripper.bundle
make: *** [build-ext] Error 2
一方、このようなエラー文の場合はインストールの際の環境変数の指定が足りていないときに起こっていたと思います。僕の場合はRUBY_CONFIGURE_OPTS
を指定してインストールすることでうまくいきました。
BUILD FAILED (macOS 12.0.1 using ruby-build 20211227)
Inspect or clean up the working tree at /var/folders/bh/qpjwyh5s3qd18bxhbd4sz_zh0000gn/T/ruby-build.20220101121055.84844.JyHOKR
Results logged to /var/folders/bh/qpjwyh5s3qd18bxhbd4sz_zh0000gn/T/ruby-build.20220101121055.84844.log
Last 10 log lines:
The Ruby openssl extension was not compiled.
ERROR: Ruby install aborted due to missing extensions
Configure options used:
--prefix=/Users/sakaishun/.rbenv/versions/2.6.7
--enable-shared
--with-readline-dir=/opt/homebrew/opt/readline
--with-openssl-dir=/opt/homebrew/opt/openssl@1.1
CC=clang
LDFLAGS=-L/Users/sakaishun/.rbenv/versions/2.6.7/lib -L/opt/homebrew/opt/libffi/lib
CPPFLAGS=-I/Users/sakaishun/.rbenv/versions/2.6.7/include -I/opt/homebrew/opt/libffi/include
詳しいことはわからないですが、参考になればうれしいです。
今年も一年よろしくお願いします!