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
ls
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.