13

In xcode I have a 'run script' build phase that runs a ruby script. However, it seems that xcode is trying to run it using the default mac 1.8 version of ruby rather than the latest version. Given that the script requires a gem, it's failing with a require error and the path in the error points to /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/

Obviously the 1.8 in that path is making me suspicious.

If I open up terminal and run the command ruby -v then it correctly returns 2.0.0p0 which I installed and set as default using RVM.

How might I get Xcode to look in the right place? Or am I mis-interpreting this error?

Update:

To give a little more info, here is the exact error that the compiler is throwing:

/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require': no such file to load -- json (LoadError)
    from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `require'
from /Volumes/Macintosh HD/Documents/Projects/WesternMusicElements/WesternMusicElements/Ruby/NoteCollectionParser.rb:9
Command /bin/sh failed with exit code 1
Mark Wheeler
  • 587
  • 2
  • 6
  • 19

7 Answers7

3

You can also do rvm use system which switches to default mac os ruby and then install the gems with sudo. Seemed to be the easiest for me.

Cyril Cermak
  • 191
  • 2
  • 4
2

I used this to run Xcode with an interpreter other than the default system interpreter:

  1. Quit Xcode if it's open and go to your terminal.
  2. Activate the rvm you want to use.
    rvm use 2.0.0p0
  3. Then open Xcode from the terminal:
    open -a Xcode

To test this, you can include something in your build that logs the interpreter's version, or available gems.

chb
  • 1,727
  • 7
  • 25
  • 47
  • Is there a difference between opening XCode with `open -a Xcode` and opening XCode through the dock or spotlight? My run script only builds successfully when I open XCode through `open -a Xcode`. When I don't open it through the command line I get errors that make me believe XCode is running my script with 2.3 instead of my .rvm Ruby version 2.7.0 – Danielson Jan 30 '20 at 00:29
2

If you have Xcode print out the environment variables, IIRC, the $PATH set by Xcode is different from the $PATH that you load in a regular terminal session.

This leaves you with (at least) two ways to go about fixing the problem:

  1. You can edit your script to point to the rvm-installed ruby directly. This might be better if your script will have to work on multiple systems (even if it does go against how rvm promises to work).

  2. According to Peter Hosey's comment on Chris Hanson's answer in this question, you can set environment variables in ~/.MacOSX/environment.plist that will apply to all processes you launch.

Community
  • 1
  • 1
zadr
  • 2,505
  • 18
  • 19
2

Assuming you are specifying your rvm ruby versioning in your project (and I think you should be), I think this is a more generally applicable answer than the present ones.

In broad strokes, in your shell script, you should

# provides the rvm *function* to your shell script 
source "${SOMETHING}/scripts/rvm"

# then, for this script, the rvm function will change to
# the controlling ruby version for the pwd when this is run
rvm use .

I cover this in more production-ready detail in this answer.

Community
  • 1
  • 1
Clay Bridges
  • 11,602
  • 10
  • 68
  • 118
1

I had a similar situation, but the script being run had a shebang line referring to the system-installed Ruby.

#!/usr/bin/ruby

My solution, which might be dangerous, was to replace /usr/bin/ruby with a symlink to the Ruby installed by RVM.

Here are the steps I took:

  1. Install multi-user RVM (the sudo option from https://rvm.io/rvm/install)
  2. Add myself to the rvm group with the Settings app (Settings > Users and Groups).
  3. Log out and back in to ensure group membership is respected by the OS.
  4. Install the gems I needed from the command line. Since I'm now a RVM manager by virtue of being in the rvm group, this affects the stuff under /usr/local/rvm.
  5. Move /usr/bin/ruby out of the way and symlink the RVM default Ruby in its place. (This is the dangerous part. It might lead to issues if programs assume they'll be using the system-installed version.)

To replace the symlink, I did:

sudo mv /usr/bin/ruby /usr/bin/system_ruby
sudo ln -s /usr/local/rvm/rubies/default/bin/ruby /usr/bin/ruby

After doing this (while Xcode was not running), the next time I built the project in Xcode, the script used the Ruby I had installed with RVM.

amacleod
  • 1,450
  • 2
  • 15
  • 23
1

If you use rbenv you can put the following line before everything in your build phase script:

export PATH=~/.rbenv/shims:$PATH
pckill
  • 3,709
  • 36
  • 48
0

I had this problem also, as i wanted to use xcpretty. I end up doing the following below to point at the correct gem folder

$RUBYPATH=$(eval ~/.rvm/bin/rvm info | grep "GEM_HOME:" | awk -F '"' '{print $2}')

whatever gem you want will be in this folder.

Also if you just want you ruby home path:

$RUBYPATH=$(eval ~/.rvm/bin/rvm info | grep "MY_RUBY_HOME:" | awk -F '"' '{print $2}')

xcpretty was under the first option in bin/ in my scenario as bellow. So for me i end up doing the following:

$RUBYPATH=$(eval ~/.rvm/bin/rvm info | grep "GEM_HOME:" | awk -F '"' '{print $2}')
RUBY_BIN_PATH=${RUBY_PATH}"/bin/" 
XCPRETTY=$RUBY_BIN_PATH"xcpretty"
eval $XCPRETTY

You can find out where your ruby command is beforehand by doing which

RicardoDuarte
  • 728
  • 7
  • 23