Tuesday, September 28, 2010

Source Installation of Ruby in Ubuntu

In order to get ruby 1.9.2 running on Ubuntu 10.04 (or before, it's been the primary OS I've been using since 2006) you will have to build from source. Don't let your 'newness' scare you, I was just as new when I started doing it a while back, and I learned a lot about source code and the build process. Hopefully you can benefit from my mistakes, I mean, "experience" :)
First things first:

1. You will have to uninstall your current ruby. All gems, all ruby libraries. Everything. When you build something in Ubuntu, the resulting binaries are kept in a different place than if you install from a package. Installed packages go to /usr/bin/ and home rolled binaries go to /usr/local/bin/. The difference is huge. Packages installed in /usr/bin/ are maintained by the package manager. If you delete a package there, your package manager (synaptic) won't like it. Packages installed in /usr/local/bin are your responsibility. You will have to keep the binaries installed there up to date. No big deal, I'll share a little script I wrote (in ruby) to do just that after I am done explaining how to get to that point.
2. Go ahead and download the source code. Normally, I would tell you to grab dependencies, but I want you to get the source code now for a reason. Read this page, http://www.ruby-lang.org/en/news/2010/08/18/ruby-1-9-2-is-released/ and then download the source that ends in .tar.gz
3. Open a command terminal (Applications > Accessories > Terminal Its Icon is a black box with ">_" inside of it.
issue the command "tar -xzvf /path/to/tar.gz" if you download things to the Downloads folder it will be
tar -xzvf ./Downloads/ruby-1.9.2-p0.tar.gz
notice the little dot in front of the first /? that means "start looking in this directory."

You can use the tab button to complete the command, for instance I would type tar -xzvf ./Down and the tab would complete the folder name and then "ru" to complete the .tar.gz filename. Tab completion is laziness at its best!

4. after the tar command works its beautiful magic, type
cd ./ruby/ext/
that will put you in the ruby source code directory for ruby extension libraries. now type
to list out all the files and directories in this folder.
What I am having you do is look at the various pieces of ruby that may or may not install when it's installation time. It will help you install dependencies.

5. Time to hunt for dependencies! Open synaptic and start searching. Use the real search tool, not the quick search. The packages you need to install end in "-dev". These packages are actually C header files the make tool will use to build your ruby!

6. You still have your terminal open right? Most of the entries you see are directories that contain the source code for building different parts of the ruby library. Ruby doesn't *technically* need these these parts to be ruby, but they do make ruby useful. The bad news is that you have to have the dependencies installed in order to compile the source contained in these directories.

7. Back to Synaptic! Before you build ruby, you need to install some packages that will let you build anything. First search for something called "build essential" The first package that results will let you build source code, click it, and get it ready to install, you are not done yet. Second, search for "autoconf". You will need it to preconfigure the building of ruby. Next search for a package called "bison" You will need to check that one to install as well. The final tool you will need is "subversion", which is how ruby keeps track of itself once you are ready to download the absolute freshest source code. Now let's find some libraries that ruby depends upon. First, search for "readline dev" This one is tricky because you will find the package you want listed as "libreadline5-dev" there are a lot of choices, because there is much variety in the readline library. I compile my ruby with readline 5. Now search for "openssl" This one is really tricky, because the library you actually want to install is called "libssl-dev" There is wisdom to be gained by looking around at the results of your search, namely that there are libraries that have the name 'ruby' in them! Do not install them! You are on a different path. Their path is the one you are leaving. One benefit of installing the openssl development library is that it also depends upon the zlib library, which ruby also needs! Two dependencies for the price of one!

8. Install the packages. It may take a while.

9. Now that those packages are installed, you are ready to go back to the terminal and begin building ruby. Go to your terminal and type "cd .." That means, "change directory, to the one immediately below this one." That should be the root directory of your ruby source code. The magic begins now.

10. First, type "ls", and look at what is there. A lot of files end in ".c" : That's your source code! In a minute it will become your ruby mine. Next, you need to type "sudo autoconf" at the command prompt. That will generate a "configure" script that you use to configure your source code to your environment.

11. next you will need to type "./configure --enable-shared" notice the . and the / in front of configure. they mean 'use the configure command located in this directory, furthermore, use the flag that will configure the source to generate shared objects library.' The shared objects library is important if you want to use shoes, or rails.

12. Time to make! Type "sudo make" and watch the code work. It should take a few minutes, but at the end, you will see success.

13. Test the code! Type "sudo make test" to test the code. If it passes, you will be ready to install.

14. Install the built code! Type "sudo make install". This will install the code you will be running.

15. Now you need to clean up your mess. You want to leave your source clean, so you type "sudo make clean" to clean it up.

16. You have the ruby! Yippee, but you are not done. First visit the Ruby Core Page and look at how to install the source code.
Mull it over!
Are you really bold?
http://pastie.org/1187982 for my script on updating my ruby.


elmany said...

Very interesting your recipe to install ruby1.9.2
The only thing to stop me to follow it was: how do I remove everything about ruby before?
I have ruby1.8.7 with rails 2.3.8 and want to upgrade to 1.9.2 and to rails 3 but without break my system...
Thanks for your help.

G. Brandon Hoyt said...

Hi elmany,
I am assuming you are running Ubuntu. If you are not, let me know! How did you install ruby?

elmany said...

Hello Bradon, thanks for answer me.
Yes you are right I'm running Ubuntu 10.01.
I installed ruby manually. it is here:

G. Brandon Hoyt said...

ok elmany,
first of all, it looks like ruby has been installed in two different places, that usually means you've installed it via the package manager and in some other fashion. First thing you need to do is remove the /usr/local/bin/ruby file, and then fire up your package manager, synaptic. search for ruby, and see where the boxes are checked that show you what is installed, click them to remove the packages, and then you should be good to go.

elmany said...


in /usr/local/bin
there are also:

Do I have to delete them also?

elmany said...

I reread your post... You already said that everything must be deleted...

elmany said...

Everything fine. Already installed 1.9.2.

G. Brandon Hoyt said...

Ok, great! Glad I could help. What gems do you have installed?

elmany said...

right after intall ruby1.9.2 I installed rails, so here is my list:

*** LOCAL GEMS ***

abstract (1.0.0)
actionmailer (3.0.0)
actionpack (3.0.0)
activemodel (3.0.0)
activerecord (3.0.0)
activeresource (3.0.0)
activesupport (3.0.0)
arel (1.0.1)
builder (2.1.2)
bundler (1.0.1)
erubis (2.6.6)
i18n (0.4.1)
mail (
mime-types (1.16)
minitest (1.6.0)
polyglot (0.3.1)
rack (1.2.1)
rack-mount (0.6.13)
rack-test (0.5.6)
rails (3.0.0)
railties (3.0.0)
rake (0.8.7)
rdoc (2.5.8)
thor (0.14.2)
treetop (1.4.8)
tzinfo (0.3.23)

G. Brandon Hoyt said...

Oh Good, happy programming! Glad once again to be able to help.

elmany said...

Thanks again for share your experience and knowledge.
Everything is running fantastic, no problems at all.
People like you make the ruby community rock.

dan42 said...

Thank you, that helped me to get readline working. I really didn't expect that I needed libreadline5-dev instead of libreadline6-dev