31

I have openssl installed in my virtual machine ubuntu12.04lts.

When I run the gem command it gives error.

Error: while executing gem (Gem::Exception)
Unable to require openssl. install openSSL and rebuilt ruby (preferred) or use non HTTPs sources

And I also test require openssl in irb mode. it gives error.

Loaderror: cannot load such file --openssl
from /usr/local/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55 `require'

I have openssl

$openssl version
OpenSSL 1.0.1 14 mar 2012

How to fix the error?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Siva Gnanam
  • 938
  • 2
  • 10
  • 26
  • What version of Ruby are you using and how did you install it? You probably have to rebuild Ruby as it says in the error message. – David Grayson Jan 18 '14 at 08:08
  • Hi David I am using ruby2.1.0p0 of ruby. I installed it by built its source. – Siva Gnanam Jan 18 '14 at 08:40
  • The problem looks like the filename: `--openssl` vs just `openssl` (unless Ruby prepends the dashes on error output). Is `openssl` accidentally getting formatted as a switch? – jww Jan 18 '14 at 10:43
  • Hello noloader, I am sorry I could not understand what you just asking. Is openssl accidentally getting formatted as a switch? what it mean how can I answer to this question. Help me to do Thanks :) – Siva Gnanam Jan 18 '14 at 11:53
  • What if I do not want to run with openssl? – Louie Miranda May 29 '14 at 05:30
  • Without openssl enabled in ruby environment we are using. could not access remote gems. It shows clearly that **install openSSL and rebuilt ruby (preferred) or use non HTTPs sources.** To use HTTPs sources ruby have to access to openssl library. – Siva Gnanam May 29 '14 at 12:10

13 Answers13

24

Note: Calls to rubygems.org are deprecated - proceed with caution!

I had the same issue on Mac OSX after also building ruby2.1.0p0 from the source. I already had openssl installed. It appears that the reference in gems needed refreshing. I ran:

gem source -r https://rubygems.org/ to remove

followed by

gem source -a https://rubygems.org/ to read

After this, I was able to run gems install bundler successfully.

If you run into further errors, you can try ./configure --with-openssl-dir=/usr/local/ssl in your ruby downloaded dir/.

jspacek
  • 1,895
  • 3
  • 13
  • 16
  • Thanks for the heads up! I will put a note that this solution is deprecated, as the answer is old now. – jspacek Feb 13 '15 at 16:44
  • 1
    thanks. I just had this same problem after installing ruby 2.2 on linux from sources also. Your fix worked – Nasser Jun 23 '15 at 04:33
  • More than being deprecated, this is scary from a security standpoint. – Drew Stephens Mar 15 '17 at 19:07
  • @DrewStephens I'm not very knowledgable about security - would you mind elaborating? Would be good to know! – jspacek Mar 19 '17 at 18:17
  • 1
    @jspacek Not using HTTP**S** opens the connection up to man-in-the-middle attacks. HTTPS without certificate pinning is still susceptible to a lot of attacks, but at least they require trusted certificates. Less important in the case of retrieving gems is that **S** prevents attackers from eavesdropping on your HTTP requests. – Drew Stephens Mar 20 '17 at 16:53
  • It seems that running "gem source -a https://rubygems.org/" works just as well as http. Actually, http version now prints out the warning "https://rubygems.org is recommended for security over http://rubygems.org/" for the unsecure version. I'll edit the original. Thanks for pointing this out! – jspacek Mar 22 '17 at 01:52
  • i can't run `gem source`, it says `cannot load such file -- openssl` – Daniel Lizik Jan 06 '20 at 04:41
18

I got this error while using debian where openssl was in /usr/bin.

Following the suggestion of jspacek I reconfigured ruby using:

./configure --with-openssl-dir=/usr/bin

After make and sudo make install I was able to install rails.

Tyler Carberry
  • 2,001
  • 1
  • 14
  • 10
Toni Letendre
  • 181
  • 1
  • 3
16

make sure you have libssl-dev installed:

dpkg -s libssl-dev

if not, install it:

sudo apt-get -y install libssl-dev
kitteehh
  • 442
  • 7
  • 10
8

In case someone else has this problem, try reinstalling your Ruby version with openssl included

brew install openssl
rvm reinstall 2.4.0 --with-openssl-dir=`brew --prefix openssl`

You might already have openssl - so you can ignore the first step. This helped me.

The Whiz of Oz
  • 6,763
  • 9
  • 48
  • 85
4

I'm leaving this answer for reference to future Googlers:

Type all these commands in your Terminal (OSX) just to be extra sure you've done everything (this is what eventually worked for me)

rvm get stable
brew update
brew doctor
brew install openssl
rvm install ruby-2.1 (or whatever version)
rvm use (ruby version here)  i.e. rvm use ruby-2.1
rvm gemset create NAME   i.e. rvm gemset create rails41
gem install rails -v 4.1

Finally...

As many commenters have pointed out - you need OpenSSL installed before you compile ruby before you install rails (or other gems)

Jaykumar Patel
  • 26,836
  • 12
  • 74
  • 76
notaceo
  • 1,093
  • 10
  • 28
3

You probably built Ruby from source before installing OpenSSL. Make sure you install it and then try reconfiguring and rebuilding Ruby.

David Grayson
  • 84,103
  • 24
  • 152
  • 189
  • doing `$ openssl` in bash works for me...but trying to run any `gem` command ends up in `cannot load such file -- openssl` – Daniel Lizik Jan 06 '20 at 06:29
2

Recently I was struggling with ruby installations due to the recent auto-update in openssl version from 1.0 to 1.1 and wanted to revert it back to older version. No other solution except the one given below worked for me:

brew install rbenv/tap/openssl@1.0 rvm reinstall 1.9.3-p551 --with-openssl-dir='/usr/local/opt/openssl@1.0' gem update --system Now my ruby runs with openssl 1.0 whereas all other libraries are running on the default openssl version 1.1.

Akhil Latta
  • 528
  • 5
  • 13
1

Rebuilding Ruby after running sudo apt-get install libssl-dev libreadline-dev libgdbm-dev can solve this issue.

I found this solution here.

Nic
  • 6,211
  • 10
  • 46
  • 69
Justus
  • 11
  • 1
1

I was getting this error installing Ruby with RVM. Nothing worked. I switched to rbenv. Problem solved!

mmell
  • 2,448
  • 1
  • 24
  • 16
1

i had similar issue and tried to solve it by following guides. The "rebuild ruby" is an important step!

after openssl_devel is installed

cd folder_of_ruby_source_code/ext/openssl  
ruby ./extconf.rb  
make  
make install  

cd folder_of_ruby_source_code  
./configure  
make clean       <= clean is important here
make  
make install  
carrottop
  • 73
  • 7
0

for me it was a case of missing dependencies.

i thought i had all the dependencies required to install ruby, but i too was getting an openSSL and readline error.

i attempted to install ruby with RVM without root privileges which failed but gave me a list of missing dependencies. i then installed the dependencies with a package manager. i then re-attempted to install ruby from source from the beginning, and everything worked OK

the dependencies that were missing, for me, were:

 patch libyaml-devel autoconf gcc-c++
 readline-devel libffi-devel openssl-devel
 automake libtool bison sqlite-devel

but they may be different for you.

update: i don't originally remember where exactly i found this list, but a quick search found similar lists on the following pages, which might also help you:

https://www.digitalocean.com/community/tutorials/how-to-install-ruby-on-rails-on-centos-6-with-rvm

http://www.itzgeek.com/how-tos/linux/ubuntu-how-tos/install-ruby-on-rails-on-centos-ubuntu-fedora-from-source.html

infinite-etcetera
  • 1,018
  • 8
  • 7
0

These steps worked for me:

  1. Fixed ruby install:

    brew upgrade openssl; openssldir=which openssl rvm reinstall ruby-2.4.2 --with-openssl-dir=$openssldir

  2. Removed and re-added sources for gems

    gem sources -r https://rubygems.org/ gem sources --add https://rubygems.org/

Sudhanshu Mishra
  • 6,523
  • 2
  • 59
  • 76
-3

Do you use RVM?

If not, try using it and see if it fixes your problem.

TonyTK9
  • 60
  • 2
  • I already installed ruby from its source.So as you said to install rvm. How to do it. Whether i want to remove ruby i already installed and install ruby with help of rvm. I have no clear idea if you can tell me. what difference between install ruby from source and install by rvm. please let me know i need some good reference. Thanks :) – Siva Gnanam Jan 18 '14 at 08:45
  • I'm using rvm and searching for documentation on SSL installation support. I'm going to follow @notaceo 's advice – nf071590 Feb 15 '17 at 06:32