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
1234567
$ 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
$ 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
123
$ rvm install 1.8.7
...
Install of ruby-1.8.7-p352 - #complete
Now check the available ruby’s in rvm
12345678910111213141516
$ 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
12345678
$ 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
1234567
~$ 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
~/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 :
1234567
~/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)
12345
~/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
123456789
~/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
123456789
~/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.
~/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.
1234567891011121314151617181920212223
~/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:
12345
~/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
123456789101112131415161718192021222324252627
~/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.
1234567
~/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:
123456789101112131415161718192021222324
~/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.
12345678
~/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.
1234
~/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 :-).
12345
~/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