cli icon indicating copy to clipboard operation
cli copied to clipboard

CLI doesn't detect Cococapods installed via rbenv.

Open dcaaraujo opened this issue 3 years ago • 13 comments

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.

dcaaraujo avatar Jul 13 '22 10:07 dcaaraujo

Improvements are very welcome!

thymikee avatar Jul 13 '22 10:07 thymikee

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.

heyalexchoi avatar Jul 13 '22 15:07 heyalexchoi

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.

heyalexchoi avatar Jul 13 '22 15:07 heyalexchoi

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"

heyalexchoi avatar Jul 13 '22 16:07 heyalexchoi

Thanks @heyalexchoi. I'll install Ruby 2.7.5 and see if that fixes the issue.

dcaaraujo avatar Jul 14 '22 07:07 dcaaraujo

Improvements are very welcome!

I'll give it a shot and see if I can figure out why it's not detecting it. I'm not a JavaScript dev so it might take me a while. 😅 The only strange thing I noticed when looking at the code is that I think isSoftwareNotInstalled might fail. For some reason the command-exists package doesn't detect pod either. However, from my understanding I think this function is only used with cli-doctor?

dcaaraujo avatar Jul 14 '22 07:07 dcaaraujo

I'll give it a shot and see if I can figure out why it's not detecting it. I'm not a JavaScript dev so it might take me a while.

@dcaaraujo At a guess, this might be the cause (since I've hit similar problems with nvm in shell scripts before):

When you set up rbenv you add an init line to your shell setup script. One of the things this does is add the shims directory to the front of the path, e.g. export PATH="/Users/liam.jones/.rbenv/shims:${PATH}". This means ruby, etc will be found via shims before any system version.

The way doctor is running system commands may start a new shell that doesn't have that change present. If you get the doctor command to output the $PATH (or which ruby) in the same manner it normally calls system commands you may find it's missing the extra path addition.

liamjones avatar Jul 14 '22 10:07 liamjones

After uninstall rvm and reinstall cocoapods by sudo gem install cocoapods, I successfully run rn on my iPhone simulator.

HaloWang avatar Jul 20 '22 03:07 HaloWang

@dcaaraujo You may just skip that "cocoapods installation" step, and change the .ruby-version to the ruby version in your environment. Next, cd into the iOS directory and pod install. Finally, in directory of the project, yarn ios would success.

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

nscribble avatar Jul 26 '22 03:07 nscribble

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 avatar Jul 26 '22 07:07 liamjones

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...

nscribble avatar Jul 29 '22 07:07 nscribble

Maybe it should suggest installing rbenv? Then it's just rbenv install (or something similar) in the project directory to install the requested version

liamjones avatar Jul 29 '22 07:07 liamjones

Maybe it should suggest installing rbenv? Then it's just rbenv install (or something similar) in the project directory to install the requested version

npx react-native init

nscribble avatar Jul 29 '22 10:07 nscribble

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.

Vyazovoy avatar Aug 19 '22 14:08 Vyazovoy

Thanks everyone. I ended up just moving to Cocoapods installed via Homebrew. However, I agree that a nice-to-have would be to test which Ruby version the user has installed and suggest installing something like rbenv.

I'll close this ticket because my issue was resolved.

dcaaraujo avatar Aug 24 '22 09:08 dcaaraujo

~~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

hb-webdev avatar Oct 28 '22 12:10 hb-webdev

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

rdhox avatar Oct 31 '22 18:10 rdhox

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

Agree, tried everything, doesnt seem to work. Sadly no proper documentation anywhere.

vaibhav834 avatar Nov 09 '22 17:11 vaibhav834

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

vaibhav834 avatar Nov 09 '22 18:11 vaibhav834

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

Thanks! I had to reinstall cocoapods for having the ruby version with rvm knowing the right path.

rdhox avatar Nov 18 '22 09:11 rdhox

just pitching in after I installed rbenv for the first time now - installed rbenv via brew, ran rbenv init, it tells you to put some eval into .zshrc, did it. ran rbenv install 3.2.2, rbenv global 3.2.2, restarted shell, ran gem install cocoapods, restarted shell, and all the paths work as expected

rtrembecky avatar Jan 05 '24 10:01 rtrembecky