Ruby 1.9.2 Segmentation Fault and OpenSSL

by Irish on September 2, 2011

In one of my current project, whenever I ran a rake task that did a net/http request it was causing segmentation faults.

$ bundle exec rake test:task
$ /Users/cirish/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/net/http.rb:678: [BUG] Segmentation fault
ruby 1.9.2 (2011-06-30 patchlevel 290) [i686-darwin10.8.0]

And it seems whenever I get a segmentation fault the first place I need to look is at OpenSSL. I’ve had similar problems with seg faults and openssl in the past, namely this OpenSSL Bus Error that I was getting on Ruby 1.8.7.

If you read that post you’ll see this happened because I use MacPorts and have OpenSSL installed through it, in addition to a local OS X version. To get Ruby to be happy, you have to point your Ruby install to the correct version at installation time. So remove the bad install

$ rvm remove ruby-1.9.2

And reinstall

$ rvm install ruby-1.9.2 --with-openssl-dir=/opt/local --with-iconv-dir=$rvm_path/usr

Which I forgot to do with my Ruby 1.9.2 install after upgrading OS X versions. That last bit about iconv isn’t directly related, but I have inconv package installed to RVM, you can read about that here RVM Iconv.

Also, this Phusion blog post REE 1-8-7-2011-03-released mentions problems with OpenSSL and MacPorts. Moral of the story… use HomeBrew? Dunno, but I’ll probably start using it when I upgrade my laptop.

Need web application development, maintenance for your existing app, or a third party code review?

Velocity Labs can help.

Click here to get started!

{ 21 comments… read them below or add one }

Sugel September 16, 2011 at 9:01 pm

For me the fix for this with an RVM version of ruby is to install it with the RVM OpenSSL package .In particular I used the following to install ruby 1.8.7-p174 .rvm package install iconv.rvm package install openssl.rvm install 1.8.7-p174 –with-openssl-dir HOME .rvm usr –with-iconv-dir rvm path usr…

Reply

Peter Degen-Portnoy November 17, 2011 at 5:47 pm

Chris, thanks for posting this. After wading through a fair number of bug reports, your instructions were clear, concise and correct!

Reply

Cynical Grinch December 5, 2011 at 1:31 pm

Your delete command is wrong. It should be:

$ rvm remove ruby-1.9.2

Reply

Irish December 5, 2011 at 2:06 pm

Yep, thanks for the catch, post updated.

Reply

Tim December 21, 2011 at 3:04 pm

Does this work with Homebrew? According to https://github.com/carlhuda/bundler/issues/1596 we need openssl 1.0, but it looks like Homebrew only has openssl 0.9.8r

Reply

Irish December 21, 2011 at 3:12 pm

Hey Tim, I’m not sure as I’m not currently using Homebrew. From the sound of that ticket, perhaps OpenSSL 1.0 is only a requirement of Ruby 1.9.3… But I’m not really sure.

Reply

Tim December 21, 2011 at 3:24 pm

No problem. I got it working with macports :-) Thanks for the blog post!

Reply

Mark Lanett December 27, 2011 at 5:23 pm

Thanks for the tips. This is my clean install:

1
2
3
4
5
6
export PATH=/usr/bin:/bin:/usr/sbin:/sbin:$HOME/.rvm/bin # keep MacPorts out of PATH during compile
rvm pkg install iconv
rvm pkg install openssl
rvm install ruby-1.9.2 –with-openssl-dir=~/.rvm/usr –with-iconv-dir=~/.rvm/usr
rvm 1.9.2
ruby -ropenssl -e ‘p OpenSSL::Digest::Digest.new(“sha256″)’ # verify OpenSSL works

Reply

Dano Lee January 27, 2012 at 1:17 am

rvm install ruby-1.9.3-head -C –with-gcc=clang,–with-openssl-dir=~/.rvm/usr,–with-iconv-dir=~/.rvm/usr

the above worked for me. Thanks!

Reply

Derek February 11, 2012 at 2:23 am

This works with Ruby 1.9.3 on Snow Leopard.

Thanks Mark!

Reply

Dan Cartoon February 11, 2012 at 12:58 pm

This worked for me as well. Thanks!

Reply

Ethan March 1, 2012 at 7:54 pm

On my Mac OS 10.7.3 I was able to install iconv and openssl but it wouldn’t run the command to install Ruby…

$ rvm install ruby-1.9.2 –with-openssl-dir=~/.rvm/usr –with-iconv-dir=~/.rvm/usr

Unrecognized command line argument(s): ‘–with-openssl-dir=~/.rvm/usr ‘ ( see: ‘rvm usage’ )

However, just installing the packages seems to have corrected the OpenSSL problem. If I just install Ruby normally without the options, then OpenSSL appears to work…

$ ruby -ropenssl -e ‘p OpenSSL::Digest::Digest.new(“sha256″)’ # verify OpenSSL works

#

Reply

Ethan March 1, 2012 at 7:55 pm

The blogging software ate the last line but, the point was that the command ran without an error.

Reply

Tim January 13, 2012 at 1:38 pm

Thank you Mark. That saved me a lot of time and worked on Mac OSX 10.7.2

Reply

Francisco January 25, 2012 at 1:35 pm

Thank you very much Irish!
Worked perfect for me!

Reply

Danroux February 23, 2012 at 6:40 am

Thank you for your solution, this also helped me.

Reply

Eddroid March 13, 2012 at 4:09 pm

I actually fixed this problem by downgrading OpenSSL. I had already installed the MacPorts one, but I built ruby 1.9.3-p125 without MacPorts and the segfault on https URL requests went away. See https://github.com/carlhuda/bundler/issues/1596

Reply

tommyfun April 14, 2012 at 2:05 pm

This is driving me crazy. I shouldn’t have to install rvm to get ruby to build correctly. There are so many rails developers on os x, I can’t believe this is not fixed yet.

Reply

Irish April 16, 2012 at 4:49 pm

You shouldn’t have to, you just need to make sure you’re pointing to the correct openssl when you install it. RVM is just for more convenience.

Reply

Jamie McDonald June 25, 2012 at 1:45 pm

Thanks for the solution to this, really helpful post!

Reply

Brian Pfeil October 4, 2012 at 1:15 pm

Best Post of the Year Award!!

Thanks you so much. I spent hours trying to figure this out.

Reply

Leave a Comment

{ 5 trackbacks }

Previous post:

Next post: