26

I need to use openssl in ruby. How should I install the same? I've installed ruby through rbenv, and am using ubuntu 12.04.

kprakasam@ubuntu:~$ ruby -v
ruby 1.9.2p180 (2011-02-18 revision 30909) [x86_64-linux]

kprakasam@ubuntu:~$ irb
irb(main):001:0> require 'openssl'
LoadError: no such file to load -- openssl
    from /home/kprakasam/.rbenv/versions/1.9.2-p180/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from /home/kprakasam/.rbenv/versions/1.9.2-p180/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from (irb):1
    from /home/kprakasam/.rbenv/versions/1.9.2-p180/bin/irb:12:in `<main>'
Kowshik
  • 1,541
  • 3
  • 17
  • 25

7 Answers7

40

For Mac OSX this is what saved me:

RUBY_CONFIGURE_OPTS=--with-openssl-dir=<openssl install dir> rbenv install

From the Ruby build wiki

But.. how to find the openssl install dir?:

$ brew list openssl
/usr/local/Cellar/openssl/1.0.2d_1/bin/c_rehash
/usr/local/Cellar/openssl/1.0.2d_1/bin/openssl
...

Then the openssl install dir is:

/usr/local/Cellar/openssl/1.0.2d_1/

And the ruby installation command ends as this:

RUBY_CONFIGURE_OPTS=--with-openssl-dir=/usr/local/Cellar/openssl/1.0.2d_1/ rbenv install
fguillen
  • 36,125
  • 23
  • 149
  • 210
  • 1
    I'll add that my compilation error on OSX mentioned two versions of openssl, I then removed all versions of openssl from brew (uninstall and prune) and reinstalled openssl. Then compiling worked with `ruby-install ruby 2.5.1 -- --with-openssl-dir=/usr/local/Cellar/openssl/1.0.2o_1/`. – Harry Moreno May 12 '18 at 20:28
  • Fixes `/tmp/ruby-build.20190501225018.25089/ruby-2.6.3/lib/rubygems/core_ext/kernel_require.rb:54:in \`require': libssl.so.1.0.0: cannot open shared object file: No such file or directory - /tmp/ruby-build.20190501225018.25089/ruby-2.6.3/.ext/x86_64-linux/openssl.so (LoadError)` on Debian 9 as well – Conor May 03 '19 at 06:59
  • Thank you! also fixes "Failed to configure openssl. It will not be installed." – senor_bacon Apr 16 '20 at 23:30
  • 1
    `brew --prefix openssl@1.1` can be used to get the openssl path, i.e. `RUBY_CONFIGURE_OPTS=--with-openssl-dir=$(brew --prefix openssl@1.1)` – fn control option Dec 23 '21 at 21:10
  • Unrecognised command, it just opens help on how to use flags. I copied the command exactly. – iOS Blacksmith Jul 05 '23 at 13:00
24

openssl needs to be installed on your local machine.

You then need to compile Ruby with openssl support, which is achieved via the --with-openssl-dir command-line switch.

Maybe this will help you.

s.m.
  • 7,895
  • 2
  • 38
  • 46
13

Ubuntu

(and other linux distros)

$ # Display the installation directory:
$ openssl version -d
OPENSSLDIR: "/usr/lib/ssl"

$ # May need to uninstall the previous installation:
$ rbenv uninstall 3.1.2
rbenv: remove /home/aidan/.rbenv/versions/3.1.2? [yN] Y

$ # Then reinstall (using the dir from the first step)
$ RUBY_CONFIGURE_OPTS=--with-openssl-dir=/usr/lib/ssl rbenv install 3.1.2 
Installing ruby-3.1.2...
Installed ruby-3.1.2 to /home/aidan/.rbenv/versions/3.1.2
aidan
  • 9,310
  • 8
  • 68
  • 82
  • Thanks. For ruby 2.4.4 the dir of first command did not work. I had to do `RUBY_CONFIGURE_OPTS=--with-openssl-dir=/usr/share/rvm/usr rbenv install 2.4.4` instead – Pablo Mar 08 '23 at 18:44
11

First, install openssl:

sudo apt-get -y install build-essential zlib1g-dev libreadline-dev libssl-dev libcurl4-openssl-dev

Afterwards, recompile Ruby.

Note: Only fixing the comment from @Nebojsa above

Kristian
  • 1,099
  • 1
  • 11
  • 24
mseebacher
  • 4,931
  • 1
  • 16
  • 11
  • This worked for me when installing ruby-2.0.0-p451 from source onto Ubuntu-Saucy 13.10. – Moxley Stratton Apr 06 '14 at 04:38
  • I have upgraded my ubuntu to version 20.04.1 and my ruby stopped working. Using this command and reinstalling the ruby versions on my rbenv did the trick. Thanks! – flpfar Oct 20 '20 at 16:40
6

This might help you: Rails: cannot load such file — openssl.

Samy Dindane
  • 17,900
  • 3
  • 40
  • 50
4

EDIT: Please note that this answer may be out of date. The issue in question was resolved in v0.8.1.


After reading multiple answers to this question, I managed to get it working on macOS 10.15 using the following commands:

brew install rbenv/tap/openssl@1.0
OPENSSL_1_0_DIR=$(brew --prefix rbenv/tap/openssl@1.0)

export CPPFLAGS=-I${OPENSSL_1_0_DIR}/include
export LDFLAGS=-L${OPENSSL_1_0_DIR}/lib

ruby-install ruby 2.2.10 -- --with-openssl-dir=${OPENSSL_1_0_DIR}
Hosam Aly
  • 41,555
  • 36
  • 141
  • 182
0

I am on an M2 mac with two versions of openssl installed via Brew (1.1 & 3).

I set an env var for the --with-openssl-dir with version 1.1's path.

fish:

set -gx RUBY_CONFIGURE_OPTS "--with-openssl-dir=/opt/homebrew/opt/openssl@1.1"

bash/zsh:

export RUBY_CONFIGURE_OPTS="--with-openssl-dir=/opt/homebrew/opt/openssl@1.1"

The build succeeded after the export for me.

Note: That I also exported corresponding CPPFLAGS & LDFLAGS for the compiler.

export LDFLAGS=-L/opt/homebrew/opt/openssl@1.1
export CPPFLAGS=-I/opt/homebrew/opt/openssl@1.1/include
Aure77
  • 3,034
  • 7
  • 33
  • 53