Peter Vandenabeele
Installing Ruby on Rails with Postgresql on Ubuntu Jaunty
The goal is to start from an entirely new/fresh Ubuntu Jaunty
system and capture every package (and not much more) that is
required to run a minimal Ruby on Rails demo/development set-up
with postgres on an Ubuntu system. This set-up is certainly not
intended for production deployment. For serious production
deployment, talk to a dedicated Ruby on Rails hosting company.
0. Update/upgrade system
1. Install vim and vim-ruby
2. Install ruby, irb and rdoc (all version 1.8)
3. Install required Ubuntu packages
4. Install rubygems
5. Install Rails (with gem)
6. Install postgresql
7. Install the gem for connecting from ruby to postgres
8. Instantiate a first Rails application
9. Run the Rails application in development mode
0. Update/upgrade system
$ sudo apt-get update
Hit http://security.ubuntu.com jaunty-security Release.gpg
...
Reading package lists... Done
$ sudo apt-get upgrade
Reading package lists... Done
Building dependency tree
Reading state information... Done
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
1. Install vim and vim-ruby
$ sudo apt-get install vim vim-ruby
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
libruby1.8 tcl8.4 vim-gtk vim-gui-common vim-runtime
Suggested packages:
tclreadline ctags vim-doc vim-scripts cscope ttf-dejavu
The following NEW packages will be installed:
libruby1.8 tcl8.4 vim vim-gtk vim-gui-common vim-ruby vim-runtime
...
[now switch from the default vi on Ubuntu to vim :-D]
[back now in vim, the up down arrows now work correctly]
2. Install ruby, irb and rdoc (all version 1.8)
$ sudo apt-get install ruby irb rdoc
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
irb1.8 libreadline-ruby1.8 rdoc1.8 ruby1.8
Suggested packages:
graphviz ruby1.8-examples ri1.8
The following NEW packages will be installed:
irb irb1.8 libreadline-ruby1.8 rdoc rdoc1.8 ruby ruby1.8
3. Install required Ubuntu packages
A number of additional packages are required to allow building
native gems and connecting to the postgres database.
$ sudo apt-get install libopenssl-ruby build-essential ruby1.8-dev libpq-dev
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
comerr-dev dpkg-dev g++ g++-4.3 libkadm55 libkrb5-dev libopenssl-ruby1.8 libssl-dev libstdc++6-4.3-dev patch
zlib1g-dev
Suggested packages:
debian-keyring g++-multilib g++-4.3-multilib gcc-4.3-doc libstdc++6-4.3-dbg krb5-doc postgresql-doc-8.3
libstdc++6-4.3-doc diff-doc
The following NEW packages will be installed:
build-essential comerr-dev dpkg-dev g++ g++-4.3 libkadm55 libkrb5-dev libopenssl-ruby libopenssl-ruby1.8
libpq-dev libssl-dev libstdc++6-4.3-dev patch ruby1.8-dev zlib1g-dev
...
4. Install rubygems
From here on, part of the installation will deviate from the
standard Debian/Ubuntu package management system. This point
is certainly open for discussion and alternative implementations.
This is how I did it, not necessarily the best or only way ...
WARNING: Before you do this, note that apt-get install gem is NOT
what you want here (the gem package on Ubuntu/Debian is the
Graphics Environment for Multimedia - PureData library package).
I have not tested how the procedure below would collide with the
presence of the Ubuntu/Debian gem package.
$ cd ror-installation $ wget http://rubyforge.org/frs/download.php/56227/rubygems-1.3.3.tgz --2009-05-27 22:24:15-- http://rubyforge.org/frs/download.php/56227/rubygems-1.3.3.tgz Resolving rubyforge.org... 205.234.109.19 Connecting to rubyforge.org|205.234.109.19|:80... connected. HTTP request sent, awaiting response... 302 Found Location: http://files.rubyforge.vm.bytemark.co.uk/rubygems/rubygems-1.3.3.tgz [following] --2009-05-27 22:24:15-- http://files.rubyforge.vm.bytemark.co.uk/rubygems/rubygems-1.3.3.tgz Resolving files.rubyforge.vm.bytemark.co.uk... 80.68.94.54 Connecting to files.rubyforge.vm.bytemark.co.uk|80.68.94.54|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 292766 (286K) [application/x-tar] Saving to: `rubygems-1.3.3.tgz' 100%[=========================================>] 292,766 1.09M/s in 0.3s 2009-05-27 22:24:15 (1.09 MB/s) - `rubygems-1.3.3.tgz' saved [292766/292766] $ ls -l total 292 -rw-r--r-- 1 peterv peterv 292766 2009-05-05 01:07 rubygems-1.3.3.tgz $ tar -xvzf rubygems-1.3.3.tgz rubygems-1.3.3/ ... rubygems-1.3.3/util/gem_prelude.rb.template $ ls -l total 296 drwxr-xr-x 8 peterv peterv 4096 2009-05-05 01:04 rubygems-1.3.3 -rw-r--r-- 1 peterv peterv 292766 2009-05-05 01:07 rubygems-1.3.3.tgz $ cd rubygems-1.3.3/ $ sudo ruby setup.rb Installing RubyGems Installing gem executable Removing old source_cache files Removing old RubyGems RDoc and ri Installing rubygems-1.3.3 ri into /usr/lib/ruby/gems/1.8/doc/rubygems-1.3.3/ri Installing rubygems-1.3.3 rdoc into /usr/lib/ruby/gems/1.8/doc/rubygems-1.3.3/rdoc ------------------------------------------------------------------------------ = Announce: RubyGems Release 1.3.3 ...
This by default installs as the command gem1.8 ; you may want to symlink
the 'gem' command to gem1.8 (again, I did not check how this would collide
with the 'gem' (graphics related) package on Ubuntu/Debian ...
$ sudo ln -s /usr/bin/gem1.8 /usr/bin/gem
BTW, a similar convention is implemented natively by Ubuntu Jaunty
for ruby, rdoc, irb, ...
$ ls -l /usr/bin/rdoc lrwxrwxrwx 1 root root 7 2009-05-27 22:20 /usr/bin/rdoc -> rdoc1.8 $ ls -l /usr/bin/irb lrwxrwxrwx 1 root root 6 2009-05-27 22:20 /usr/bin/irb -> irb1.8 $ ls -l /usr/bin/ruby lrwxrwxrwx 1 root root 7 2009-05-27 22:20 /usr/bin/ruby -> ruby1.8 $ ls -l /usr/bin/gem lrwxrwxrwx 1 root root 15 2009-05-27 22:27 /usr/bin/gem -> /usr/bin/gem1.8
Updating gem to the latest version:
$ sudo gem update --system
Updating RubyGems
Nothing to update
5. Install Rails (with gem)
Again, we are using the gem version of Rails here (not the Ubuntu/Debian
package manager version). This flies in the face of Ubuntu/Debian
package management philosophy, but it's how Rails is working at this
time.
$ sudo gem install rails
[sudo] password for peterv:
Successfully installed rake-0.8.7
Successfully installed activesupport-2.3.2
Successfully installed activerecord-2.3.2
Successfully installed actionpack-2.3.2
Successfully installed actionmailer-2.3.2
Successfully installed activeresource-2.3.2
Successfully installed rails-2.3.2
7 gems installed
Installing ri documentation for rake-0.8.7...
Installing ri documentation for activesupport-2.3.2...
Installing ri documentation for activerecord-2.3.2...
Installing ri documentation for actionpack-2.3.2...
Installing ri documentation for actionmailer-2.3.2...
Installing ri documentation for activeresource-2.3.2...
Installing ri documentation for rails-2.3.2...
Installing RDoc documentation for rake-0.8.7...
Installing RDoc documentation for activesupport-2.3.2...
Installing RDoc documentation for activerecord-2.3.2...
Installing RDoc documentation for actionpack-2.3.2...
Installing RDoc documentation for actionmailer-2.3.2...
Installing RDoc documentation for activeresource-2.3.2...
Installing RDoc documentation for rails-2.3.2...
6. Install postgresql
$ sudo apt-get install postgresql
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
libpq5 postgresql-8.3 postgresql-client-8.3 postgresql-client-common
postgresql-common
Suggested packages:
oidentd ident-server postgresql-doc-8.3
The following NEW packages will be installed:
libpq5 postgresql postgresql-8.3 postgresql-client-8.3 postgresql-client-common
postgresql-common
...
Check that postgres is really listening on port 5432:
$ netstat -atn | grep -v tcp6 | grep 5432
tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN
Make first database user (a superuser is required for practical migrations) in postgres:
peterv@e6500:~$ sudo su postgres
postgres@e6500:/home/peterv$ createuser peterv
Shall the new role be a superuser? (y/n) y
postgres@e6500:/home/peterv$ exit
exit
peterv@e6500:~$
Create a first development database:
peterv@e6500:~$ psql template1
Welcome to psql 8.3.7, the PostgreSQL interactive terminal.
...
template1=# \l
List of databases
Name | Owner | Encoding
-----------+----------+----------
postgres | postgres | UTF8
template0 | postgres | UTF8
template1 | postgres | UTF8
(3 rows)
template1=# CREATE DATABASE rails_development;
CREATE DATABASE
template1=# \l
List of databases
Name | Owner | Encoding
-------------------+----------+----------
postgres | postgres | UTF8
rails_development | peterv | UTF8
template0 | postgres | UTF8
template1 | postgres | UTF8
(4 rows)
template1=# \q
7. Install the gem for connecting from ruby to postgres
$ sudo gem install postgres
Building native extensions. This could take a while...
Successfully installed postgres-0.7.9.2008.01.28
1 gem installed
Installing ri documentation for postgres-0.7.9.2008.01.28...
Installing RDoc documentation for postgres-0.7.9.2008.01.28...
8. Instantiate a first Rails application
$ rails new_rails_app
create
create app/controllers
...
create log/test.log
$ cd new_rails_app/
.../new_rails_app$ ls -l
total 60
drwxr-xr-x 6 peterv peterv 4096 2009-05-27 23:13 app
drwxr-xr-x 5 peterv peterv 4096 2009-05-27 23:13 config
drwxr-xr-x 2 peterv peterv 4096 2009-05-27 23:13 db
drwxr-xr-x 2 peterv peterv 4096 2009-05-27 23:13 doc
drwxr-xr-x 3 peterv peterv 4096 2009-05-27 23:13 lib
drwxr-xr-x 2 peterv peterv 4096 2009-05-27 23:13 log
drwxr-xr-x 5 peterv peterv 4096 2009-05-27 23:13 public
-rw-r--r-- 1 peterv peterv 307 2009-05-27 23:13 Rakefile
-rw-r--r-- 1 peterv peterv 10011 2009-05-27 23:13 README
drwxr-xr-x 3 peterv peterv 4096 2009-05-27 23:13 script
drwxr-xr-x 7 peterv peterv 4096 2009-05-27 23:13 test
drwxr-xr-x 6 peterv peterv 4096 2009-05-27 23:13 tmp
drwxr-xr-x 3 peterv peterv 4096 2009-05-27 23:13 vendor
Set the database.yml connection to the postgres database:
.../new_rails_app$ vim config/database.yml
.../new_rails_app$ cat config/database.yml
development:
adapter: postgresql
database: rails_development
username: peterv
encoding: utf8
pool: 5
9. Run the Rails application in development mode
.../new_rails_app$ ./script/server webrick => Booting WEBrick => Rails 2.3.2 application starting on http://0.0.0.0:3000 => Call with -d to detach => Ctrl-C to shutdown server [2009-05-27 23:41:48] INFO WEBrick 1.3.1 [2009-05-27 23:41:48] INFO ruby 1.8.7 (2008-08-11) [i486-linux] [2009-05-27 23:41:53] INFO WEBrick::HTTPServer#start: pid=21031 port=3000
Check the site on the URL http://localhost:3000/ .
From now on, this is Rails a usual, not Ubuntu specific anymore.
Certainly check the "Ruby on Rails Guides" and books from
"Pragmatic Programmers" for very interesting further information.