M1 MacBook无法安装json gem v2.6.2问题求助
M1 MacBook安装json gem v2.6.2失败问题解决
问题重现
执行命令:
sudo gem install json -v '2.6.2'
出现错误:
Building native extensions. This could take a while... ERROR: Error installing json: ERROR: Failed to build gem native extension. current directory: /Library/Ruby/Gems/2.6.0/gems/json-2.6.2/ext/json/ext/generator /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/bin/ruby -I /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0 -r ./siteconf20221018-37799-1wcwkww.rb extconf.rb creating Makefile current directory: /Library/Ruby/Gems/2.6.0/gems/json-2.6.2/ext/json/ext/generator make "DESTDIR=" clean current directory: /Library/Ruby/Gems/2.6.0/gems/json-2.6.2/ext/json/ext/generator make "DESTDIR=" compiling generator.c In file included from generator.c:1: In file included from ./../fbuffer/fbuffer.h:5: In file included from /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.0.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/include/ruby-2.6.0/ruby.h:33: /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.0.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/include/ruby-2.6.0/ruby/ruby.h:24:10: fatal error: 'ruby/config.h' file not found #include "ruby/config.h" ^~~~~~~~~~~~~~~ /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.0.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/include/ruby-2.6.0/ruby/ruby.h:24:10: note: did not find header 'config.h' in framework 'ruby' (loaded from '/Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks') 1 error generated. make: *** [generator.o] Error 1 make failed, exit code 2 Gem files will remain installed in /Library/Ruby/Gems/2.6.0/gems/json-2.6.2 for inspection. Results logged to /Library/Ruby/Gems/2.6.0/extensions/universal-darwin-21/2.6.0/json-2.6.2/gem_make.out
用户怀疑是Xcode 14.1 Beta 3导致兼容问题,但不清楚如何切换Xcode版本,也不确定是否为该原因。
解决方案
1. 确认问题根源
错误提示中找不到ruby/config.h,确实和Xcode Beta版本的SDK兼容性有关——Beta版SDK对系统Ruby(2.6.0)的头文件支持存在缺失,导致编译失败。
2. 切换到正式版Xcode
如果已安装正式版Xcode,执行以下步骤切换:
- 查看当前Xcode路径:
xcode-select -p - 切换到正式版(默认路径为
/Applications/Xcode.app):sudo xcode-select -s /Applications/Xcode.app/Contents/Developer - 验证切换结果:
输出应为xcode-select -p/Applications/Xcode.app/Contents/Developer
若未安装正式版,建议从App Store下载最新正式版,安装后重复上述切换步骤。
3. 不切换Xcode的临时修复
如果暂时无法切换Xcode版本,可通过指定编译参数强制安装:
sudo gem install json -v '2.6.2' -- --with-cflags="-Wno-error=implicit-function-declaration" --with-ldflags="-L/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib" --with-include-dir=/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/include
4. 长期解决方案:使用Ruby版本管理器
系统Ruby版本老旧且依赖系统环境,兼容性差。推荐用rbenv或rvm管理独立Ruby版本:
- 安装rbenv:
brew install rbenv rbenv init - 重启终端后,安装兼容的Ruby版本:
rbenv install 2.7.6 # 可选择其他适配版本 rbenv global 2.7.6 - 直接安装json gem:
gem install json -v '2.6.2'
内容的提问来源于stack exchange,提问作者Berry Blue
相关产品推荐
相关产品推荐

