Read my latest article: Ezra Zygmuntowicz -- Farewell, Friend. (posted Mon, 01 Dec 2014 17:53:00 GMT)

Install Ruby, Rails, and PostgreSQL on OSX

Posted by Mon, 29 May 2006 14:46:00 GMT

15 comments Latest by Manjoor Thu, 13 Jul 2006 11:00:30 GMT

WARNING: This post contains some outdated instructions. Please read Installing Ruby on Rails and PostgreSQL on OS X, Second Edition.

Our Creative Director, Allison Beckwith, picked up a new black MacBook this weekend and I had the luxury of getting it setup to model our standard setup. We all try to keep our setups fairly similar so that we don’t hit too many issues when working together on projects.

I’ll try to keep this short and to the point… because if you’re like me… you just want to start playing with Rails! ;-)

The steps I followed to get her setup like the rest of the development team at PLANET ARGON went something like this.

XCode and DarwinPorts

  • Download and install iterm (the Universal dmg)
  • Download and install XCode tools from Apple (dmg)
  • Download and install DarwinPorts (dmg)
  • Start up iterm.
In this step we are going to modify the default bash profile so that every user on the machine that uses bash will get the path for darwinports in their bash_profile.
sudo vi /etc/profile

Modify the following line to include /opt/local/bin in the PATH… save the file (see vim documentation for details)


  PATH="/bin:/sbin:/opt/local/bin:/usr/bin:/usr/sbin" 

Ruby and Rails

  • Open up a new iterm tab (apple-t)
  • Install ruby with darwinports with: sudo port install ruby rb-rubygems
  • Install Ruby on Rails and all its dependencies with: sudo gem install -y rails

PostgreSQL and Ruby libs

  • Install PostgreSQL8 with: sudo port install postgresql8
  • We need to modify the /etc/profile file again because the postgresql8 install doesn’t add programs like pg_ctl to /opt/local/bin. Change the PATH to now look like this and save.

  PATH="/bin:/sbin:/opt/local/bin:/usr/bin:/usr/sbin:/opt/local/lib/pgsql8/bin" 
  • Install the postgres gem with: sudo gem install postgres
    • Oh NO!!! You should see an error about it not finding libraries… what will we do?

  cd /opt/local/lib/ruby/gems/1.8/gems/postgres-0.7.1
  sudo ruby extconf.rb --with-pgsql-include=/opt/local/include/pgsql8 --with-pgsql-lib=/opt/local/lib/pgsql8
  sudo make && sudo make install
  # for good measure...
  sudo gem install postgres

Successfully installed postgres-0.7.1

Configure PostgreSQL for single user

In our development environments, we don’t find it necessary to keep PostgreSQL running all the time on our servers. We only want it running when we’re doing development. We also typically install it per user on a machine to keep us from needing things like usernames and passwords to connect to it from an application we’re running on the machine. Let’s setup PostgreSQL the PLANET ARGON way!

  • Open up iterm and go to your home directory
  • Init your new PostgreSQL database with: initdb -D pgdata
  • Start up PostgreSQL with: pg_ctl -D pgdata -l pgdata/psql.log start
  • Create a new database with: createdb argon_development
  • Test the new database with: psql argon_development
  • Did it load up your new database? If so, great! If not… check your steps… :-)

Test Rails + PostgreSQL

  • Navigate to a directory where you don’t mind sticking projects… mkdir development; cd development
  • Generate a new Rails application with: rails -d postgresql argon
  • Navigate to new Rails application directory. cd argon
  • Generate a new model to test with: ./script/generate model Argonista
  • Edit and save the migration that was generated ( db/migrate/001_create_argonistas.rb ) file with your favorite editor…

  class CreateArgonistas < ActiveRecord::Migration
    def self.up
      create_table :argonistas do |t|
        t.column :name, :string
        t.column :blog_url, :string
      end
    end

    def self.down
      drop_table :argonistas
    end
  end
  • Edit config/database.yml to look like the following… you’ll notice that we don’t need to supply a username or password.

  development:
    adapter: postgresql
    database: argon_development

  test:
    adapter: postgresql 
    database: argon_test

  production:
    adapter: postgresql
    database: argon_production
* Run the migration with: rake db:migrate

  $ rake db:migrate
  (in /Users/allisonbeckwith/development/argon)
  == CreateArgonistas: migrating ================================================
  -- create_table(:argonistas)
  NOTICE:  CREATE TABLE will create implicit sequence "argonistas_id_seq" for serial column "argonistas.id" 
  NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "argonistas_pkey" for table "argonistas" 
     -> 0.0399s
  == CreateArgonistas: migrated (0.0402s) =======================================
* Test your new model from console

  $ ./script/console 
  Loading development environment.
  >> a = Argonista.new
  => #<Argonista:0x24569dc @attributes={"name"=>nil, "blog_url"=>nil}, @new_record=true>
  >> a.name = 'Robby'
  => "Robby" 
  >> a.blog_url = 'http://www.robbyonrails.com'
  => "http://www.robbyonrails.com" 
  >> a.save
  => true
  >> exit
* Great, let’s go look at our database table…

  $ psql argon_development
  Welcome to psql 8.1.3, the PostgreSQL interactive terminal.

  Type:  \copyright for distribution terms
         \h for help with SQL commands
         \? for help with psql commands
         \g or terminate with semicolon to execute query
         \q to quit

  argon_development=# SELECT * FROM argonistas;
   id | name  |          blog_url           
  ----+-------+-----------------------------
    1 | Robby | http://www.robbyonrails.com
  (1 row)

There we go, we’ve setup Ruby, Rails, and PostgreSQL on a brand new Intel MacBook without breaking a sweat!

Extra Goodies

  • Subversion: sudo port install subversion
  • Lighttpd: sudo port install lighttpd
  • ImageMagick: sudo port install ImageMagick (known to take a while…)
  • GraphicsMagick: sudo port install GraphicsMagick
  • Install the rmagick gem: sudo gem install rmagick

Have fun!

Subscribe to my RSS feed Enjoying the content? Be sure to subscribe to my RSS feed.
comments powered by Disqus
Comments

Leave a response

  1. Avatar
    atmos Mon, 29 May 2006 16:30:27 GMT

    I did a similar writeup but did a few things differently. :) You can check out my macbook pro how-to here .

  2. Avatar
    Xenith Mon, 29 May 2006 21:21:14 GMT

    There’s no need to recompile the postgres gem. You can pass the configure options straight to the gem using -- as so:

    sudo gem install postgres -- --with-pgsql-include=/opt/local/include/pgsql8 --with-pgsql-lib=/opt/local/lib/pgsql8
  3. Avatar
    David Tue, 30 May 2006 14:36:32 GMT

    Just a minor correction: It’s spelled Xcode, not XCode. Seriously, nobody calls you RObby, do they?

  4. Avatar
    Jacob Harris Tue, 30 May 2006 19:02:36 GMT

    FYI, there was an issue where Postgresql would fight with iChat for shared memory when you attempted to do a video chat (it would get a signal fine from the iSight for preview, but then complain there was no signal when a chat is initiated). There is a fix on the Interwebs, but I figured I’d let you know in case allisbe’s iChat starts acting up.

  5. Avatar
    Al Hulaton Wed, 31 May 2006 14:40:42 GMT

    Very timely article. I just got a Macbook Pro and was about to muddle through the postgres install. Now I can just copy your homework. Thanks!

  6. Avatar
    David Salgado Sat, 03 Jun 2006 08:47:44 GMT

    Very useful article – many thanks.

    On my Powerbook running Panther, I got a complaint about autoconf versions when trying to do “sudo port install ruby rb-rubygems”.

    To fix this, simply;

    sudo port install autoconf
    sudo port install ruby rb-rubygems

    Thanks again.

    David

  7. Avatar
    Jim Greer Wed, 07 Jun 2006 04:21:04 GMT

    Works for me!

  8. Avatar
    Chris Wed, 14 Jun 2006 19:10:28 GMT

    For a new developer, this site is so useful. Tips like these that help me streamline my own forming processes. Thanks!

  9. Avatar
    zorph Thu, 22 Jun 2006 02:28:18 GMT

    Leaving out the username and password in database.yml caused an error when I tried to rake migrate. When I put the username and password back in, it migrated! (I’m setting up rails/postgresql on windows XP though)

  10. Avatar
    chris Sat, 24 Jun 2006 18:54:05 GMT

    Thanks for the help.

    Good stuff.

  11. Avatar
    Joe Sun, 25 Jun 2006 20:19:03 GMT

    Well, since it’s aPple, why isn’t it xCode?

  12. Avatar
    Dan B Sun, 09 Jul 2006 21:05:47 GMT

    Nice writeup. I’m having problems with getting ruby and postgresql installed. They both depend on readline and darwinports cannot install readline:

    mac:~ mac$ sudo port install readline Password:

    -> Fetching readline -> Attempting to fetch readline51-001 from ftp://ftp.cwru.edu/pub/bash/readline-5.1-patches/ -> Attempting to fetch readline51-001 from http://distfiles-od.opendarwin.org/ -> Attempting to fetch readline51-001 from http://distfiles-msn.opendarwin.org/ -> Attempting to fetch readline51-001 from http://distfiles-bay13.opendarwin.org/ Error: Target com.apple.fetch returned: fetch failed Error: /opt/local/bin/port: Status 1 encountered during processing. mac:~ mac$

    I did find a readline tar-ball here: ftp://ftp.gnu.org/gnu/readline/readline-5.1.tar.gz

    Is there any way I can graft it on to /opt/local so that ports [ ruby for example ], which depend on it can get installed?

    Thanks, -Dan

  13. Avatar
    Manjoor Thu, 13 Jul 2006 10:58:26 GMT

    hi isr i am manjoor alihere i want asoftware Cd of postgreSQl version 8.1.3 sir plz help me to run my project to it because it uninstall on my PC your friend Er. Manjoor Ali

  14. Avatar
    Manjoor Thu, 13 Jul 2006 10:59:03 GMT

    hi

  15. Avatar
    Manjoor Thu, 13 Jul 2006 11:00:30 GMT

    mail me at manzoor@aboutd.com phone me at +91-9829298623 india. i wat ur mail or phone