LoginSignup
5
1

M1 Macでpsychがインストールできない問題と解消法

Posted at

私のOS状況

  • M1Macを使用
  • CPUアーキテクチャはarm64
$ uname -m
arm64

エラー状況

RailsをAPIモードで作成しようと以下のコマンドを実行

rails new uber-eats-like --api

すると

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

checking for yaml.h... yes
checking for yaml_get_version() in -lyaml... no
libyaml not found
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

To see why this extension failed to compile, please check the mkmf.log which can be found here:

/Users/[名前]/.rbenv/versions/3.0.5/lib/ruby/gems/3.0.0/extensions/arm64-darwin-22/3.0.0/psych-5.1.0/mkmf.log

//省略
An error occurred while installing psych (5.1.0), and Bundler cannot continue.

libyamlが見つからず、それに依存したpsych (5.1.0)がインストールがされず、エラーとなった。

解決手順

1.Homebrewを使用してlibyamlをインストール

brew install libyaml

2.psychをインストール

gem install psych

上記と同じエラーが発生。

3.エラー文にthe mkmf.logを見に行けとあるので行ってみた

To see why this extension failed to compile, please check the mkmf.log which can be found here:

/Users/[名前]/.rbenv/versions/3.0.5/lib/ruby/gems/3.0.0/extensions/arm64-darwin-22/3.0.0/psych-5.1.0/mkmf.log
mkmf.log
ld: warning: ignoring file /usr/local/lib/libyaml.dylib, building for macOS-arm64 but attempting to link with file built for macOS-x86_64

libyaml.をmacOS-arm64 用にビルドしていますが、macOS-x86_64 用にビルドされたファイルとリンクしようとしよとしているとのこと。

4.Homebrewでインストールしたlibyamlの情報を見てみる

brew info libyaml
1cb8a391276afb7--libyaml--0.2.5.arm64_ventura.bottle.tar.gz
==> Pouring libyaml--0.2.5.arm64_ventura.bottle.tar.gz
🍺  /opt/homebrew/Cellar/libyaml/0.2.5: 10 files, 351.3KB
==> Running `brew cleanup libyaml`...

Homebrewを使ってlibyamlのバージョン0.2.5をARM64アーキテクチャ(arm64_ventura)用にインストールし、それが成功していることがわかる。

どうゆうことなのか。。。。

5. libyamlのパスを指定して再試行

gem install psych -- --with-yaml-dir=/opt/homebrew/Cellar/libyaml/0.2.5
  • --with-yaml-dir=でlibyamlのライブラリとインクルードファイルがあるディレクトリを指定。

  • /opt/homebrew/Cellar/libyaml/0.2.5は、先程brew info libyamlで確認したlibyamlがインストールされているディレクトリです

psych-5.1.0のインストールに成功しました!

Building native extensions with: '--with-yaml-dir=/opt/homebrew/Cellar/libyaml/0.2.5'
This could take a while...
Successfully installed psych-5.1.0
Parsing documentation for psych-5.1.0
Done installing documentation for psych after 0 seconds
1 gem installed

6.再度、RailsをAPIモードで作成しようと以下のコマンドを実行

rails new uber-eats-like --api

エラーも出ずに作成できました!

まとめ

  • 依存関係: psychはYAMLデータを解析または生成するためのRubyのライブラリで、その動作にはlibyamlというCライブラリが必要だった。

  • ローカル環境: システム全体に影響を与えずに、特定のディレクトリにlibyamlをインストールしている場合や、複数バージョンのlibyamlが存在する場合に、どのlibyamlを使用するか明示的に指定する必要があった。

参考サイト

5
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
1