MacPort’s ruby19 does not play nice with defauly OSX’s Ruby 1.8

Update: My apology to Spree team for falsely accusing Spree discriminates Ruby19. It actually has nothing to do with Spree.

The release of version Spree 0.10.2 brings the support for Ruby19 platform thanks to the hard works of Romul Smirnov aka romul. However Spree doesn’t play fair and square with Ruby19 yet, I have a very weird issue and I hope experts out there could shed some light for me.

I am on Mac OS X 10.6.2 with OSX’s default Ruby 1.8.7 and MacPort’s Ruby 1.9.1. Ruby19 resides within /opt/local and hence doesn’t affect local Ruby18 in any ways..(I assume). Ruby19 take precedence over Ruby18 by setting the $PATH variable:

echo $PATH
/opt/local/bin:/opt/local/sbin:/Developer/usr/bin:/opt/local/bin:/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin

which ruby
/opt/local/bin/ruby

which gem
/opt/local/bin/gem

Here is my Ruby19’s gem env:

gem env

RubyGems Environment:
  - RUBYGEMS VERSION: 1.3.6
  - RUBY VERSION: 1.9.1 (2009-12-07 patchlevel 376) [i386-darwin10]
  - INSTALLATION DIRECTORY: /opt/local/lib/ruby1.9/gems/1.9.1
  - RUBY EXECUTABLE: /opt/local/bin/ruby1.9
  - EXECUTABLE DIRECTORY: /opt/local/bin
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86-darwin-10
  - GEM PATHS:
     - /opt/local/lib/ruby1.9/gems/1.9.1
     - /Users/MAC/.gem/ruby/1.9.1
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :benchmark => false
     - :backtrace => false
     - :bulk_threshold => 1000
  - REMOTE SOURCES:
     - http://rubygems.org/

And here is my local Ruby18’s gem env:

/usr/bin/gem env

RubyGems Environment:
  - RUBYGEMS VERSION: 1.3.6
  - RUBY VERSION: 1.8.7 (2008-08-11 patchlevel 72) [universal-darwin10.0]
  - INSTALLATION DIRECTORY: /Library/Ruby/Gems/1.8
  - RUBY EXECUTABLE: /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby
  - EXECUTABLE DIRECTORY: /usr/bin
  - RUBYGEMS PLATFORMS:
    - ruby
    - universal-darwin-10
  - GEM PATHS:
     - /Library/Ruby/Gems/1.8
     - /Users/MAC/.gem/ruby/1.8
     - /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :benchmark => false
     - :backtrace => false
     - :bulk_threshold => 1000
  - REMOTE SOURCES:
     - http://rubygems.org/

As you could see, both use different GEM_PATHS and RUBY_EXECUTABLE, therefore it’s rational to say that the 2 platforms won’t conflict. Okay, now I check the default Gem.path:

ruby -r rubygems -e "p Gem.path"
["/Users/MAC/.gem/ruby/1.9.1", "/opt/local/lib/ruby1.9/gems/1.9.1"]

Looking good, so the default platform is Ruby19. However, when I run ‘ruby script/console’ within a Spree generated project, I have following error:

Loading development environment (Rails 2.3.5)
Missing the Rails 2.3.5 gem. Please `gem install -v=2.3.5 rails`.

What?! Missing Rails 2.3.5?!! Running a check and I confirm that Rails 2.3.5 is installed on my Ruby19 and works correctly:

gem list rails

*** LOCAL GEMS ***

rails (2.3.5)

Then I do another check on my Ruby18 gem just to make sure:

/usr/bin/gem list rails

*** LOCAL GEMS ***

rails (2.2.2, 1.2.6)

* BUMP * So the Spree actually use the Ruby18 platform instead…. Okay then I think I will force my system to use the Ruby19 then by issuing:

export GEM_PATH=/opt/local/lib/ruby1.9/gems/1.9.1
export GEM_HOME=/opt/local/lib/ruby1.9/gems/1.9.1

ruby -r rubygems -e "p Gem.path"

["/opt/local/lib/ruby1.9/gems/1.9.1"]

I tried again and it passes the Rails check but encounter this:

ruby script/console 
Loading development environment (Rails 2.3.5)
/opt/local/lib/ruby1.9/gems/1.9.1/gems/sqlite3-ruby-1.2.5/lib/sqlite3_api.bundle: [BUG] Segmentation fault
ruby 1.8.7 (2008-08-11 patchlevel 72) [universal-darwin10.0]

Abort trap

Then again, it mentions that default Ruby is 1.8.7 still…If it’s 1.8.7, it obviously would fail due to the fact that sqlite3-ruby is installed for Ruby19. Seriously, I have no idea how to enforce default rubygems and ruby to Ruby19. Hope someone can help.

UPDATE: I figured outa DIRTY way to enforce the environment on 1.9.1 by setting the soft link of /usr/bin to 1.9.1 version:

# Remove current 1.8 soft link
sudo rm /usr/bin/erb
sudo rm /usr/bin/gem
sudo rm /usr/bin/irb
sudo rm /usr/bin/rake
sudo rm /usr/bin/rdoc
sudo rm /usr/bin/ri
sudo rm /usr/bin/ruby
sudo rm /usr/bin/testrb

# Add soft link for MacPort's Ruby 1.9.1
sudo ln -s /opt/local/bin/erb1.9 /usr/bin/erb
sudo ln -s /opt/local/bin/gem1.9 /usr/bin/gem
sudo ln -s /opt/local/bin/irb1.9 /usr/bin/irb
sudo ln -s /opt/local/bin/rake1.9 /usr/bin/rake
sudo ln -s /opt/local/bin/rdoc1.9 /usr/bin/rdoc
sudo ln -s /opt/local/bin/ri1.9 /usr/bin/ri
sudo ln -s /opt/local/bin/ruby1.9 /usr/bin/ruby
sudo ln -s /opt/local/bin/testrb1.9 /usr/bin/testrb

# If you want to revert to default 1.8
  ln -s /System/Library/Frameworks/Ruby.framework/Versions/Current/usr/bin/erb /usr/bin/erb
  ln -s /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/gem /usr/bin/gem
  ln -s /System/Library/Frameworks/Ruby.framework/Versions/Current/usr/bin/irb /usr/bin/irb
  ln -s /usr/bin/rake.1.8 /usr/bin/rake
  ln -s /System/Library/Frameworks/Ruby.framework/Versions/Current/usr/bin/rdoc /usr/bin/rdoc
  ln -s /System/Library/Frameworks/Ruby.framework/Versions/Current/usr/bin/ri /usr/bin/ri
  ln -s /System/Library/Frameworks/Ruby.framework/Versions/Current/usr/bin/ruby /usr/bin/ruby
  ln -s /System/Library/Frameworks/Ruby.framework/Versions/Current/usr/bin/testrb /usr/bin/testrb

Well it kinda works but still sucks because I could not switch between Ruby version. Romul suggested me to go for ‘rvm’ and I did give it a try. ‘rvm’ makes things so easy to switch between ruby version. I love MacPort’s Ruby but regrettably I have to say goodbye to it.

UPDATE: I found a shell script that help switching between 1.8.7 and macport 1.9.1:

chruby () {
  v=`ruby -e "puts RUBY_VERSION.split('.')[0,2].join('.')"`
  rubyexes=( erb gem irb rake rdoc ri ruby testrb)
  for i in ${rubyexes[@]}; do
    sudo unlink "/usr/bin/${i}"
    
    if [ $v == "1.9" ];then
      src="/System/Library/Frameworks/Ruby.framework/Versions/Current/usr/bin/${i}"
    else
      src="/opt/local/bin/${i}1.9"
    fi
    
    sudo ln -s $src "/usr/bin/${i}"
  done
  echo "Now Running: "`ruby -v`
}

Thanks for suggestion from Bijan Rahnema, RVM does this switching job better and I highly recommend Ruby and Rails developer use it.

Advertisements

About Jones Lee

Nothing much about me..

4 responses to “MacPort’s ruby19 does not play nice with defauly OSX’s Ruby 1.8

  1. Hey why not check out RVM? Ruby Version Manager…
    http://rvm.beginrescueend.com/

    railscasts.com has a tutorial how to switch between ruby version… 😀

  2. Robert Massaioli

    This should be here for any other people wanting info on this bug to see; it is the bug report that you raised: http://railsdog.lighthouseapp.com/projects/31096/tickets/1285-spree-does-not-respect-ruby-version

  3. I really like reading through a post that will make people think.
    Also, many thanks for allowing me to comment!

  4. Fantastic beat ! I would like to apprentice at the same
    time as you amend your site, how could i subscribe for a blog web site?
    The account aided me a appropriate deal. I have been a little bit familiar
    of this your broadcast offered bright clear concept

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: