Frustrations abound: RVM vs. MacPorts and the [BUG] Segmentation fault

Do you use RVM? Do you use MacPorts? Have you installed openssh via MacPorts? And have you torn your hair out when trying to compile Ruby 1.9.x and gotten errors like this:

% bundle install
Fetching gem metadata from https://rubygems.org/......
Fetching gem metadata from https://rubygems.org/..
/Users/amy/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/http.rb:799: [BUG] Segmentation fault
ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-darwin11.3.0]

-- Control frame information -----------------------------------------------
c:0033 p:---- s:0199 b:0199 l:000198 d:000198 CFUNC  :connect
c:0032 p:0011 s:0196 b:0196 l:002460 d:000195 BLOCK  /Users/amy/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/http.rb:799
c:0031 p:0031 s:0194 b:0194 l:000193 d:000193 METHOD /Users/amy/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/timeout.rb:54
c:0030 p:0026 s:0182 b:0182 l:000181 d:000181 METHOD /Users/amy/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/timeout.rb:99
c:0029 p:0485 s:0176 b:0176 l:002460 d:002460 METHOD /Users/amy/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/http.rb:799
c:0028 p:0011 s:0168 b:0168 l:000167 d:000167 METHOD /Users/amy/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/http.rb:755
c:0027 p:0089 s:0165 b:0165 l:000164 d:000164 METHOD /Users/amy/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/http.rb:750

You are most likely to have been in my position for the past day or so where unfruitful searches for “rvm” and “http.rb:799: [BUG] Segmentation fault” turned up not much conclusive. There are a number of posts on StackOverflow which seem to be talking about this and even a very detailed post, but I found the solutions to be incomplete.┬áThen I chanced across the Using MacPorts with RVM page. It turns out that using MacPorts will install openssl but in a different place than what RVM is expecting, hence because your Gemfile┬ámost likely has:

source 'https://rubygems.org'

gem 'rails', '3.2.1'

Rails/bundler is going to try to use openssl, but of course http.rb is going to fail with a:

-- C level backtrace information -------------------------------------------

   See Crash Report log file under ~/Library/Logs/CrashReporter or
   /Library/Logs/CrashReporter, for the more detail of.

...

You may have encountered a bug in the Ruby interpreter or extension libraries.
Bug reports are welcome.
For details: http://www.ruby-lang.org/bugreport.html

Abort trap: 6

Not happy. Worse yet, many of the solutions just say to use “with-openssl-dir”, but that didn’t completely solve it for me.

Fortunately the official RVM page has an incantation if you want the MacPorts’ installation of openssl: add this to your master $HOME/.rvmrc:

export CFLAGS="-O2 -arch x86_64"
export LDFLAGS="-L/opt/local/lib"
export CPPFLAGS="-I/opt/local/include"

Follow that up with a basic install of 1.8.7:

rvm install 1.8.7 --with-openssl-dir=/opt/local

And finish that with the actual Ruby version of your choice:

rvm install ruby-1.9.3-p194 --with-opt-dir=/opt/local

(You can also do an rvm reinstall.)

The happy result?

rvm reinstall ruby-1.9.3-p194 --with-opt-dir=/opt/local
Removing /Users/amy/.rvm/src/ruby-1.9.3-p194...
Removing /Users/amy/.rvm/rubies/ruby-1.9.3-p194...
Removing ruby-1.9.3-p194 aliases...
Removing ruby-1.9.3-p194 wrappers...
Removing ruby-1.9.3-p194 environments...
Removing ruby-1.9.3-p194 binaries...
Please note that you are using your own '--with-opt-dir=', make sure 'libyaml' is installed and available for ruby compilation.
Installing Ruby from source to: /Users/amy/.rvm/rubies/ruby-1.9.3-p194, this may take a while depending on your cpu(s)...

ruby-1.9.3-p194 - #fetching
ruby-1.9.3-p194 - #extracting ruby-1.9.3-p194 to /Users/amy/.rvm/src/ruby-1.9.3-p194
ruby-1.9.3-p194 - #extracted to /Users/amy/.rvm/src/ruby-1.9.3-p194
ruby-1.9.3-p194 - #configuring
ruby-1.9.3-p194 - #compiling
ruby-1.9.3-p194 - #installing
Removing old Rubygems files...
Installing rubygems-1.8.24 for ruby-1.9.3-p194 ...
Installation of rubygems completed successfully.
ruby-1.9.3-p194 - adjusting #shebangs for (gem irb erb ri rdoc testrb rake).
ruby-1.9.3-p194 - #importing default gemsets (/Users/amy/.rvm/gemsets/)
Install of ruby-1.9.3-p194 - #complete

Note the fact I also had done a sudo port install libyaml. Also note that should that fail, you may have to do the manual installation of libyaml per instructions on http://pyyaml.org/wiki/LibYAML:

% ./configure
% make
% make install (or "sudo make install")