64

I am using osx 10.8.2 installed ruby 2.0 and.... got this when trying to run "sudo gem install rails"

$ sudo gem install rails
ERROR:  Loading command: install (LoadError)
    cannot load such file -- openssl
ERROR:  While executing gem ... (NoMethodError)
    undefined method `invoke_with_build_args' for nil:NilClass

I had ruby 1.9.x and rails 3.2.x working alright before

Stephen Nguyen
  • 5,357
  • 5
  • 24
  • 28

14 Answers14

92

You have to install OpenSSL first and recompile ruby again:

RVM:

rvm pkg install openssl
rvm reinstall ruby-2.0.0-p0 --with-gcc=gcc-4.7 --with-openssl-dir=$rvm_path/usr
Valery Kvon
  • 4,438
  • 1
  • 20
  • 15
  • 6
    I suggest you add `ruby_configure_flags=--with-openssl-dir="$rvm_path/usr"` to `~/.rvm/user/db` so that if you re-install Ruby again to add another package you don't forget to include OpenSSL. – georgebrock Jul 26 '13 at 08:45
  • 4
    How about on Windows?? – user1429980 Dec 12 '13 at 05:46
  • I am also having this problem on windows. Any help for us? – Automatico Aug 24 '14 at 21:04
  • 1
    @ValeryKvon I was compiling ruby from source. Then I executed gem install bundler. I got this error : `ERROR: Loading command: install (LoadError) cannot load such file -- zlib ERROR: While executing gem ... (NoMethodError) undefined method `invoke_with_build_args' for nil:NilClass` – abhimanyuaryan Oct 30 '15 at 10:04
  • @user1429980 for windows don't worry about the installation process. You can straight away download rails installer and install .exe: http://railsinstaller.org/en – abhimanyuaryan Oct 30 '15 at 10:05
  • 9
    This worked for me _without_ the `--with-gcc` flag, FWIW. – Yohaï-Eliel Berreby Sep 15 '16 at 16:11
  • 1
    `-bash: rvm: command not found`, see: [rvm: command not found MAC OX](http://stackoverflow.com/a/11776021/394969) – John Erck Jan 16 '17 at 14:41
  • if suggested command fails with error about missing gcc in PATH, use `--with-gcc` without certain version helps: `rvm reinstall ruby-2.0.0-p0 --with-gcc --with-openssl-dir=$rvm_path/usr` – TooroSan Feb 19 '20 at 18:05
  • This helped me to solve my problem. – drupalfan Dec 30 '21 at 12:33
40

If you're using RVM please follow:

$ brew install automake
$ rvm pkg install openssl
$ rvm requirements run
$ rvm reinstall all --force
$ gem install rails

It worked for me.

xyz
  • 2,277
  • 2
  • 25
  • 41
11

I had very bad time with this ERROR. Finally i done with it. There is only Once solution either you are with RVM or without RVM.

Make sure you have installed OpenSSL first BEFORE installing ruby.

RVM

  1. Uninstall rvm

    rvm implode

or

rm -rf ~/.rvm

Don’t forget to remove the script calls in your .bashrc and/or .bash_profile (or whatever shell you’re using). sudo apt-get install zlib1g zlib1g-dev build-essential openssl libssl-dev libmysqlclient18 libmysqlclient-dev libyaml-dev curl git-core python-software-properties libpq-dev nodejs

Then install RVM, post ruby.

OR

rvm pkg install openssl
rvm reinstall ruby-2.0.0-p0 --with-gcc=gcc-4.7 --with-openssl-dir=$rvm_path/usr

WITHOUT RVM

First you should find where Ruby is:

whereis ruby

will list all the places where it exists on your system, then you can remove all them explicitly. Or you can use something like this:

rm -rf /usr/local/lib/ruby
rm -rf /usr/lib/ruby
rm -f /usr/local/bin/ruby
rm -f /usr/bin/ruby
rm -f /usr/local/bin/irb
rm -f /usr/bin/irb
rm -f /usr/local/bin/gem
rm -f /usr/bin/gem

THEN

sudo apt-get install zlib1g zlib1g-dev build-essential openssl libssl-dev libmysqlclient18 libmysqlclient-dev libyaml-dev curl git-core python-software-properties libpq-dev nodejs

apt-get -y update
apt-get -y install build-essential zlib1g-dev libssl-dev libreadline6-dev libyaml-dev
cd /tmp
wget http://cache.ruby-lang.org/pub/ruby/2.0/ruby-2.0.0-p247.tar.gz
tar -xvzf ruby-2.0.0-p247.tar.gz
cd ruby-2.0.0-p247/
./configure --prefix=/usr/local
make
make install

I hope this help you.

Pravin Mishra
  • 8,298
  • 4
  • 36
  • 49
7

I fix this error for ruby 2.1.2 as follow.

rvm pkg install openssl
rvm reinstall ruby-2.1.2 --with-openssl-dir=$rvm_path/usr
Danish Ali
  • 184
  • 1
  • 9
4

On OSX, with rbenv and homebrew, the following worked for me:

brew install openssl
CONFIGURE_OPTS=--with-openssl-dir=$(brew --prefix openssl) rbenv install whatever-ruby-version
hjing
  • 4,922
  • 1
  • 26
  • 29
3

This worked for me which is similar to some of the other answers already posted.

rvm pkg install openssl
rvm reinstall all --force
Flynn
  • 61
  • 1
1

When installing ruby 2.0, it is possible that rubygems 2.0 installation did not complete ok, because of openssl. Make sure you provide a valid path to the openssl config file; you could:

find . -type f -name "openssl.cnf"

path is usually $HOME/.rvm/usr or $HOME/.rvm/usr/ssl

Then

[sudo] rvm reinstall ruby-2.0.0-p0 --with-openssl-dir=[openssl.cnf path] --verify-downloads 1

Make sure rubygems installation complete successfully. Might be a better way to fix that path without reinstalling, but this should do it.

fdibartolo
  • 45
  • 1
1

Make sure to check out this page on the rvm site: https://rvm.io/packages/openssl/

Running

rvm requirements run

gave me:

Missing required packages: autoconf, automake, libtool, pkg-config, apple-gcc42, readline, libxml2, libxslt, libksba, openssl, sqlite

after brew install autoconf automake ...

I was able to rvm reinstall 2.0.0 without openssl errors

Josan
  • 145
  • 1
  • 6
1

I had the same problem with the same OS version. I use rvm and followed the steps in this command:

$ rvm requirements

Following those instructions, I ran:

$ brew update
$ brew tap homebrew/dupes
$ brew install bash curl git
$ brew install autoconf automake apple-gcc42 libtool pkg-config openssl readline libyaml sqlite libxml2 libxslt libksba
1

I had the same problem earlier, tried all of the snippets about and none of them worked out. After looking around for a bit the following worked for me:

$ rvm remove 2.0.0 # get rid of unsuccessful installation
$ rvm get head --autolibs=3 # get the latest RVM and build required libs
$ rvm requirements # just in case, install all other required stuff
$ rvm install ruby-2.0.0
$ rvm --default use ruby-2.0.0

What does rvm get head --autolibs=3 do exactly? I'm guess it automatically downloads dependencies, but I was hoping for a clear answer.

Nikola
  • 817
  • 13
  • 19
0

I encountered the same openssl error on Fedora when trying to use gem install <package>. It seems you need to install additional packages with yum/dnf

sudo dnf install rubygems rubygem-bundler ruby-devel mariadb-devel

After running the above command, gem install <package> should now work.

Adam Prax
  • 6,413
  • 3
  • 30
  • 31
0

just went through the same problem. Takes a bit time but, upgrading openssl with

brew upgrade openssl@1.1

worked for me. Version might be different in the future, type brew install openssl to see which version you can upgrade to. I hope it helps.

2rule
  • 9
  • 1
0

If you have libssl1.1, your problem may be that these older versions of ruby (2.4 is the cutoff) are only compatible with libssl1.0.

For debian/ubuntu, please ensure that apt-get install libssl1.0-dev succeeds.

JellicleCat
  • 28,480
  • 24
  • 109
  • 162
-1

If you are using ruby-install, it will take the same openssl argument as rvm:

ruby-install ruby-2.0.0-p247 -- --with-openssl-dir=/usr/local/opt/openssl