cli: CLI doesn't detect Cococapods installed via rbenv.

Environment

> rbenv --version
rbenv 1.2.0

> ruby --version
ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [x86_64-darwin21]

> pod --version
1.11.3

> which pod
/Users/christopher.araujo/.rbenv/shims/pod

> node --version
v16.16.0

Description

The React Native CLI does not seem to detect Cocoapods installed via RubyGems managed by rbenv. The CLI prompts me to install Cocoapods. However, selecting Yes, with gem (may require sudo) throws an error.

✔ CocoaPods (https://cocoapods.org/) is not installed. CocoaPods is necessary for the iOS project to run correctly. Do you want to install it? › Yes, with gem (may require sudo)
✖ Installing CocoaPods
error 
✖ Installing CocoaPods
error Error: An error occured while trying to install CocoaPods, which is required by this template.
Please try again manually: sudo gem install cocoapods.

Reproducible Demo

Run npx react-native init MyFirstRNProject on a machine that has Cocoapods installed via RubyGems managed by rbenv.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 6
  • Comments: 21 (4 by maintainers)

Most upvoted comments

For anyone else having problems with rbenv and cocoapods, I got through this by:

  1. uninstalling cocoapods gem and reinstalling through brew https://stackoverflow.com/questions/64901180/how-to-run-cocoapods-on-apple-silicon-m1
  2. I aliased pod since rbenv shim kept taking over and I’m not sure I want to go around deleting all the cocoapods installed on different ruby versions.alias pod="/opt/homebrew/bin/pod"

After running npx react-native init Project, I see there is a .ruby-version file in the project that is setting the ruby version to 2.7.5. I do not have this version of ruby installed. I have been using 3.0.2. Seems like this could be an issue if react-native cli is supposed to use your rbenv selected ruby version.

I just gave up installing my project with RN version 0.70. Impossible to install Bundler because of the Ruby version, impossible to make any Ruby version manager work

also having this problem. already have cocoapods installed on version of ruby that is set via rbenv. m1 mac. running sudo gem install cocoapods does not help.

I was trying to be a good little boy by installing a Ruby version manager (rbenv) like the docs suggest, even though I know I didn’t install a Ruby manager in the past. Cue a multi-hour-long nightmare. In the end I uninstalled rbenv, and my problems magically disappeared:

rbenv versions (identify the non-system Ruby version #) sudo rbenv uninstall 2.7.6 (of course replace 2.7.6 with your non-system Ruby version #) rbenv global system rbenv local system rbenv versions (confirm the system Ruby version is selected & the non-system version is uninstalled/unlisted) brew uninstall rbenv ~~https://stackoverflow.com/a/63885864/3511695~~

What a waste of time. Will avoid using Ruby version managers in the future.

Also, at the time of writing, the ruby-version file they link to in their official docs currently says Ruby 2.7.6, so that’s the version I installed with rbenv because, again, that’s what the docs told me to do. But when I ran react-native init, the ruby version that was placed into the project’s .ruby-version file was instead 2.7.5. So that was infuriating. (Maybe if I instead installed 2.7.5 with rbenv like the docs should have told me to, I wouldn’t have run into these issues in the first place.)

In place of linking to the ruby-version file on the live main branch in their Environment Setup doc, they should instead be linking to the ruby-version file from the latest RN release because (of course) the latest version of RN is what the npx react-native init command is going to end up using!! (Just created a Pull Request)

EDIT: Yeah I had to start over with 0.70.4 and just installed the the ruby-version file from the latest RN release instead of the ruby-version file on the live main branch and it worked

As other people mentioned, if you are using rbenv (highly recommend) and your list of installed Rubies does not contain the version from .ruby-version file then CLI instead of telling you that your environment does not have a Ruby version required by a template just tries to find pod executable so the the output of CLI is misleading. There is a trick: if you are quick enough you can run npx react-native init and right after that go to the directory and edit the .ruby-version file so that it contain a version that you have installed 😂 So by the time when CLI will try to install pods the .ruby-version content will be correct for your env. Ideally CLI should test whether required Ruby version present.

Just install ruby using rvm instead of rbenv its life saving. https://nrogap.medium.com/install-rvm-in-macos-step-by-step-d3b3c236953b

Maybe cli scripts should detect the real ruby-version in the machine, not just dump it from the templates.

@nscribble that would negate the point - it’s specifying a version of ruby that’s known to work with the template/project. Changing it for whatever is installed on the system may mean it doesn’t function.

If you’re not using rbenv, rvm, etc then the .ruby-version file won’t do anything anyway.

@liamjones Right~ The problem is, the cli would not download ruby or cocoapods of specific version. Maybe the CLI scripts could do a little more…