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

macOS下React Native项目初始化遇Ruby Gems权限及bundler缺失问题

解决React Native初始化时Ruby Gems及Bundler相关错误

问题重现

初始化React Native项目时遇到以下错误:

Welcome to React Native!                
                Learn once, write anywhere               

✔ Downloading template
✔ Copying template
✔ Processing template
✖ Installing Ruby Gems

error /Library/Ruby/Site/2.6.0/rubygems.rb:264:in `find_spec_for_exe': can't find gem bundler (>= 0.a) with executable bundle (Gem::GemNotFoundException)
from /Library/Ruby/Site/2.6.0/rubygems.rb:283:in `activate_bin_path'
from /usr/bin/bundle:23:in `<main>'

✖ Installing Ruby Gems

error Looks like your iOS environment is not properly set. Please go to https://reactnative.dev/docs/environment-setup?os=macos&platform=android and follow the React Native CLI QuickStart guide for macOS and iOS.

info Run CLI with --verbose flag for more details.

解决方案

1. 替换系统Ruby,避免权限问题

系统自带Ruby的目录权限限制严格,直接用sudo gem install易引发权限冲突,推荐用Ruby版本管理器(如rbenv)搭建独立Ruby环境:

  • 用Homebrew安装rbenv:
    brew install rbenv
    
  • 初始化rbenv并配置shell:
    rbenv init
    
    按照提示将初始化命令添加到你的shell配置文件(~/.zshrc 或 ~/.bash_profile),之后重启终端。
  • 安装React Native兼容的Ruby版本(例如2.7.8):
    rbenv install 2.7.8
    
  • 设置全局Ruby版本:
    rbenv global 2.7.8
    
  • 验证版本:
    ruby -v
    

2. 安装Bundler

使用新的Ruby环境无需sudo,直接安装bundler:

gem install bundler

验证安装:

bundle -v

3. 修复iOS环境配置

  • 确保已安装Xcode,首次打开Xcode需完成协议同意和组件安装;
  • 安装CocoaPods:
    gem install cocoapods
    
  • 进入项目的ios目录,执行依赖安装:
    cd your-project/ios
    pod install
    

完成以上步骤后,重新执行React Native项目初始化命令即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 07:52:09