M2 Mac Flutter环境搭建:CocoaPods已安装但无法使用求助
M2 MacBook Pro Flutter环境搭建:CocoaPods无效问题解决
问题背景
在M2芯片MacBook Pro上按Flutter官方iOS指南搭建环境时,执行flutter doctor报错:
[!] Xcode - develop for iOS and macOS (Xcode 16.2)
✗ CocoaPods installed but not working.
You appear to have CocoaPods installed but it is not working.
This can happen if the version of Ruby that CocoaPods was installed with is different from the one being used to invoke it.
This can usually be fixed by re-installing CocoaPods.
已尝试两种Stack Overflow方案,均通过ruby -v验证Ruby版本为3.4.2,但问题未解决:
尝试过的方案1(RVM管理Ruby)
# 原命令第一步存在笔误,正确应为curl curl -L https://get.rvm.io | bash -s stable rvm install ruby-3.4.2 rvm use ruby-3.4.2 rvm --default use 3.4.2 sudo gem install cocoapods
尝试过的方案2(Homebrew管理Ruby)
rvm uninstall ruby-3.4.2 sudo gem uninstall cocoapods /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" brew install ruby # 更新.zshrc,将PATH指向/opt/homebrew/opt/ruby/bin source ~/.zshrc brew install cocoapods
此外安装Rosetta 2时出现以下警告,但显示安装成功,不确定是否关联:
sudo softwareupdate --install-rosetta --agree-to-license Password: By using the agreetolicense option, you are agreeing that you have run this tool with the license only option and have read and agreed to the terms. If you do not agree, press CTRL-C and cancel this process immediately. 2025-03-12 03:01:12.577 softwareupdate[1089:11935] Package Authoring Error: 072-70655: Package reference com.apple.pkg.RosettaUpdateAuto is missing installKBytes attribute Install of Rosetta 2 finished successfully
针对性解决步骤(Apple Silicon专属)
1. 彻底清理现有环境
# 卸载RVM及残留 rvm implode sudo rm -rf ~/.rvm ~/.rvmrc /etc/rvmrc /etc/profile.d/rvm.sh # 完全卸载CocoaPods sudo gem uninstall -all cocoapods brew uninstall cocoapods --force # 删除残留文件 sudo rm -rf ~/.cocoapods sudo rm -rf /Library/Ruby/Gems/*/gems/cocoapods-* sudo rm -rf /usr/local/bin/pod /opt/homebrew/bin/pod
2. 使用系统Ruby+用户级Gem安装CocoaPods
Apple Silicon的系统Ruby无需额外升级,用用户级安装可避免权限冲突:
# 添加用户Gem路径到环境变量 echo 'export PATH="$HOME/.gem/ruby/$(ruby -e "puts RUBY_VERSION.match(/\d+\.\d+/)[0]")/bin:$PATH"' >> ~/.zshrc source ~/.zshrc # 安装CocoaPods到用户目录 gem install cocoapods --user-install # 验证安装 pod --version
3. 配置Xcode命令行工具
# 安装/更新Xcode命令行工具 xcode-select --install # 同意Xcode许可协议 sudo xcodebuild -license accept # 确认Xcode路径正确 sudo xcode-select -s /Applications/Xcode.app/Contents/Developer
4. 验证Rosetta 2(可选)
Rosetta 2的警告不影响功能,若需确认可重新安装:
sudo /usr/sbin/softwareupdate --remove-rosetta sudo softwareupdate --install-rosetta --agree-to-license
5. 最终验证
重启终端后执行:
flutter doctor
内容的提问来源于stack exchange,提问作者srinivas biramoni
相关产品推荐
相关产品推荐

