Ruby and Rails for business

Rants about software development

Installing-ruby-rails-passenger-on-Ubuntu-11.10

For an internal Rails server of a customer I have set-up Ruby on Rails with passenger on Ubuntu 11.10. This is the raw log of the set-up commands.

WARNING: this system is not secured for exposure to the Internet. Additional security measures are required for a publicly accessible server.

Setting up ssh and some typical work directories:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
cd
mkdir .ssh
chmod 700 .ssh
cd .ssh
vim authorized_keys2
vim id_rsa # needed to call out to github
chmod 600 authorized_keys2
chmod 600 id_rsa
cd
mkdir -p data/backed_up
ln -s data/backed_up/ b
cd b
mkdir -p github/petervandenabeele
cd github/petervandenabeele
git clone git@github.com:petervandenabeele/stuff.git
cd stuff/rc_files/
ls -al
cp \.* ~/ # .rc files for standard apps
cd
. .bashrc
l # should now do `ls -l`

Basic software and development dependencies:

1
2
3
sudo apt-get install vim postgresql apache2 git-core curl subversion
sudo apt-get install build-essential openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev automake libtool bison tcl
# do NOT install ruby, passenger etc. through apt-get

Ruby 1.8.7 and 1.9.3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
cd ~/data/
mkdir temp ; cd temp
mkdir ruby ; cd ruby

# first ruby 1.8.7 (miniprelude problems)
svn co http://svn.ruby-lang.org/repos/ruby/tags/v1_8_7_357
cd v1_8_7_367
autoconf
cd ..
mkdir build_1_8_7 ; cd build_1_8_7
../v1_8_7_357/configure
make
sudo make install
ruby -ve 'puts 4' # => success
cd ..

# now ruby 1.9.3
svn co http://svn.ruby-lang.org/repos/ruby/tags/v1_9_3_0
cd v_1_9_3_0
autoconf
cd ..
mkdir build ; cd build
../v1_9_3_0/configure
make
sudo make install
ruby -ve 'puts 4' #=> success

Install Passenger

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# For rails 3.2.0.rc2 we need at least rubygems 1.8.14
# also, 1.8.15 fixes a yaml bug, so 1.8.15 seems good
sudo gem update --system 1.8.15

sudo gem install passenger
sudo apt-get install libcurl4-openssl-dev apache2-prefork-dev libapr1-dev libaprutil1-dev
sudo passenger-install-apache2-module

# adapt the exact versions below to the output of passenger installer
cd /etc/apache2/mods-available
sudo sh -c "echo 'PassengerRoot /usr/local/lib/ruby/gems/1.9.1/gems/passenger-3.0.11' >> passenger.conf"
sudo sh -c "echo 'PassengerRuby /usr/local/bin/ruby' >> passenger.conf"
cat passenger.conf
# PassengerRoot /usr/local/lib/ruby/gems/1.9.1/gems/passenger-3.0.11
# PassengerRuby /usr/local/bin/ruby

sudo sh -c "echo 'LoadModule passenger_module /usr/local/lib/ruby/gems/1.9.1/gems/passenger-3.0.11/ext/apache2/mod_passenger.so' >> passenger.load"
cat passenger.load
# LoadModule passenger_module /usr/local/lib/ruby/gems/1.9.1/gems/passenger-3.0.11/ext/apache2/mod_passenger.so

cd ../mods-enabled
sudo ln -s ../mods-available/passenger.conf
sudo ln -s ../mods-available/passenger.load
sudo service apache2 restart
# the restart should not fail

A demo Rails project. Use Capistrano or at least git clone from the development system for deployment of real apps.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
cd ; cd b ; mkdir projects ; cd projects
sudo gem install rails --version 3.2.0.rc2
... # success
27 gems installed

rails new demo --skip-bundle
cd demo
echo "gem 'therubyracer'" >> Gemfile
bundle install # to create a Gemfile.lock ; do NOT do this with real apps
bundle install --deployment --without development test
rake environment # should not fail
# just to test, set up a Home controller, make routes 'root' point to it
mv public/index.html public/index-original.html

RAILS_ENV=production bundle exec rake assets:precompile

Set-up the virtual host for this demo app

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# create file (as sudo) sites-available/demo
<VirtualHost *>
    ServerName demo.localhost
    DocumentRoot /home/peterv/b/projects/demo/public

    RailsSpawnMethod smart
    RailsEnv production
</VirtualHost>

# add a symlink from ../sites-enabled/demo to ../sites-available/demo
# remove the symlink to ../sites-available/default

# restart server with
sudo service apache2 restart
touch ~/b/projects/demo/tmp/restart.txt

curl localhost | grep Welcome
# => <p>Welcome to Rails</p>

# WARNING: Additional measures are required to secure
#          the site for public use. Only use internally.

Some basic performance tests

1
2
3
4
5
6
7
8
9
10
11
ab -c5 -n100  localhost/?test=36
# Percentage of the requests served within a certain time (ms)
#  50%     69
#  66%     73
#  75%     76
#  80%     78
#  90%     81
#  95%     84
#  98%     90
#  99%     90
# 100%     90 (longest request)

Passenger and Apache have a lot more options, but for a simple internal server, this can be a start.

Installing ruby-debug19 with ruby 1.9.3 on rvm

In the Gemfile of a new Rails 3 application, this section is commented out by default:

Gemfile
1
2
3
...
# To use debugger
# gem 'ruby-debug19', :require => 'ruby-debug'

Uncommenting the last line will install ruby-debug19 and dependencies (including linecache19).

However, in the combination of rvm with ruby 1.9.3 on Ubuntu this fails. The bundle install command hangs forever on the installation of linecache19 (until interrupted with Control-C):

1
2
3
4
5
6
7
8
$ bundle install
Fetching source index for http://rubygems.org/
Using rake (0.9.2.2)
...
Installing linecache19 (0.5.12) with native extensions
^C
Interrupt: An error occured while installing linecache19 (0.5.12), and Bundler cannot continue.
Make sure that `gem install linecache19 -v '0.5.12'` succeeds before bundling.

The root cause of the problem was not clear, but I was able to make this pass with a “manual” gem install of linecache19 and ruby-debug-base19 with an explicit include of an extra source directory:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$ gem install linecache19 -- --with-ruby-include=$rvm_path/src/ruby-1.9.3-p0
Building native extensions.  This could take a while...
Successfully installed linecache19-0.5.12
1 gem installed
$ bundle install
Fetching source index for http://rubygems.org/
...
Installing ruby-debug-base19 (0.11.25) with native extensions ^C
Interrupt: An error occured while installing ruby-debug-base19 (0.11.25), and Bundler cannot continue.
Make sure that `gem install ruby-debug-base19 -v '0.11.25'` succeeds before bundling.
...
$ gem install ruby-debug-base19 -- --with-ruby-include=$rvm_path/src/ruby-1.9.3-p0
Building native extensions.  This could take a while...
Successfully installed ruby-debug-base19-0.11.25
1 gem installed
$ bundle install
...
Your bundle is complete! ...
$

Once this hurdle is passed, a second problem arises …

The currently published versions of ruby-debug-base and linecache19

1
2
ruby-debug-base19 (0.11.25)
linecache19 (0.5.12)

raise exceptions on Rails on ruby 1.9.3

1
2
3
4
5
6
.../gems/ruby-debug-base19-0.11.25/lib/ruby-debug-base.rb:1:in `require':
.../gems/ruby-debug-base19-0.11.25/lib/ruby_debug.so: undefined symbol: ruby_current_thread -
.../gems/ruby-debug-base19-0.11.25/lib/ruby_debug.so (LoadError)
 from .../gems/ruby-debug-base19-0.11.25/lib/ruby-debug-base.rb:1:in `<top (required)>'
  from .../gems/ruby-debug19-0.11.6/cli/ruby-debug.rb:5:in `require'
 from .../gems/ruby-debug19-0.11.6/cli/ruby-debug.rb:5:in `<top (required)>'

The solution for this problem is found on a number of blog posts:

and boils down to this:

  • download newer versions linecache19-0.5.13.gem and ruby-debug-base19-0.11.26.gem from http://rubyforge.org/frs/?group_id=8883
  • gem install those 2 gems from the local Download location, with the above include trick into the current rvm gemset
1
2
3
4
5
6
7
8
$ gem install ~/Downloads/linecache19-0.5.13.gem -- --with-ruby-include=$rvm_path/src/ruby-1.9.3-p0
Building native extensions.  This could take a while...
Successfully installed linecache19-0.5.13
1 gem installed
$ gem install ~/Downloads/ruby-debug-base19-0.11.26.gem -- --with-ruby-include=$rvm_path/src/ruby-1.9.3-p0
Building native extensions.  This could take a while...
Successfully installed ruby-debug-base19-0.11.26
1 gem installed
  • force the Gemfile to use these version
1
2
3
4
5
# To use debugger
gem 'linecache19', '0.5.13', :path => "~/.rvm/gems/ruby-1.9.3-p0/gems/linecache19-0.5.13/"
gem 'ruby-debug-base19', '0.11.26', :path => "~/.rvm/gems/ruby-1.9.3-p0/gems/ruby-debug-base19-0.11.26/"

gem 'ruby-debug19', :require => 'ruby-debug'
  • run bundle install

Once these higher level gems are used, using the ruby-debugger works. E.g. in an rspec test spec:

1
2
3
4
5
require 'ruby-debug'

  describe "my difficult feature" do
  ...
  end

and now simply adding to the code under test

1
  debugger

yields us a debugging prompt to check in detail the operational code

1
2
result = false
(rdb:1) n

Installing ruby and rails with rvm on Ubuntu 11.10

This is the detailed log of an installation of Ruby and Rails on a freshly installed Ubuntu 11.10 (client version, 32-bit on an x86 PC). I started logging straight after the Ubuntu install, so all steps are logged.

Why rvm (and not native ruby packages)

There may be debate, but I have found that for a development system, it is increadibly easy to use rvm, with it’s different ruby versions, different gemsets per project.

In practice that means I do NOT run apt-get install ruby at any time …

Some basic stuff

1
2
3
4
5
6
7
$ sudo apt-get install vim vim-runtime
# take a copy of your .bashrc since it will be
# modified by rvm install
$ cp .bashrc .bashrc.ORIGINAL
# install some requirements for rvm install
$ sudo apt-get install curl libcurl3
$ sudo apt-get install git-core liberror-perl

Install rvm

Based on http://beginrescueend.com/rvm/install/ execute a single user install of rvm

1
$ bash < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer )

Now read the output carefully, it has specific commands that need to be executed. You can get those again later with “rvm requirements”.

Specifically for a fresh Ubuntu install, this line was added as last line to .bashrc

1
2
3
4
$ diff .bashrc.ORIGINAL .bashrc
103a104,105
>
> [[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*

Rerun your .bashrc and test correct rvm installation with

1
2
3
4
5
$ . .bashrc
# test correct rvm installation
$ type rvm | head -1
rvm is a function
# should yield "rvm is a function"

Checking the rvm requirements:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$ rvm requirements

Requirements for Linux ( DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=11.10
DISTRIB_CODENAME=oneiric
DISTRIB_DESCRIPTION="Ubuntu 11.10" )

NOTE: 'ruby' represents Matz's Ruby Interpreter (MRI) (1.8.X, 1.9.X)
             This is the *original* / standard Ruby Language Interpreter
      'ree'  represents Ruby Enterprise Edition
      'rbx'  represents Rubinius
...

To install rbx and/or Ruby 1.9 head (MRI) (eg. 1.9.2-head),
then you must install and use rvm 1.8.7 first.

Additional Dependencies:
# For Ruby / Ruby HEAD (MRI, Rubinius, & REE), install the following:
  ruby: /usr/bin/apt-get install build-essential openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev automake libtool bison subversion
...

Assuming we start with MRI ruby, execute

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
$ sudo apt-get install build-essential openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev automake libtool bison subversion
...
The following NEW packages will be installed:
  autoconf automake autotools-dev bison build-essential dpkg-dev g++ g++-4.6
  git-core libalgorithm-diff-perl libalgorithm-diff-xs-perl
  libalgorithm-merge-perl libapr1 libaprutil1 libdpkg-perl libltdl-dev
  libncurses5-dev libreadline6-dev libsqlite3-dev libssl-dev libssl-doc
  libstdc++6-4.6-dev libsvn1 libtimedate-perl libtinfo-dev libtool libxml2-dev
  libxslt1-dev libyaml-0-2 libyaml-dev m4 sqlite3 subversion zlib1g-dev
...
Setting up m4 (1.4.16-1) ...
Setting up autoconf (2.68-1ubuntu1) ...
Setting up autotools-dev (20110511.1) ...
Setting up automake (1:1.11.1-1ubuntu1) ...
update-alternatives: using /usr/bin/automake-1.11 to provide /usr/bin/automake (automake) in auto mode.
Setting up bison (1:2.4.1.dfsg-3) ...
update-alternatives: using /usr/bin/bison.yacc to provide /usr/bin/yacc (yacc) in auto mode.
Setting up libtimedate-perl (1.2000-1) ...
Setting up libdpkg-perl (1.16.0.3ubuntu5) ...
Setting up dpkg-dev (1.16.0.3ubuntu5) ...
Setting up git-core (1:1.7.5.4-1) ...
Setting up libalgorithm-diff-perl (1.19.02-2) ...
Setting up libalgorithm-diff-xs-perl (0.04-1build1) ...
Setting up libalgorithm-merge-perl (0.08-2) ...
Setting up libapr1 (1.4.5-1) ...
Setting up libaprutil1 (1.3.12+dfsg-2) ...
Setting up libltdl-dev (2.4-2ubuntu1) ...
Setting up libtinfo-dev (5.9-1ubuntu5) ...
Setting up libncurses5-dev (5.9-1ubuntu5) ...
Setting up libreadline6-dev (6.2-2ubuntu1) ...
Setting up libsqlite3-dev (3.7.7-2ubuntu2) ...
Setting up zlib1g-dev (1:1.2.3.4.dfsg-3ubuntu3) ...
Setting up libssl-dev (1.0.0e-2ubuntu4) ...
Setting up libssl-doc (1.0.0e-2ubuntu4) ...
Setting up libsvn1 (1.6.12dfsg-4ubuntu5) ...
Setting up libtool (2.4-2ubuntu1) ...
Setting up libxml2-dev (2.7.8.dfsg-4) ...
Setting up libxslt1-dev (1.1.26-7) ...
Setting up libyaml-0-2 (0.1.4-1) ...
Setting up sqlite3 (3.7.7-2ubuntu2) ...
Setting up subversion (1.6.12dfsg-4ubuntu5) ...
Setting up libyaml-dev (0.1.4-1) ...
Setting up libstdc++6-4.6-dev (4.6.1-9ubuntu3) ...
Setting up g++-4.6 (4.6.1-9ubuntu3) ...
Setting up g++ (4:4.6.1-2ubuntu5) ...
update-alternatives: using /usr/bin/g++ to provide /usr/bin/c++ (c++) in auto mode.
Setting up build-essential (11.5ubuntu1) ...
Processing triggers for libc-bin ...
ldconfig deferred processing now taking place

First install a 1.8.7 ruby as indicated

1
2
3
$ rvm install 1.8.7
...
Install of ruby-1.8.7-p352 - #complete 

Now check the available ruby’s in rvm

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$ rvm list known | head -15
# MRI Rubies
[ruby-]1.8.6[-p420]
[ruby-]1.8.6-head
[ruby-]1.8.7[-p352]
[ruby-]1.8.7-head
[ruby-]1.9.1-p378
[ruby-]1.9.1[-p431]
[ruby-]1.9.1-head
[ruby-]1.9.2-p180
[ruby-]1.9.2[-p290]
[ruby-]1.9.2-head
[ruby-]1.9.3-preview1
[ruby-]1.9.3-rc1
[ruby-]1.9.3[-p0]
[ruby-]1.9.3-head

Install the lastest ruby 1.9.3

1
2
3
4
5
6
7
8
$ rvm install 1.9.3
...
Install of ruby-1.9.3-p0 - #complete 
# test that ruby is installed
~$ rvm use 1.9.3
Using /home/peterv/.rvm/gems/ruby-1.9.3-p0
~$ ruby -v
ruby 1.9.3p0 (2011-10-30 revision 33570) [i686-linux]

Now make and use a gemset for rails 3.1

1
2
3
4
5
6
7
~$ rvm gemset create rails31
'rails31' gemset created (/home/peterv/.rvm/gems/ruby-1.9.3-p0@rails31).
~$ # and use this rails 31 gemset
~$ rvm gemset use rails31
Using /home/peterv/.rvm/gems/ruby-1.9.3-p0 with gemset rails31
~$ rvm current
ruby-1.9.3-p0@rails31

Check existing gems in this ‘rails31’ rvm gemset

1
2
3
4
5
~$ gem list

*** LOCAL GEMS ***

bundler (1.0.21)

Turn off ri and rdoc for local gem installs

1
~$ echo "gem: --no-rdoc --no-ri" > ~/.gemrc

Install basic rails in this gemset

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
~$ gem install rails
...
Successfully installed multi_json-1.0.3
Successfully installed activesupport-3.1.3
Successfully installed builder-3.0.0
Successfully installed i18n-0.6.0
Successfully installed activemodel-3.1.3
Successfully installed rack-1.3.5
Successfully installed rack-cache-1.1
Successfully installed rack-test-0.6.1
Successfully installed rack-mount-0.8.3
Successfully installed hike-1.2.1
Successfully installed tilt-1.3.3
Successfully installed sprockets-2.0.3
Successfully installed erubis-2.7.0
Successfully installed actionpack-3.1.3
Successfully installed arel-2.2.1
Successfully installed tzinfo-0.3.31
Successfully installed activerecord-3.1.3
Successfully installed activeresource-3.1.3
Successfully installed mime-types-1.17.2
Successfully installed polyglot-0.3.3
Successfully installed treetop-1.4.10
Successfully installed mail-2.3.0
Successfully installed actionmailer-3.1.3
Successfully installed rake-0.9.2.2
Successfully installed thor-0.14.6
Successfully installed rack-ssl-1.3.2
Successfully installed json-1.6.1
Successfully installed rdoc-3.11
Successfully installed railties-3.1.3
Successfully installed rails-3.1.3
30 gems installed

Go to a working directory to be placed under git

1
2
~$ mkdir -p data/git ; cd data/git
~/data/git$ 

Make a new rails project (with postgresql and no TestUnit)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
~/data/git$ rails new NewProject -d postgresql -T
create
...

Fetching source index for http://rubygems.org/
Using rake (0.9.2.2)
Using multi_json (1.0.3)
Using activesupport (3.1.3)
Using builder (3.0.0)
Using i18n (0.6.0)
Using activemodel (3.1.3)
Using erubis (2.7.0)
Using rack (1.3.5)
Using rack-cache (1.1)
Using rack-mount (0.8.3)
Using rack-test (0.6.1)
Using hike (1.2.1)
Using tilt (1.3.3)
Using sprockets (2.0.3)
Using actionpack (3.1.3)
Using mime-types (1.17.2)
Using polyglot (0.3.3)
Using treetop (1.4.10)
Using mail (2.3.0)
Using actionmailer (3.1.3)
Using arel (2.2.1)
Using tzinfo (0.3.31)
Using activerecord (3.1.3)
Using activeresource (3.1.3)
Using bundler (1.0.21)
Installing coffee-script-source (1.1.3)
Installing execjs (1.2.9)
Installing coffee-script (2.2.0)
Using rack-ssl (1.3.2)
Using json (1.6.1)
Using rdoc (3.11)
Using thor (0.14.6)
Using railties (3.1.3)
Installing coffee-rails (3.1.1)
Installing jquery-rails (1.0.19)
Installing pg (0.11.0) with native extensions
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

        /home/peterv/.rvm/rubies/ruby-1.9.3-p0/bin/ruby extconf.rb
checking for pg_config... no
No pg_config... trying anyway. If building fails, please try again with
 --with-pg-config=/path/to/pg_config
checking for libpq-fe.h... no
Can't find the 'libpq-fe.h header
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers.  Check the mkmf.log file for more
details.  You may need configuration options.

Provided configuration options:
  --with-opt-dir
  --without-opt-dir
  --with-opt-include
  --without-opt-include=${opt-dir}/include
  --with-opt-lib
  --without-opt-lib=${opt-dir}/lib
  --with-make-prog
  --without-make-prog
  --srcdir=.
  --curdir
  --ruby=/home/peterv/.rvm/rubies/ruby-1.9.3-p0/bin/ruby
  --with-pg
  --without-pg
  --with-pg-dir
  --without-pg-dir
  --with-pg-include
  --without-pg-include=${pg-dir}/include
  --with-pg-lib
  --without-pg-lib=${pg-dir}/lib
  --with-pg-config
  --without-pg-config
  --with-pg_config
  --without-pg_config


Gem files will remain installed in /home/peterv/.rvm/gems/ruby-1.9.3-p0@rails31/gems/pg-0.11.0 for inspection.
Results logged to /home/peterv/.rvm/gems/ruby-1.9.3-p0@rails31/gems/pg-0.11.0/ext/gem_make.out
An error occured while installing pg (0.11.0), and Bundler cannot continue.
Make sure that `gem install pg -v '0.11.0'` succeeds before bundling.

So, this fails because a postgresql development library is missing. Most often in such a case, it is the lib-…-dev package that is missing. These are not installed by default in Ubuntu client version.

A good way to resolve this is to use apt-cache search to find the dev lib. Also compare with

1
dpkg -l '*package*' | grep ^ii

to see what is actually installed.

In this case, this line in the error message points us to a ‘pq’ file missing:

1
# ==> Can't find the 'libpq-fe.h header

This is the result of searching for ‘pq’ in apt-cache :

1
2
3
4
5
6
7
~/data/git$ apt-cache search postgresql | grep lib | grep dev | grep pq
libpq-dev - header files for libpq5 (PostgreSQL library)
libpqxx3-dev - C++ library to connect to PostgreSQL (development files)
libapq-postgresql1-dev - APQ Ada 95 Postgresql Binding Plugin (development)
libapq1-dev - A pluggable Ada 95 Binding to various database systems (development)
libpostgresql-ocaml-dev - OCaml bindings to PostgreSQL's libpq
libpqxx-dev - C++ library to connect to PostgreSQL (development files)

Let’s try the first one : libpq-dev (others are for other languages)

1
2
3
4
5
~/data/git$ sudo apt-get install libpq-dev
...
The following NEW packages will be installed:
  comerr-dev krb5-multidev libgssrpc4 libkadm5clnt-mit8 libkadm5srv-mit8
  libkdb5-5 libkrb5-dev libpq-dev libpq5

Let’s try again the bundle install

1
2
3
4
5
6
7
8
9
~/data/git$ cd NewProject/
~/data/git/NewProject$ bundle install
...
Installing pg (0.11.0) with native extensions
Using rails (3.1.3)
Installing sass (3.1.10)
Installing sass-rails (3.1.5)
Installing uglifier (1.1.0)
Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.

So, this time it worked :-)

Add the rubyracer to the Gemfile to allow a first check

1
2
3
4
5
6
7
8
9
~/data/git/NewProject$ echo "gem 'therubyracer'" >> Gemfile
~/data/git/NewProject$ bundle install
Fetching source index for http://rubygems.org/
...
Installing libv8 (3.3.10.4)
...
Installing therubyracer (0.9.9) with native extensions
...
Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.

Test the rails environment and yes it works :-)

1
~/data/git/NewProject$ rake environment # Success :-) !

Initial git commit of the new Rails project

1
2
3
4
~/data/git/NewProject$ git init
Initialized empty Git repository in /home/peterv/data/git/NewProject/.git/
~/data/git/NewProject$ git add .
~/data/git/NewProject$ git commit -m "Initial commit of rails new -d postgresql -T"

Make sure this project is always ran with this ruby version and gemset.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
~/data/git/NewProject$ vim .rvmrc
~/data/git/NewProject$ cat .rvmrc
rvm use 1.9.3
rvm gemset use rails31
~/data/git/NewProject$ cd ..
~/data/git$ cd NewProject/
==============================================================================
= NOTICE                                                                     =
==============================================================================
= RVM has encountered a new or modified .rvmrc file in the current directory =
= This is a shell script and therefore may contain any shell commands.       =
=                                                                            =
= Examine the contents of this file carefully to be sure the contents are    =
= safe before trusting it! ( Choose v[iew] below to view the contents )      =
==============================================================================
Do you wish to trust this .rvmrc file? (/home/peterv/data/git/NewProject/.rvmrc)
y[es], n[o], v[iew], c[ancel]> yes
Using /home/peterv/.rvm/gems/ruby-1.9.3-p0
Using /home/peterv/.rvm/gems/ruby-1.9.3-p0 with gemset rails31
Now using gemset 'rails31'
Using /home/peterv/.rvm/gems/ruby-1.9.3-p0 with gemset rails31
~/data/git/NewProject$ git add .rvmrc
~/data/git/NewProject$ git commit -m "Add a .rvmrc for 1.9.3@rails31"

Now the rvm ruby version and gemset are set by rvm each time you enter this directory (with cd).

We now have a working and stable environment for Ruby, Rails and rvm, specifically for this project.

Installing postgresql server

Also, the postgresql server needs to be installed:

1
2
3
4
5
~/data/git/NewProject$ sudo apt-get install postgresql
...
The following NEW packages will be installed:
  postgresql postgresql-9.1 postgresql-client-9.1 postgresql-client-common
  postgresql-common

Installing mysql2 gem and mysql server

A similar problem of missing -dev library occurs when try to install the mysql2 gem. This is the error message and the resolution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
~/data/git/NewProject$ grep mysql2 Gemfile
gem 'mysql2'
~/data/git/NewProject$ bundle install
Fetching source index for http://rubygems.org/
...
Installing mysql2 (0.3.10) with native extensions
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

        /home/peterv/.rvm/rubies/ruby-1.9.3-p0/bin/ruby extconf.rb
checking for rb_thread_blocking_region()... yes
checking for rb_wait_for_single_fd()... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lm... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lz... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lsocket... no
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lnsl... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lmygcc... no
checking for mysql_query() in -lmysqlclient... no
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers.  Check the mkmf.log file for more
details.  You may need configuration options.
...

First install the ‘mysql-client’ and ‘mysql-server’ packages.

1
2
3
4
5
6
7
~/data/git/NewProject$ sudo apt-get install mysql
...
The following NEW packages will be installed:
  libdbd-mysql-perl libdbi-perl libhtml-template-perl libnet-daemon-perl
  libplrpc-perl mysql-client mysql-client-5.1 mysql-client-core-5.1
  mysql-server mysql-server-5.1 mysql-server-core-5.1
...

Trying again to install the mysql2 gem with bundle install:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
~/data/git/NewProject$ bundle install
...
Installing mysql2 (0.3.10) with native extensions
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

        /home/peterv/.rvm/rubies/ruby-1.9.3-p0/bin/ruby extconf.rb
checking for rb_thread_blocking_region()... yes
checking for rb_wait_for_single_fd()... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lm... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lz... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lsocket... no
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lnsl... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lmygcc... no
checking for mysql_query() in -lmysqlclient... no
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers.  Check the mkmf.log file for more
details.  You may need configuration options.
...

It is clear that the mysql2 build requires functionality in the ‘mysqlclient’ library (a library in gcc is called with ‘l’). So let’s look for the -dev package associated with the mysqlclient package.

1
2
3
4
5
6
7
8
~/data/git/NewProject$ apt-cache search libmysql | grep '\-dev'
libmysqlclient-dev - MySQL database development files
libmysqlclient16-dev - MySQL database development files - empty transitional package
libmysqlcppconn-dev - MySQL Connector for C++ (development files)
libmysqld-dev - MySQL embedded database development files
libmysql++-dev - MySQL C++ library bindings (development)
libmysql-cil-dev - MySQL database connector for CLI
libmysql-ocaml-dev - OCaml bindings for MySql

Again, the first result ‘libmysqlclient-dev’ seems most promising.

1
2
3
4
~/data/git/NewProject$ sudo apt-get install libmysqlclient-dev
...
The following NEW packages will be installed:
  libmysqlclient-dev

And yes, now the gem installation with native compilation for mysql2 works out :-).

1
2
3
4
5
~/data/git/NewProject$ bundle install
...
Installing mysql2 (0.3.10) with native extensions
...
Your bundle is complete! ...

A list of Debian/Ubuntu package dependencies for gem/bundle install

PostgreSQL : gem ‘pg’ ==> sudo apt-get install postgresql libpq-dev

Mysql : gem ‘mysql2’ ==> sudo apt-get install mysql-client mysql-server libmysqlclient-dev

RMagick : gem ‘rmagick’ => sudo apt-get install libmagickcore-dev libmagickwand-dev

(typical error: Can’t install RMagick 2.13.1. Can’t find Magick-config)