问题
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
回答1:
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:
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).
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.
回答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.
回答3:
I used this to run Xcode with an interpreter other than the default system interpreter:
- Quit Xcode if it's open and go to your terminal.
- Activate the rvm you want to use.
rvm use 2.0.0p0
- 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.
回答4:
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:
- Install multi-user RVM (the
sudo
option from https://rvm.io/rvm/install) - Add myself to the
rvm
group with the Settings app (Settings > Users and Groups). - Log out and back in to ensure group membership is respected by the OS.
- 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
. - 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.
回答5:
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.
回答6:
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
来源:https://stackoverflow.com/questions/15776319/how-to-make-xcode-use-the-correct-version-of-ruby-when-running-a-script