116

I installed zlib package and ruby 1.9.3 using rvm, but whenever I try to install gems it says cannot load such file -- zlib

The commands I used to install are

$ rvm install 1.9.3
$ rvm pkg install zlib

$ rvm reinstall 1.9.3 --with-readline-dir=/home/username/.rvm/usr \
    --with-iconv-dir=/home/username/.rvm/usr \
    --with-zlib-dir=/home/username/.rvm/usr \
    --with-openssl-dir=/home/username/.rvm/usr

$ rvm use 1.9.3

$ gem install heroku
ERROR: Loading command: install (LoadError)
    cannot load such file -- zlib
ERROR: While executing gem ... (NameError)
    uninitialized constant Gem::Commands::InstallCommand

All the help on this subject tells me to just do rvm pkg install zlib, and then reinstall the ruby with --with-zlib-dir=/home/username/.rvm/usr, but this didn't work.

Razor Storm
  • 12,167
  • 20
  • 88
  • 148

6 Answers6

203

I ended up installing zlib from apt-get and then reinstalling ruby to not use the rvm directory for zlib.

Here's how do:

$ sudo apt-get install zlib1g-dev
$ rvm reinstall 1.9.3

[Edit] As commenter @chrisfinne mentions, on CentOS/RedHat based systems:

$ sudo yum install zlib-devel
$ rvm reinstall 1.9.3
maerics
  • 151,642
  • 46
  • 269
  • 291
Razor Storm
  • 12,167
  • 20
  • 88
  • 148
  • 21
    or sudo yum install zlib-devel – chris finne Jul 11 '12 at 08:31
  • 4
    Well, regardless of the OS, @chrisfinne 's advice is still useful (for me at least)! –  Sep 27 '12 at 22:28
  • Worked perfectly for me. Although I had to recompile once more since I was missing libssl-dev too (I needed it because rvm wanted to fetch the ruby sources via HTTPS) :-) –  Feb 13 '14 at 14:12
9

The both other responses were almost right...

The rvm site for zlib provides a quick fix!

On the top off the page is a "Quick fix" follow it's instructions, it will:

  • fix your installation,
  • provide you instructions what to do to install zlib from your system
  • and show how to reinstall ruby
richo
  • 8,717
  • 3
  • 29
  • 47
mpapis
  • 52,729
  • 14
  • 121
  • 158
2

Use autolibs to install dependencies:

rvm autolibs enable
rvm reinstall 1.9.3

RVM will automatically install missing dependencies and use them for compiling ruby.

More details on autolibs => http://rvm.io/rvm/autolibs

mpapis
  • 52,729
  • 14
  • 121
  • 158
2

The solution that worked for me is described in rvm github repo, issue #3389.

Run

rvm install 2.3.0 --with-zlib-dir=/usr/local/Cellar/zlib/1.2.8

for ruby 2.3.0 version.

belgoros
  • 3,590
  • 7
  • 38
  • 76
1
rvm pkg install openssl && rvm pkg install zlib && rvm pkg install libyaml &&
  rvm pkg install iconv
rvm install 1.9.3-p0 --with-opt-dir=$HOME/.rvm/usr \
  --with-libxml2-dir=/usr/local/Cellar/libxml2/2.7.8 \
  --with-readline-dir=/usr/local/Cellar/readline/6.2.1
Zombo
  • 1
  • 62
  • 391
  • 407
Kewin
  • 248
  • 1
  • 4
  • 8
1

Ruby has a bunch of package dependencies like the one on zlib-devel. They're nasty because there's nothing to indicate you need the library until get some obscure error message while trying to do something routine.

The solution is to use rvm. Use it even if you only need one version of ruby. Why? Because then you can do

rvm requirements

This will probe your particular OS and produce a set of commands you can copy back to the command line to install those missing packages.