The easiest way to install Ruby on your system is to use one of these Ruby version managers: RVM or rbenv.
But sometimes, you just need to install Ruby from source, for example for your production environment where there isn’t version manager installed. In fact, I was tasked with upgrading Ruby on our production server last week. And it was not as straight forward as I was expecting.
TL;DR
This is the original post that has everything you need to install Ruby 1.9.3 manually from the source: Collective Idea - Install ruby 1.9.3 with LibYAML on CentOS
I wish I had read the post before attempting Ruby installation.
Install Ruby for rbenv
Before I attempted upgrade on the production server, I “practice” the upgrade first on my dev environment.
I use rbenv and while rbenv does the version management for you, one thing that it doesn’t do is install Ruby. For installing Ruby, most rbenv users would use its ruby-build. This is the easiest option to get ruby installed and configured for rbenv.
But of course you could also just install Ruby from the source to be used for rbenv. Below are the steps that I took to install Ruby 1.9.3-p194 manually for rbenv.
Install LibYAML (if not installed)
First thing first - make sure LibYAML is installed. If you aren’t sure - it doesn’t hurt to just install it.
Since Ruby 1.9.3-p0, libyaml is required so YAML parsing. You could install Ruby without it but that just doesn’t make sense as there are important Ruby libraries that depend on it (such as Bundler).
The Collective Idea link above outlines steps to do it.
Configure and Install Ruby
Ruby installation is straight forward (if you know what to set up - more on that later), after download, extrac and cd into the directory, you’d just need to issue following:
./configure --prefix=[RBENV_DIR]/versions/1.9.3-p194 --enable-shared --with-opt-dir=/usr/local/lib
./make
./make install
Notice the first command, this is crucial to get your Ruby correctly compiled and installed:
- prefix options tells where to put the Ruby after installation. Rbenv keeps the ruby versions on ~/.rbenv/versions. Subsitute [RBENV_DIR] above with the full path of your rbenv installation (normally on ~/.rbenv)
- with-opt-dir is the location of the libraries needed to compile Ruby (libyaml location should be specified here).
Now if you do rbenv versions
, you should see 1.9.3-p194 installed there.
Change your global Ruby by issuing rbenv global 1.9.3-p194
. And you’re done!
Summary
Installing Ruby from source is not that terribly difficult, while it doesn’t beat the simplicity of using ruby-build, it’s always good to do it the manual way just in case you’d need to do it in the future.
I did make a mistake on my first attempt, my mistake was
not specifying with-opt-dir when running configure
. And
as a result Ruby was compiled without YAML support.
When I did that, I got this warning:
/usr/local/lib/ruby/1.9.1/yaml.rb:56:in `<top (required)>':
It seems your ruby installation is missing psych (for YAML output).
To eliminate this warning, please install libyaml and reinstall your ruby.
Anyway, lesson learned.