10

Outside of Xcode I use a specific version of Ruby, using RVM to manage multiple Ruby installations.

Apple's command line dev tools install Ruby at /usr/bin/ruby and is version 1.8.7.

I use 1.9.3 through RVM.

Is there a way to force Xcode to use my 1.9.3 installation when running its Run Script build phases?

I already tried setting the Shell path to the full path of my specific Ruby, but that didn't seem to make a difference, by which I mean that the particular Gems I have installed in my 1.9.3 weren't available/visible to the script when run within Xcode.

If I run my project through xcodebuild on the command line, the Run Script phase uses my specific Ruby because it's being run from within my shell environment (even if the Shell path in the project file is set to /usr/bin/ruby, it still uses my 1.9.3).

What can I do to make the IDE use my 1.9.3 Ruby install?

Clay Bridges
  • 11,602
  • 10
  • 68
  • 118
Jasarien
  • 58,279
  • 31
  • 157
  • 188
  • Wouldn't creating a symbolic link do the trick? For instance: `ln -s $HOME/.rvm/bin/rvm-auto-ruby /usr/bin/ruby` – fmendez Mar 18 '13 at 11:39
  • I suppose it could, but I want to leave the default installation intact if possible - and I'd like a solution that would work on another machine without having to mess with its default install as well (installing RVM would be required obviously, but if I can leave the /usr/bin install alone that's ideal) – Jasarien Mar 18 '13 at 11:46

2 Answers2

6

I had the same (well, worse) problem, and the code that follows worked for me. The key thing to realize is that, on the command line, you are using <something>/bin/rvm, but in a shell script, in order for rvm to change that environment, you must use a function, and you must first load that function to your shell script by calling source <something>/scripts/rvm. More on all this here.

This code is also gisted.

#!/usr/bin/env bash

# Xcode scripting does not invoke rvm. To get the correct ruby,
# we must invoke rvm manually. This requires loading the rvm 
# *shell function*, which can manipulate the active shell-script
# environment.
# cf. http://rvm.io/workflow/scripting

# Load RVM into a shell session *as a function*
if [[ -s "$HOME/.rvm/scripts/rvm" ]] ; then

  # First try to load from a user install
  source "$HOME/.rvm/scripts/rvm"

elif [[ -s "/usr/local/rvm/scripts/rvm" ]] ; then

  # Then try to load from a root install
  source "/usr/local/rvm/scripts/rvm"
else

  printf "ERROR: An RVM installation was not found.\n"
  exit 128
fi

# rvm will use the controlling versioning (e.g. .ruby-version) for the
# pwd using this function call.
rvm use .

As a protip, I find embedding shell code in a project.pbxproj file yucky. For all but the most trivial stuff, my actual run script step is usually just a one-line call out to an external script:

enter image description here

Clay Bridges
  • 11,602
  • 10
  • 68
  • 118
  • 1
    Super helpful, if it wasn't for the link that you added (for more details), it'd have taken me a really long time to figure this out. Also, FWIW, rvm adds those lines to people's bash_profile/zshrc files as follows: ``` [[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" ``` So I added this to my code: ``` # Load rvm into a shell session *as a function* [[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Handles rvm installed as user [[ -s "/usr/local/rvm/scripts/rvm" ]] && source "/usr/local/rvm/scripts/rvm" # Handles rvm installed as sudo ``` – Roger Oba Mar 18 '20 at 23:57
4

Try this at the beginning of your script in Xcode:

source "$HOME/.rvm/scripts/rvm"
shoumikhin
  • 1,327
  • 15
  • 23