1

Installing Ruby on Rails in FreeBSD with Postgres

Posted by Jason Noble on 09/02/2008 in FreeBSD, postgres, ruby, ruby on rails |

After setting up a minimal FreeBSD install, I want to get Ruby on Rails up and running.

Let’s see what’s available in the ports tree.

[text]
freebsd-vm# make search key=gem | grep Path: | grep ruby
Path: /usr/ports/audio/rubygem-mp3info
Path: /usr/ports/benchmarks/rubygem-railsbench
Path: /usr/ports/databases/ruby-dbd_pg
Path: /usr/ports/databases/ruby-rdbc1
Path: /usr/ports/databases/rubygem-activerecord
[…]
Path: /usr/ports/devel/ruby-gemfinder
Path: /usr/ports/devel/ruby-gems
Path: /usr/ports/devel/rubygem-activesupport
[…]
Path: /usr/ports/www/rubygem-scrubyt
Path: /usr/ports/www/rubygem-taggable
freebsd-vm#
[/text]

The ruby-gems port looks promising.

[text]
freebsd-vm# cd devel/ruby-gems
freebsd-vm# cat pkg-descr
a package management framework for the Ruby programming language
An application or library is packaged into a gem, which is
a single installation unit.
RubyGems entirely manages its own filesystem space, rather
than installing files into the "usual" places. This enables
greater functionality and reliability.

Using RubyGems, you can:
– download and install Ruby libraries easily
– not worry about libraries A and B depending on
different versions of library C
– easily remove libraries you no longer use
– have power and control over your Ruby platform!

WWW: http://docs.rubygems.org/
freebsd-vm#
[/text]

Let’s install it. The install requires ruby to be installed, so it installs that for us (if it’s not currently installed). I enabled RDOC and DEBUG and disabled IPV6.
(This may take a while, go grab some coffee)

[text]
freebsd-vm# make install
=> rubygems-1.2.0.tgz doesn’t seem to exist in /usr/ports/distfiles/ruby.
=> Attempting to fetch from http://rubyforge.rubyuser.de/rubygems/.
rubygems-1.2.0.tgz 100% of 241 kB 138 kBps
===> Extracting for ruby18-gems-1.2.0_1
[…]
If `gem` was installed by a previous RubyGems installation, you may need
to remove it by hand.

===> Registering installation for ruby18-gems-1.2.0_1
freebsd-vm#
[/text]

Now we need to install rails.

[text]
freebsd-vm# gem install rails
Successfully installed rake-0.8.1
Successfully installed activesupport-2.1.0
Successfully installed activerecord-2.1.0
Successfully installed actionpack-2.1.0
[…]
Installing RDoc documentation for actionpack-2.1.0…
Installing RDoc documentation for actionmailer-2.1.0…
Installing RDoc documentation for activeresource-2.1.0…
freebsd-vm#
[/text]

We need to install postgresql-server.

[text]
freebsd-vm# make search key=postgres | grep Path: | grep server
Path: /usr/ports/databases/aolserver-nspostgres
Path: /usr/ports/databases/erserver
Path: /usr/ports/databases/postgresql73-server
Path: /usr/ports/databases/postgresql74-server
Path: /usr/ports/databases/postgresql80-server
Path: /usr/ports/databases/postgresql81-server
Path: /usr/ports/databases/postgresql82-server
Path: /usr/ports/databases/postgresql83-server
Path: /usr/ports/finance/tinyerp-server
Path: /usr/ports/net/sipxcommserverlib
Path: /usr/ports/net-im/iserverd
freebsd-vm#
[/text]

Let’s install version 8.3. In the options that came up, I selected Build with PAM support and left the other defaults.

[text]
freebsd-vm# cd databases/postgresql83-server/
freebsd-vm# make install
cd /usr/ports/databases/postgresql83-server && make config;
[…]
For more information, and contact details about the security
status of this software, see the following webpage:
http://www.postgresql.org/
freebsd-vm#
[/text]

We need to set postgres to run at startup.

[text]
freebsd-vm# echo ‘postgresql_enable="YES"’ >> /etc/rc.conf
freebsd-vm#
[/text]

Initialize the database.

[text]
freebsd-vm# /usr/local/etc/rc.d/postgresql initdb
The files belonging to this database system will be owned by user "pgsql".
This user must also own the server process.

The database cluster will be initialized with locale C.
The default text search configuration will be set to "english".

creating directory /usr/local/pgsql/data … ok
creating subdirectories … ok
selecting default max_connections … 40
selecting default shared_buffers/max_fsm_pages … 28MB/179200
creating configuration files … ok
creating template1 database in /usr/local/pgsql/data/base/1 … ok
initializing pg_authid … ok
initializing dependencies … ok
creating system views … ok
loading system objects’ descriptions … ok
creating conversions … ok
creating dictionaries … ok
setting privileges on built-in objects … ok
creating information schema … ok
vacuuming database template1 … ok
copying template1 to template0 … ok
copying template1 to postgres … ok

WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the -A option the
next time you run initdb.

Success. You can now start the database server using:

/usr/local/bin/postgres -D /usr/local/pgsql/data
or
/usr/local/bin/pg_ctl -D /usr/local/pgsql/data -l logfile start

freebsd-vm#
[/text]

Start up the database.

[text]
freebsd-vm# /usr/local/etc/rc.d/postgresql start
freebsd-vm#
[/text]

Verify that postgres is working.

[text]
freebsd-vm# createdb test -U pgsql
freebsd-vm# dropdb test -U pgsql
freebsd-vm#
[/text]

There is a postgres GEM. Let’s install it.

[text]
freebsd-vm# 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…
freebsd-vm#
[/text]

Now we can create a rails application (I switched to a non-root user).

[text]
freebsd-vm$ rails -d postgresql sample_rails_app
create
create app/controllers
create app/helpers
create app/models
create app/views/layouts
create config/environments
[…]
create log/server.log
create log/production.log
create log/development.log
create log/test.log
freebsd-vm$
[/text]

Setup the postgres user for your application

[text]
freebsd-vm$ createuser –username pgsql
Enter name of role to add: sample_rails_app
Shall the new role be a superuser? (y/n) n
Shall the new role be allowed to create databases? (y/n) y
Shall the new role be allowed to create more new roles? (y/n) n
freebsd-vm$ cd sample_rails_app/
freebsd-vm$ rake db:create
(in /usr/home/jasonn/sample_rails_app)
freebsd-vm$
[/text]

Create a User scaffold

[text]
freebsd-vm$ script/generate scaffold User username:string password:string homedir:string ftp_disabled:boolean
exists app/models/
exists app/controllers/
exists app/helpers/
create app/views/users
exists app/views/layouts/
exists test/functional/
exists test/unit/
exists public/stylesheets/
create app/views/users/index.html.erb
create app/views/users/show.html.erb
create app/views/users/new.html.erb
create app/views/users/edit.html.erb
create app/views/layouts/users.html.erb
create public/stylesheets/scaffold.css
create app/controllers/users_controller.rb
create test/functional/users_controller_test.rb
create app/helpers/users_helper.rb
route map.resources :users
dependency model
exists app/models/
exists test/unit/
exists test/fixtures/
create app/models/user.rb
create test/unit/user_test.rb
create test/fixtures/users.yml
create db/migrate
create db/migrate/20080902231538_create_users.rb
freebsd-vm$ rake db:migrate
(in /usr/home/jasonn/sample_rails_app)
== 20080902231538 CreateUsers: migrating ======================================
— create_table(:users)
NOTICE: CREATE TABLE will create implicit sequence "users_id_seq" for serial column "users.id"
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "users_pkey" for table "users"
-> 0.0645s
== 20080902231538 CreateUsers: migrated (0.0665s) =============================

freebsd-vm$
[/text]

Start up the server and visit http://localhost:3000/users (you may need to substitute your IP address)

[text]
freebsd-vm$ script/server
=> Booting WEBrick…
=> Rails 2.1.0 application started on http://0.0.0.0:3000
=> Ctrl-C to shutdown server; call with –help for options
[…]
[/text]

Create some users, your Rails app is up and running.

Updated 12/15/2011
– Updated outdated link to setting up FreeBSD in a parallels vm

1 Comment

  • Anonymous says:

    >I was just setting up postgres on a freebsd jail and had some problems connecting from rails, though psql worked fine. Turns out the problem is that you should use unix domain sockets so that it doesn't block the jail's IP, so I removed the host: localhost line from database.yml and it worked fine. Also, apparently pg is the new postgresql gem and the old postgres gem isn't developed anymore.

Comments are closed. Would you like to contact the author directly?

Copyright © 2008-2024 Jason Noble's Technical Adventures All rights reserved.
This site is using the Desk Mess Mirrored theme, v2.5, from BuyNowShop.com.