Thursday, July 30, 2009

The pain of getting ruby 1.9 to work in Mac OS X

I consider my self a perpetual learner and novice of computer science. That is why if you see one my question in one the 100k forums out there I will be the newbie asking a question.

I like ruby, it is an easy to learn scripting language that I find fun to play with and supper handy. Years ago, I considered scripting languages to be either a joke or strictly a system-admin tool. After all I am a C language old fart programmer that start learning computer programing in 1980.

Back then... well, it is all being said by many.

Nowadays with better technology some scripting languages have a chance to be as popular as C is today to do low-level tasks and maybe be part of an os like sh, bash, tsh , etc.

Anyway, back to the pain of upgrading ruby in OS X. After some research I could not find an easy one stop do it all, way to upgrade to ruby 1.9. At one point I end up messing up most of the default ruby installation.
I did find a method that worked for me at :
http://bparanj.blogspot.com/2008/03/installing-ruby-19-on-mac-os-x-with.html
This procedure could use some cleanup but it worked!!!
I am going to share with you what I end up doing so anyone can use this procedure by executing
a script.
The script will download the necessary files in to /tmp and work in this directory.
There is also a second script that it swap all the necessary files from the current version
already installed on your system to the new one.

  1. #!/usr/bin/evn bash
  2. #Initial development: Luis E. McDougall
  3. #Date :7/30/2009
  4. #Do what ever you want with this code
  5. #Intended for Mac OS X leopard
  6. cd /tmp
  7. #download readline and patch - compile and install
  8. curl -O ftp://ftp.gnu.org/gnu/readline/readline-5.2.tar.gz
  9. tar xzvf readline-5.2.tar.gz
  10. cd readline-5.2
  11. curl -O http://ftp.gnu.org/gnu/readline/readline-5.2-patches/readline52-012
  12. patch -p0 <>
  13. ./configure --prefix=$HOME/
  14. make
  15. sudo make install
  16. cd ..
  17. #download and install latest version of ruby.
  18. rm index.ht*
  19. wget -F ftp://ftp.ruby-lang.org/pub/ruby/1.9/
  20. # I am only interesting in the tar.gz files
  21. # you can go crazy and do a less/more cryptic regex here.
  22. sed -e 's#<[^>]*>##g' index.html |sed -e 's#.*File *##g'|sed -e '1,6d'|sed 'N;$!P;$!D;$d'| sed -e '/zip/d'|sed -e '/bz2/d'
  23. echo -n "please enter a file from the above list:"
  24. read -e Ruby_File
  25. curl -O ftp://ftp.ruby-lang.org/pub/ruby/1.9/$Ruby_File
  26. tar xzvf {$Ruby_File}
  27. #since we are working in /tmp
  28. #I am asuming there is no previous
  29. #directories with namme ruby.....
  30. cd ruby*
  31. autoconf
  32. sudo ./configure --prefix=$HOME/ruby19 --with-readline-dir=$HOME/
  33. sudo make
  34. sudo make install
Now I have safely installed ruby1.9 in a place where it does not disturb the original ruby.Yo can change where to install ruby by replace $HOME to whatever directory you want.

The next script allows me to swap default version. Instead of changing the path I re-build the
links every time. I should mention that rake in my system is in /usr/bin and I had to adjust
my script to take care of that.

  1. #!/usr/bin/env bash
  2. rbv=$(ruby -v)
  3. rv=${rbv:5:3}
  4. function r18 () {
  5. ln -s /System/Library/Frameworks/Ruby.framework/Versions/Current/usr/bin/erb /usr/bin/erb
  6. ln -s /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/gem /usr/bin/gem
  7. ln -s /System/Library/Frameworks/Ruby.framework/Versions/Current/usr/bin/irb /usr/bin/irb
  8. ln -s /usr/bin/rake.1.8 /usr/bin/rake
  9. ln -s /System/Library/Frameworks/Ruby.framework/Versions/Current/usr/bin/rdoc /usr/bin/rdoc
  10. ln -s /System/Library/Frameworks/Ruby.framework/Versions/Current/usr/bin/ri /usr/bin/ri
  11. ln -s /System/Library/Frameworks/Ruby.framework/Versions/Current/usr/bin/ruby /usr/bin/ruby
  12. ln -s /System/Library/Frameworks/Ruby.framework/Versions/Current/usr/bin/testrb /usr/bin/testrb
  13. }
  14. function r19 () {
  15. ln -s /Users/luis/ruby1.9/bin/erb /usr/bin/erb
  16. ln -s /Users/luis/ruby1.9/bin/gem /usr/bin/gem
  17. ln -s /Users/luis/ruby1.9/bin/irb /usr/bin/irb
  18. ln -s /Users/luis/ruby1.9/bin/rake /usr/bin/rake
  19. ln -s /Users/luis/ruby1.9/bin/rdoc /usr/bin/rdoc
  20. ln -s /Users/luis/ruby1.9/bin/ri /usr/bin/ri
  21. ln -s /Users/luis/ruby1.9/bin/ruby /usr/bin/ruby
  22. ln -s /Users/luis/ruby1.9/bin/testrb /usr/bin/testrb
  23. }
  24. function cleanruby () {
  25. rm /usr/bin/erb
  26. rm /usr/bin/gem
  27. rm /usr/bin/irb
  28. rm /usr/bin/rake
  29. rm /usr/bin/rdoc
  30. rm /usr/bin/ri
  31. rm /usr/bin/ruby
  32. rm /usr/bin/testrb
  33. }
  34. if [ ${rv} = 1.8 ]; then
  35. echo "Current version is 1.8 swaping to 1.9"
  36. cleanruby
  37. r19
  38. elif [ ${rv} = 1.9 ]; then
  39. echo "Current version is 1.9 swapping to 1.8"
  40. cleanruby
  41. r18
  42. fi
  43. exit

I bet that a more experience person can make these scripts fancier and more efficient. But this works and I am not messing with it.
By the way, you are going to need xcode from apple to be able to compile any of this code.


I like gentoo but I find sabayon to be less painful and this OS have a fantastic way to keep it updated.