Sunday, September 6, 2009

Ruby 1.9 on Snow Leopard

I think I got this working the way I want it.

I added the following code in my .bashrc
But first I should mention I installed 1.8 and 1.9 from macports.

rbv=$("ruby" -v)
rv=${rbv:5:3}
case $rv in
1.8)
export GEM_HOME=/opt/local/lib/ruby/gems/1.8
export GEM_PATH=$GEM_HOME:/Users/luis/.gem/ruby/1.8:~/lib/rubygems
export PATH=~/bin:$PATH
;;
1.9)
export GEM_HOME=/opt/local/lib/ruby1.9/gems/1.9
export GEM_PATH=$GEM_HOME:/Users/luis/.gem/ruby/1.9:~/lib/rubygems
export PATH=~/bin:$PATH
;;

*)
;;
esac

Now when I swap from one version to the other all of my environment variables are pointing
to the correct place. I've also updated my set_ruby script, here is:

#!/usr/bin/env bash
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root" 1>&2
exit 1
fi

rbv=$("ruby" -v)
rv=${rbv:5:3}
function r18 () {
ln -s /opt/local/bin/erb1.8 /opt/local/bin/erb
ln -s /opt/local/bin/gem1.8 /opt/local/bin/gem
ln -s /opt/local/bin/irb1.8 /opt/local/bin/irb
ln -s /opt/local/bin/rake1.8 /opt/local/bin/rake
ln -s /opt/local/bin/rdoc18 /opt/local/bin/rdoc
ln -s /opt/local/bin/ri1.8 /opt/local/bin/ri
ln -s /opt/local/bin/ruby1.8 /opt/local/bin/ruby
ln -s /opt/local/bin/testrv1.8 /opt/local/bin/testrb
export GEM_HOME=/opt/local/lib/ruby/gems/1.8
export GEM_PATH=$GEM_HOME:/Users/luis/.gem/ruby/1.8:~/lib/rubygems
}

function r19 () {
ln -s /opt/local/bin/erb1.9 /opt/local/bin/erb
ln -s /opt/local/bin/gem1.9 /opt/local/bin/gem
ln -s /opt/local/bin/irb1.9 /opt/local/bin/irb
ln -s /opt/local/bin/rake1.9 /opt/local/bin/rake
ln -s /opt/local/bin/rdoc1.9 /opt/local/bin/rdoc
ln -s /opt/local/bin/ri1.9 /opt/local/bin/ri
ln -s /opt/local/bin/ruby1.9 /opt/local/bin/ruby
ln -s /opt/local/bin/testrb1.9 /opt/local/bin/testrb
export GEM_HOME=/opt/local/lib/ruby/gems/1.9
export GEM_PATH=$GEM_HOME:/Users/luis/.gem/ruby/1.9:~/lib/rubygems
}

function cleanruby () {
rm /opt/local/bin/erb
rm /opt/local/bin/gem
rm /opt/local/bin/irb
rm /opt/local/bin/rake
rm /opt/local/bin/rdoc
rm /opt/local/bin/ri
rm /opt/local/bin/ruby
rm /opt/local/bin/testrb
}
if [ $# -eq 1 ]
then
case $1 in
clean)
cleanruby
exit
;;
1.8)
r18
exit
;;
1.9)
r19
exit
;;
*)
echo "+----------------------------------------------------+"
echo "| Either do not use any paramaters or use: |"
echo "| clean, 1.8 or 1.9 |"
echo "+----------------------------------------------------+"
exit
;;
esac
else
if [ ${rv} = 1.8 ]; then
echo "Current version is 1.8 swaping to 1.9"
cleanruby
r19
elif [ ${rv} = 1.9 ]; then
echo "Current version is 1.9 swapping to 1.8"
cleanruby
r18
fi
fi

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.