You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MacOS Catalina下Ruby版本切换失败及CocoaPods安装问题求助

macOS Catalina 10.15.7 安装CocoaPods问题解决方案

环境:MacBook Pro,macOS Catalina 10.15.7


1. Ruby版本切换问题

问题现象

全局已切换至2.7.5,但ruby -v仍显示系统自带的2.6.3,执行sudo rbenv local 2.7.5报错只读文件系统:

user@MacBook-Pro-de-User / % rbenv versions
  system
* 2.7.5 (set by /Users/user/.rbenv/version)
  mruby-3.0.0

user@MacBook-Pro-de-User / % ruby -v
ruby 2.6.3p62 (2019-04-16 revision 67580) [universal.x86_64-darwin19]

user@MacBook-Pro-de-User / % sudo rbenv local 2.7.5
Password:
/usr/local/Cellar/rbenv/1.2.0/libexec/rbenv-version-file-write: line 19: .ruby-version: Read-only file system

解决方案

  • 不要在根目录(/)设置本地Ruby版本,根目录受系统SIP保护无法写入.ruby-version文件。切换到你的项目目录下执行rbenv local 2.7.5即可,无需加sudo。
  • 检查rbenv的环境变量配置,确保~/.bash_profile或~/.zshrc里包含以下内容:
    export PATH="$HOME/.rbenv/bin:$PATH"
    eval "$(rbenv init -)"
    
    配置后重启终端,再执行ruby -v验证版本是否生效。

2. CocoaPods安装失败问题

问题1:sudo gem install cocoapods原生扩展构建失败

报错信息

user@MacBook-Pro-de-User / % sudo gem install cocoapods
Password:
Building native extensions. This could take a while...
ERROR:  Error installing cocoapods:
    ERROR: Failed to build gem native extension.

    current directory: /Library/Ruby/Gems/2.6.0/gems/ffi-1.16.3/ext/ffi_c
/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 ./siteconf20240111-988-w3p1cs.rb extconf.rb
*** 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.

Provided configuration options:
    --with-opt-dir
    --without-opt-dir
    --with-opt-include
    --without-opt-include=${opt-dir}/include
    --with-opt-lib
    --without-opt-lib=${opt-dir}/lib
    --with-make-prog
    --without-make-prog
    --srcdir=.
    --curdir
    --ruby=/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/bin/$(RUBY_BASE_NAME)
    --with-ffi_c-dir
    --without-ffi_c-dir
    --with-ffi_c-include
    --without-ffi_c-include=${ffi_c-dir}/include
    --with-ffi_c-lib
    --without-ffi_c-lib=${ffi_c-dir}/lib
    --enable-debug
    --disable-debug
    --enable-system-libffi
    --disable-system-libffi
    --with-libffi-config
    --without-libffi-config
    --with-pkg-config
    --without-pkg-config
/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/mkmf.rb:467:in `try_do': The compiler failed to generate an executable file. (RuntimeError)
You have to install development tools first.
    from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/mkmf.rb:546:in `block in try_link0'
    from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/tmpdir.rb:93:in `mktmpdir'
    from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/mkmf.rb:543:in `try_link0'
    from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/mkmf.rb:570:in `try_link'
    from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/mkmf.rb:672:in `try_ldflags'
    from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/mkmf.rb:1832:in `pkg_config'
    from extconf.rb:9:in `system_libffi_usable?'
    from extconf.rb:46:in `<main>'

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

  /Library/Ruby/Gems/2.6.0/extensions/universal-darwin-19/2.6.0/ffi-1.16.3/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /Library/Ruby/Gems/2.6.0/gems/ffi-1.16.3 for inspection.
Results logged to /Library/Ruby/Gems/2.6.0/extensions/universal-darwin-19/2.6.0/ffi-1.16.3/gem_make.out

mkmf.log内容

"pkg-config --exists libffi"
| pkg-config --libs libffi
=> "-lffi
"
"xcrun clang -o conftest -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/include/ruby-2.6.0/universal-darwin19 -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/include/ruby-2.6.0/ruby/backward -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/include/ruby-2.6.0 -I. -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE -D_DARWIN_UNLIMITED_SELECT -D_REENTRANT    -g -Os -pipe -DHAVE_GCC_ATOMIC_BUILTINS conftest.c  -L. -L/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib -L. -L/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.Internal.sdk/usr/local/lib   -arch x86_64   -lruby.2.6   "
In file included from conftest.c:1:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/include/ruby-2.6.0/ruby.h:33:
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.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.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.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 '/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/System/Library/Frameworks')
1 error generated.
checked program was:
/* begin */
1: #include "ruby.h"
2: 
3: int main(int argc, char **argv)
4: {
5:   return 0;
6: }
/* end */

解决方案

  • 优先使用rbenv管理的Ruby版本安装,避免修改系统Ruby:
    1. 确保rbenv的2.7.5版本已正确激活(执行rbenv global 2.7.5,重启终端后ruby -v显示2.7.5)
    2. 直接执行gem install cocoapods,不要加sudo,因为rbenv管理的Ruby在用户目录下,无需系统权限
  • 若仍需使用系统Ruby,修复SDK路径不匹配问题:
    1. 安装Xcode Command Line Tools并切换到对应SDK:
      xcode-select --install
      sudo xcode-select --switch /Library/Developer/CommandLineTools
      
    2. 设置SDK路径环境变量,临时指定Catalina的SDK:
      export SDKROOT=/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk
      sudo gem install cocoapods -- --with-cflags="-Wno-error=implicit-function-declaration"
      

问题2:brew install cocoapods卡在llvm构建步骤

报错信息

user@MacBook-Pro-de-User /usr % brew install llvm
    Warning: You are using macOS 10.15.
    We (and Apple) do not provide support for this old version.
    It is expected behaviour that some formulae will fail to build in this old version.
    It is expected behaviour that Homebrew will be buggy and slow.
    Do not create any issues about this on Homebrew's GitHub repositories.
    Do not create any issues even if you think this message is unrelated.
    Any opened issues will be immediately closed without response.
    Do not ask for help from Homebrew or its maintainers on social media.
    You may ask for help in Homebrew's discussions but are unlikely to receive a response.
    Try to figure out the problem yourself and submit a fix as a pull request.
    We will review it but may or may not accept it.
    
    ==> Fetching llvm
    ==> Downloading https://raw.githubusercontent.com/Homebrew/homebrew-core/a656e8d
    ######################################################################### 100.0%
    ==> Downloading https://github.com/llvm/llvm-project/releases/download/llvmorg-1
    Already downloaded: /Users/user/Library/Caches/Homebrew/downloads/0f45ee25df06c0fb1fa6d0d6df4677a9487c4b95f1020855dc61f95b532eb9c0--llvm-project-17.0.6.src.tar.xz
    ==> cmake -G Ninja .. -DLLVM_ENABLE_PROJECTS=clang;clang-tools-extra;lld;lldb;ml
    ==> cmake --build . 

解决方案

  • 由于Catalina已不在Homebrew支持范围内,新版本llvm构建耗时极长甚至失败,建议安装兼容Catalina的旧版本CocoaPods:
    brew install cocoapods@1.11.3
    
  • 若必须用brew安装最新版,可尝试增加内存分配(如果是虚拟机)或耐心等待,llvm构建本身需要大量资源,Catalina硬件性能可能不足以快速完成。

内容的提问来源于stack exchange,提问作者Jono

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 09:33:09