Wednesday, December 10, 2008

Ruby installation issues

Based on my experiences with installation of ruby, am giving below steps for a smooth installation:
  • I preferred zip file to one-click installer and downloaded ruby-1.8.7 (latest at this time of writing)
  • As the zip file won't contain the gem (tool for managing ruby modules, also known as gems), download rubygems-1.3.1 (latest at this time of writing)
  • Unzip the ruby distribution zip file to your local directory (say c:\ruby-1.8.7) and add c:\ruby-1.8.7\bin to your path. You can verify this by typing "ruby -v" at the command prompt.
  • Unzip the rubygems distribution zip file to a local directory (say c:\rubygems). This will contain a file called setup.rb. Run this file from command prompt as "ruby setup.rb". This would create a batch file called "gem.bat" along with creating other folders, files in your ruby installation folder (c:\ruby-1.8.7\bin).
  • Also download zlib and keep the zlib.dll in c:\ruby-1.8.7\bin as this is required for running gem.bat
Now time to verify installation of gem:
Initially there won't be any gems (ruby modules) installed. Type "gem environment" at the command prompt and confirm that all the variables are set correctly. For example, GEM PATHS would be set to c:\ruby-1.8.7\lib\ruby\gems\1.8 and REMOTE SOURCES would be set to http://gems.rubyforge.org/

After verifying above details type "gem list --remote" to get a list of gems available at rubyforge. If you are behind a proxy, you would get below output instead of a list of gems:

*** REMOTE GEMS ***

ERROR: http://gems.rubyforge.org/ does not appear to be a repository
ERROR: While executing gem ... (Gem::RemoteFetcher::FetchError)
Errno::ECONNREFUSED: No connection could be made because the target machine actively refused it. - connect(2) (http://gems.rubyforge.org/yaml)

To resolve this you need to set an environment property called http_proxy to your proxy host, would do like this on windows "set http_proxy=http://proxy_addr:port".

Now lets install a gem called progressbar by executing "gem install progressbar". Ignore the error saying SSLEAY32.dll is required ( no idea what exactly is failing, but gems are getting installed properly). Now let us test this by running the test script test.rb, available as part of progressbar installation. This would be available at c:\ruby-1.8.7\lib\ruby\gems\1.8\gems\progressbar-0.0.3\sample. Run this by entering "ruby test.rb". Read further if you get below error instead of a nice progress bar:

test.rb:1:in `require': no such file to load -- progressbar (LoadError)
from test.rb:1

This could be resolved in two ways:
  1. set an environment variable RUBYOPT as "set RUBYOPT=rubygems"
  2. alternately, add "require 'rubygems'" before adding any "require lines".
All this info is there at various places, this is just an effort to consolidate. Do let me know if you have faced any other issues.

Careful with HERE doc in shell scripts

We have a shell script scheduled as a cron job to do certain backup activities. As part of backup it uploads the data to another server via ftp and we used HERE doc for managing ftp session.

Being a nice Java developer, we formatted the script nicely forgetting the fact that for HERE doc to recognize the end of stream the token should start on a line itself; not even spaces are allowed !!

Here is an example to make this clear:
01 if [ $data = 0 ]
02 then
03   echo "Moving to backup server"
04   ftp -i -v -n myServer <<EOF
05   user $username $password
06   put $file.zip
07   bye
08   EOF
09 fi

Note how EOF is aligned on line 08. This way the stream will never end and will result in an error. Below is how it should be aligned
01 if [ $data = 0 ]
02 then
03   echo "Moving to backup server"
04   ftp -i -v -n myServer <<EOF
05   user $username $password
06   put $file.zip
07   bye
08 EOF
09 fi