memo.xight.org

日々のメモ

Apple Silicon + anyenv + rbenv + ruby2.7.2

Summary

rbenv + ruby-build で ruby をインストールすると arm64-apple が認識できない。

checking for ruby... $HOME/.anyenv/envs/rbenv/shims/ruby
tool/config.guess already exists
tool/config.sub already exists
checking build system type... Invalid configuration `arm64-apple-darwin20.2.0': machine `arm64-apple' not recognized
configure: error: /bin/sh tool/config.sub arm64-apple-darwin20.2.0 failed


tool/config.subにpatch をあててインストール ([2021-04-16] 追記)

timestamp='2019-06-30' のconfig.sub を timestamp='2021-01-08' に変更するパッチをあてる
$ rbenv install --patch 2.7.2 <<(curl -sSL https://gist.githubusercontent.com/xight/2518f1c28d28a1925d80e786ae34b1c2/raw/5b52a565add497c7e311808da838160d05ee7cc4/config.sub.patch)


tool/config.sub を入れ替えて configure (ad hoc)

$ cd /var/folders/PATH/TO/ruby-build.YYYYMMDDhhmmss.xxxxx.XXXXXX/ruby-2.7.2/tool
$ mv config.sub config.sub.bak
$ wget https://raw.githubusercontent.com/gcc-mirror/gcc/master/config.sub
$ cd ..
$ ./configure --prefix=$(rbenv root)/versions/2.7.2-arm64
$ make
$ make install


確認

$ rbenv global 2.7.2-arm64
$ ruby --version
ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [arm64-darwin20]
$ lipo -archs $(rbenv prefix)/bin/ruby
arm64


問題

x86_64 版を利用すると、
$GEM_HOME/*/*.bundleで x86 と arm64 で競合が起こる。

$GEM_HOME/gems/psych-3.2.1/lib/psych.bundle: mach-o, but wrong architecture


x86版の .bundle ファイル を退避してアップデート
$ mv $GEM_HOME/gem $GEM_HOME/gem-x86_64
$ gem update

Apple Silicon + Homebrew

Summary

TimeMachineでx86版からARM版に移行したときにハマった点をメモ。
アクティビティモニターを右クリックしてアーキテクチャを表示しておくと良い。

ARM版 Homebrewのインストール

ARM版 Homebrewは /opt/homebrew にインストールするのが推奨されている。
$ cd /opt
$ sudo mkdir homebrew
$ sudo chown $(whoami):admin homebrew
$ curl -L https://github.com/Homebrew/brew/tarball/master | tar xz --strip 1 -C homebrew
$ /opt/homebrew/bin/brew update


ハマったところ1: uname

uname でアーキテクチャを確認できていなかった
Homebrewでx86版のcoreutilsをインストールしていたため。
$ uname -m
x86_64
$ which uname
/usr/local/opt/coreutils/libexec/gnubin/uname

$ /usr/bin/uname -m
arm64


ハマったところ2: x86版のRuby gems

/Library/Ruby/Gems/2.6.0/gems に x86版のRuby lib/*.bundle ファイルが残ってたため, it is not a compatible arch
TimeMachine で移行するときにありがちっぽい。

$ /opt/homebrew/bin/brew
Traceback (most recent call last):
	22: from /opt/homebrew/Library/Homebrew/brew.rb:31:in `<main>'
	21: from /opt/homebrew/Library/Homebrew/brew.rb:31:in `require_relative'
	20: from /opt/homebrew/Library/Homebrew/global.rb:23:in `<top (required)>'
	19: from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
	18: from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
	17: from /opt/homebrew/Library/Homebrew/vendor/bundle/ruby/2.6.0/gems/activesupport-6.1.0/lib/active_support/core_ext/numeric/time.rb:3:in `<top (required)>'
	16: from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
	15: from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
	14: from /opt/homebrew/Library/Homebrew/vendor/bundle/ruby/2.6.0/gems/activesupport-6.1.0/lib/active_support/duration.rb:3:in `<top (required)>'
	13: from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
	12: from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
	11: from /opt/homebrew/Library/Homebrew/vendor/bundle/ruby/2.6.0/gems/activesupport-6.1.0/lib/active_support/core_ext/array/conversions.rb:3:in `<top (required)>'
	10: from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
	 9: from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
	 8: from /opt/homebrew/Library/Homebrew/vendor/bundle/ruby/2.6.0/gems/activesupport-6.1.0/lib/active_support/xml_mini.rb:3:in `<top (required)>'
	 7: from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
	 6: from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
	 5: from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/time.rb:3:in `<top (required)>'
	 4: from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
	 3: from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
	 2: from /Library/Ruby/Gems/2.6.0/gems/date-3.0.0/lib/date.rb:4:in `<top (required)>'
	 1: from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require': dlopen(/Library/Ruby/Gems/2.6.0/gems/date-3.0.0/lib/date_core.bundle, 0x0009): could not use '/Library/Ruby/Gems/2.6.0/gems/date-3.0.0/lib/date_core.bundle' because it is not a compatible arch - /Library/Ruby/Gems/2.6.0/gems/date-3.0.0/lib/date_core.bundle (LoadError)


x86版のRuby gemsを退避
$ cd /Library/Ruby/Gems/2.6.0
$ sudo mv gems gems-bak
$ /opt/homebrew/bin/brew list


zshenvでpathを通しておく

if [[ "$OSTYPE" == "darwin"* ]] && [[ "$CPUTYPE" == "arm64" ]]; then
	path=(
		/opt/homebrew/bin(N-/)
		/opt/homebrew/opt/coreutils/libexec/gnubin(N-/)
		/opt/homebrew/opt/binutils/libexec/gnubin(N-/)
		/opt/homebrew/opt/findutils/libexec/gnubin(N-/)
		/opt/homebrew/opt/diffutils/libexec/gnubin(N-/)
		/opt/homebrew/opt/grep/libexec/gnubin(N-/)
		/opt/homebrew/opt/gnu-tar/libexec/gnubin(N-/)
		/opt/homebrew/opt/gnu-sed/libexec/gnubin(N-/)
		/opt/homebrew/opt/gawk/libexec/gnubin(N-/)
		/opt/homebrew/opt/openssl@1.1/bin(N-/)
		$path
	)
fi

if [[ "$OSTYPE" == "darwin"* ]] && [[ "$CPUTYPE" == "arm64" ]]; then
	manpath=(
		/opt/homebrew/share/man(N-/)
		/opt/homebrew/opt/coreutils/libexec/gnuman(N-/)
		/opt/homebrew/opt/binutils/libexec/gnuman(N-/)
		/opt/homebrew/opt/findutils/libexec/gnuman(N-/)
		/opt/homebrew/opt/diffutils/libexec/gnuman(N-/)
		/opt/homebrew/opt/grep/libexec/gnuman(N-/)
		/opt/homebrew/opt/gnu-tar/libexec/gnuman(N-/)
		/opt/homebrew/opt/gnu-sed/libexec/gnuman(N-/)
		/opt/homebrew/opt/gawk/libexec/gnuman(N-/)
		/opt/homebrew/opt/openssl@1.1/man(N-/)
		$manpath
	)
fi


ARM版のパッケージをインストールし、archを確認

$ /opt/homebrwe/bin/brew install zsh
$ lipo -archs /opt/homebrew/bin/zsh
arm64


Reference

Homebrew Documentation
https://docs.brew.sh/Installation