Wednesday, 4 September 2013

Installing and configuring Ruby and Rails in Ubuntu 13.04 . Addressing issues with libxml2, nokogiri

After upgrading Ubuntu to 13.04(Raring Ringtail), I started having problems with bundle install and starting  server, main concern was libxml2. 

I managed to solve the issue using 'rvm pkg' and 'build.config'. Finally I thought of writing those steps used. The entire steps of installing rvm, ruby, libraries with some notes about the issues with lixml2.

Libraries required:

Assuming that user has an UBUNTU machine.

Install below given libraries. ex: $sudo apt-get install curl. Multiple libraries can be installed separated by spaces. You can skip those libraries that are not required. Below given list has most of the common libraries required.

$sudo apt-get install curl build-essential openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev automake libtool bison
mysql-server mysql-client libmysqlclient-dev libxml2-dev libxslt-dev imagemagick

$ sudo apt-get install libcurl3 libcurl3-gnutls libcurl4-openssl-dev(required for curb)

$ sudo apt-get install graphicsmagick-libmagick-dev-compat (required for RMagick,  libmagick9-dev doesn not exists any more)

$ sudo apt-get install libmagickcore-dev libmagickwand-dev (If you get ' wand/MAgickWand.h missing' error)

 Installing RVM(

 $ \curl -L | bash -s stable

Folow instructions if any. I had instructions to add "source ~./profile" in to  '~/home/shilesh/.bash_profile'

# making sure that it is loaded for the current instance also
            $source ~/.profile

Close out your current shell or terminal session and open a new one (preferred). You may load RVM with the following command:

user$ source ~/.rvm/scripts/rvm
If installation and configuration were successful, RVM should now load whenever you open a new shell. This can be tested by executing the following command which should output 'rvm is a function' as shown below.

user$ type rvm | head -n 1
rvm is a function

Installing  ruby

$ rvm list known   #This will show the available ruby options.

$ rvm install 1.8.7 # This will install Ruby 1.8.7

$ rvm use 1.8.7

$ rvm --default 1.8.7 # makes 1.8.7 as default ruby version.

$ ruby -v  # should give you something like ruby 1.8.7

Source  code from Git or ?.

You are expected to have ssh keys in .ssh folder of your profile (ex: /home/shilesh/.ssh/id_rsa. And it should have 066 permissions).
$ git clone<project>/<name>.git
# using https will promt for user name and password for each pull and push.
$ git checkout <some_branch> # move to development branch, if not master.

# Edit the values in database.yml with your local mysql server details. If you do not have server ready, update yml file with a user name and password which you will give to the server.

Installing Gems using bundler

Install ruby gems from "" and install.
$gem -v    # This might give you 2.0.6 or greater.

If you did not get the correct version to download, use
$ gem update --system 1.3.7 # This will update the gem to 1.3.7 version
 $ gem install bundler # Installing Bundler gem

We are ready to install all the gems, this might take some time based on your gems.
$bundle install --local


$ bundle exec rake db:create     # This will create database for development

$bundle exec rake db:migrate    # Creates schema

$bundle exec rake db:seed         # Load default values.


$ bundle exec script/server        # Your server should be up now on 3000 port.


Failed undefined method "source index for module"

reason could be  wrong version of gem.  
    $ gem update --system
    # gem install rubygems-update -v 1.3.7

There were cases where installing libxml-ruby fails as,

Installing libxml-ruby (1.1.3) 
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

/home/shilesh/.rvm/rubies/ruby-1.8.7-p374/bin/ruby extconf.rb 
checking for socket() in -lsocket... no
checking for gethostbyname() in -lnsl... yes
checking for atan() in -lm... no
checking for atan() in -lm... yes
checking for inflate() in -lz... yes
checking for iconv_open() in -liconv... no
checking for libiconv_open() in -liconv... no
checking for libiconv_open() in -llibiconv... no
checking for iconv_open() in -llibiconv... no
checking for iconv_open() in -lc... yes
checking for xmlParseDoc() in -lxml2... yes
checking for libxml/xmlversion.h... no
checking for libxml/xmlversion.h in /home/shilesh/.rvm/rubies/ruby-1.8.7-p374/include,/home/shilesh/.rvm/rubies/ruby-1.8.7-p374/include/libxml2,/opt/include/libxml2,/usr/local/include/libxml2,/usr/include/libxml2... yes
creating extconf.h
creating Makefile

And then, even after bundler successfully installed all gems, server might fail as,

$ bundle exec script/server 
=> Booting WEBrick
=> Rails 2.3.17 application starting on
/home/shilesh/.rvm/gems/ruby-1.8.7-p374@izone-1.8.7/gems/nokogiri-1.5.6/lib/nokogiri/ /usr/lib/i386-linux-gnu/ symbol xmlBufUse, version LIBXML2_2.9.0 not defined in file with link time reference - /home/shilesh/.rvm/gems/ruby-1.8.7-p374@izone-1.8.7/gems/nokogiri-1.5.6/lib/nokogiri/ (LoadError)
from /home/shilesh/.rvm/gems/ruby-1.8.7-p374@izone-1.8.7/gems/polyglot-0.3.3/lib/polyglot.rb:63:in `require'

In that case, try to specify the library after getting them with in rvm.

$ rvm pkg install libxml2 --verify-downloads 1

$ bundle config build.libxml-ruby --with-xml2-lib=${HOME}/.rvm/usr/lib --with-xml2-include=${HOME}/.rvm/usr/include/libxml2

$ bundle config build.nokogiri --with-xml2-lib=${HOME}/.rvm/usr/lib --with-xml2-include=${HOME}/.rvm/usr/include/libxml2

Then it is better to re-install all the gems using bundler. I deleted my gemset and created this again. After that installed bundler gem. Did the configurations as mentioned above, then ran bundler.

Everything was working fine then.