Robby on Rails: Installing Ruby on Rails and PostgreSQL on OS X, Third Editionthoughts.sort_by{|t| t[:topic]}.collect tag:www.robbyonrails.com,2005:TypoTypo2010-02-08T13:18:47-05:00Robby Russellurn:uuid:73dcd126-1333-417e-9203-aaefb22a65b12008-01-22T11:55:00-05:002010-02-08T13:18:47-05:00Installing Ruby on Rails and PostgreSQL on OS X, Third Edition<p>Over the past few years, I’ve helped you walk through the process of getting Ruby on Rails up and running on Mac <span class="caps">OS X</span>. The last version has been getting a lot of comments related to issues with the new Apple Leopard, so I’m going this post will expand on previous installation guides with what’s working for me as of January 2008.</p>
<p>The following guide is how our development team at <a href="http://planetargon.com">Planet Argon</a> prefers to setup our development workstations</p>
<div class="warning">
<strong><span class="caps"><span class="caps">WARNING</span></span>:</strong> This post contains some outdated instructions. Please read <a href="http://www.robbyonrails.com/articles/2010/02/08/installing-ruby-on-rails-passenger-postgresql-mysql-oh-my-zsh-on-snow-leopard-fourth-edition">Installing Ruby on Rails, Passenger, PostgreSQL, MySQL, Oh My Zsh on Snow Leopard, Fourth Edition </a>, which is focused on Installing Ruby on Rails on Snow Leopard.
</div>
<p>During this installation, we’ll have what we feel is the optimal development stack for building <a href="http://rubyonrails.org">Ruby on Rails</a> applications with our favorite database server, <a href="http://postgresql.org">PostgreSQL</a>.</p>
<p>Ready? Let’s get started…</p><p>Over the past few years, I’ve helped you walk through the process of getting Ruby on Rails up and running on Mac <span class="caps">OS X</span>. The last version has been getting a lot of comments related to issues with the new Apple Leopard, so I’m going this post will expand on previous installation guides with what’s working for me as of January 2008.</p>
<p>The following guide is how our development team at <a href="http://planetargon.com">Planet Argon</a> prefers to setup our development workstations</p>
<div class="warning">
<strong><span class="caps"><span class="caps">WARNING</span></span>:</strong> This post contains some outdated instructions. Please read <a href="http://www.robbyonrails.com/articles/2010/02/08/installing-ruby-on-rails-passenger-postgresql-mysql-oh-my-zsh-on-snow-leopard-fourth-edition">Installing Ruby on Rails, Passenger, PostgreSQL, MySQL, Oh My Zsh on Snow Leopard, Fourth Edition </a>, which is focused on Installing Ruby on Rails on Snow Leopard.
</div>
<p>During this installation, we’ll have what we feel is the optimal development stack for building <a href="http://rubyonrails.org">Ruby on Rails</a> applications with our favorite database server, <a href="http://postgresql.org">PostgreSQL</a>.</p>
<p>Ready? Let’s get started…</p>
<h2>Phase One</h2>
<p>During this initial phase, we’re going to install the underlying dependencies that we’ll be building off of.</p>
<h3>XCode 3.0</h3>
<p>The first thing that you’ll need to install to get far with this process is XCode tools, which is distributed by Apple. You can find this on the <span class="caps">DVD</span> that your Leopard installer is on. You can also download the latest version from Apple’s developer site.</p>
<ul>
<li><a href="http://developer.apple.com/tools/download/">http://developer.apple.com/tools/download/</a></li>
</ul>
<p>The current version (3.0) is 1.1 GB.. so the download time will vary depending on your connection speed. I would encourage you to drink some tea and/or <a href="http://www.amazon.com/gp/product/0684868768?ie=UTF8&tag=robonrai-20&linkCode=as2&camp=1789&creative=9325&creativeASIN=0684868768">read a book</a><img src="http://www.assoc-amazon.com/e/ir?t=robonrai-20&l=as2&o=1&a=0684868768" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /></p>
<p>Once you finish the installation, you can move forward. The rest of these installation <strong>will not work</strong> until XCode is installed. :-)</p>
<h3>MacPorts</h3>
<p>In this next step, we’ll install <a href="http://www.macports.org/">MacPorts</a> (formerly known as DarwinPorts). The MacPorts web site describes itself as, <em>“an open-source community initiative to design an easy-to-use system for compiling, installing, and upgrading either command-line, <span class="caps">X11</span> or Aqua based open-source software on the Mac <span class="caps">OS X</span> operating system.”</em></p>
<p><img src="http://img.skitch.com/20080122-jxqkyy8hc8ug7qxy4jt6qeg3d1.jpg" alt="" /></p>
<p>This tool is about to become one of the most important tools on your operating system as it’ll be used time and time again to maintain your libraries and many of the Unix tools that you’ll be using. If you’re from the Linux or <span class="caps">BSD</span> world, you are likely familiar with similar tools… such as: apt-get, port, and yum.</p>
<p>First, you’ll want to download MacPorts and install the “dmg” disk file for Leopard at the following link.</p>
<ul>
<li><a href="http://www.macports.org/install.php">http://www.macports.org/install.php</a></li>
</ul>
<p>Once downloaded, you’ll want to run the installer and install it on your workstation.</p>
<div class="thumbnail"><a href="http://skitch.com/robbyrussell/fdqg/install-macports-1.6.0"><img src="http://img.skitch.com/20080122-efm4gb9pbb79p4ujya1ceisn37.preview.jpg" alt="Install MacPorts-1.6.0" /></a></div>
<p>Work you way through the installer until successfully installed.</p>
<div class="thumbnail"><a href="http://skitch.com/robbyrussell/fdqe/install-macports-1.6.0"><img src="http://img.skitch.com/20080122-rr9e1begkg73ixt11d697wpdfh.preview.jpg" alt="Install MacPorts-1.6.0" /></a></div>
<p>Once this finishes, you can open up your favorite terminal application and run the following to test that it installed properly.</p>
<p>In my case, I’m now using Terminal.app.</p>
<p>Issue the command: <code>/opt/local/bin/port version</code></p>
<div class="thumbnail"><a href="http://skitch.com/robbyrussell/fdqm/opt-local-bin-port-version"><img src="http://img.skitch.com/20080122-piqes1e66rgj1bui7eud9sisf7.preview.jpg" alt="_opt_local_bin_port version" /></a></div>
<p>If it responds with a version number like mine did in the screenshot above, we’re moving along nicely.</p>
<h3>Environment Paths</h3>
<p>When we install MacPorts, the command to install/update ports installed to <code>/opt/local/bin</code>. We had to provide the entire path as this isn’t currently showing up in the default <code>$PATH</code> on Leopard. Let’s quickly remedy this by modifying the file <code>/etc/profile</code>.</p>
<p>If you have Textmate installed, you can run the following from your terminal: <code>mate /etc/profile</code></p>
<p>Add the following line to the bottom of <code>/etc/profile</code>.</p>
<pre><code>export PATH=/opt/local/bin:/opt/local/sbin:$PATH</code></pre>
<div class="thumbnail"><a href="http://skitch.com/robbyrussell/fdxb/profile"><img src="http://img.skitch.com/20080122-mqk8awqpbeebmdq7p7r1gyixsy.preview.jpg" alt="profile" /></a></div>
<p>You can use your favorite editor to update this file. Once you save it, you’ll want to restart your terminal application (or open a new tab) to create a new session. When your new terminal opens, run the following to verify that <code>port</code> is showing up in your <code>$PATH</code>.</p>
<p><code>which port</code></p>
<p>You should see <code>/opt/local/bin/port</code> show up as the result of this command.</p>
<div class="thumbnail"><a href="http://skitch.com/robbyrussell/fdx8/which-port"><img src="http://img.skitch.com/20080122-d4hte4cm5gn67a4cum26gbibut.preview.jpg" alt="which port" /></a></div>
<p>Great, let’s continue to move forward.</p>
<h3>Hiding Apple’s Ruby, Gems, and Rails</h3>
<p>Before we install Ruby from MacPorts, we’ll go ahead and hide Apple’s Ruby installations.</p>
<pre><code>
:~ robbyrussell$ sudo su -
Password:
:~ root# mv /usr/bin/ruby /usr/bin/ruby.orig
:~ root# mv /usr/bin/gem /usr/bin/gem.orig
:~ root# mv /usr/bin/rails /usr/bin/rails.orig
:~ root# logout
</code></pre>
<div class="thumbnail"><a href="http://skitch.com/robbyrussell/fdxe/hiding-apples-ruby"><img src="http://img.skitch.com/20080122-rgmsuy746h73b1bc93j4nyi5ar.preview.jpg" alt="hiding apples ruby" /></a></div>
<p>If you ever decide to remove MacPorts, you can just rename <code>ruby.orig</code> back to <code>ruby</code> and you’re back where you started… and the same for the others listed.</p>
<h2>Phase Two</h2>
<p>During this next phase, we’re going to install Ruby and Ruby on Rails.</p>
<h3>Installing Ruby via MacPorts</h3>
<p>Now that we have MacPorts up and running, we’re going to use it for the first time. We’ll start by using it to install Ruby and the Rubygems package.</p>
<pre><code>$ sudo port install ruby rb-rubygems</code></pre>
<p>Okay, this will take a little while. I’d suggest that you step out to get some fresh air.</p>
<p>How was it outside? What’s the weather like there today? It’s currently 2:30am <span class="caps">PST</span> so it’s dark and an 28F outside so I didn’t stay outside very long.</p>
<p>If you’re still waiting for it to install, perhaps you could watch the following video. I might encourage you to check out more of <a href="http://en.wikipedia.org/wiki/Jam_%28TV_series%29">Jam</a>, which was recommended a few years ago to me by <a href="http://interblah.net/">James Adam</a> at <a href="http://www.canadaonrails.org/">Canada on Rails</a>.</p>
<object width="425" height="355"><param name="movie" value="http://www.youtube.com/v/sLD0SNCFtyA&rel=1"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/sLD0SNCFtyA&rel=1" type="application/x-shockwave-flash" wmode="transparent" width="425" height="355"></embed></object>
<p>Be warned… it’s a strange show, but I find strange things like this funny. :-)</p>
<p>If you prefer something a bit more lighthearted…</p>
<object width="425" height="355"><param name="movie" value="http://www.youtube.com/v/SO5WoLnOOlU&rel=1"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/SO5WoLnOOlU&rel=1" type="application/x-shockwave-flash" wmode="transparent" width="425" height="355"></embed></object>
<p>Okay… when Ruby finishes installing, you’ll want to test that you can run it.</p>
<pre><code>$ ruby -v</code></pre>
<p>Great, let’s move forward!</p>
<h3>Installing Ruby on Rails via RubyGems</h3>
<p>We’re now going to install the libraries that make up Ruby on Rails via RubyGems.</p>
<pre><code>$ sudo gem install --include-dependencies rails</code></pre>
<p>This will install the following gems.</p>
<ul>
<li>rails-2.0.2</li>
<li>rake-0.8.1</li>
<li>activesupport-2.0.2</li>
<li>activerecord-2.0.2</li>
<li>actionpack-2.0.2</li>
<li>actionmailer-2.0.2</li>
<li>activeresource-2.0.2</li>
</ul>
<p>Excellent, let’s move forward…</p>
<p>If you haven’t already purchased it, I recommend that you take a look at <a href="http://www.amazon.com/gp/product/0321445619?ie=UTF8&tag=robonrai-20&linkCode=as2&camp=1789&creative=9325&creativeASIN=0321445619">The Rails Way (Addison-Wesley Professional Ruby Series)</a><img src="http://www.assoc-amazon.com/e/ir?t=robonrai-20&l=as2&o=1&a=0321445619" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /> by Obie Fernandez.</p>
<h3>Installing Mongrel via RubyGems</h3>
<p>Let’s now install Mongrel, which is an excellent Ruby-based web server for Ruby on Rails applications. We use it in development and production at <a href="http://planetargon.com">Planet Argon</a> and it’s also what we recommend to our <a href="http://planetargon.com/hosting.html">hosting customers</a>.</p>
<pre><code>$ sudo gem install --include-dependencies mongrel mongrel_cluster</code></pre>
<ul>
<li><strong>Note:</strong> Be sure to select the proper platform for mongrel. (hint: <span class="caps">OS X</span> is <span class="caps">NOT</span> mswin32)</li>
</ul>
My terminal output:
<pre><code>
Select which gem to install for your platform (i686-darwin9.1.0)
1. mongrel 1.1.3 (java)
2. mongrel 1.1.3 (i386-mswin32)
3. mongrel 1.1.3 (ruby)
4. mongrel 1.1.2 (ruby)
5. mongrel 1.1.2 (mswin32)
6. mongrel 1.1.2 (java)
7. Skip this gem
8. Cancel installation
> 3
Select which gem to install for your platform (i686-darwin9.1.0)
1. fastthread 1.0.1 (mswin32)
2. fastthread 1.0.1 (ruby)
3. Skip this gem
4. Cancel installation
> 2
Building native extensions. This could take a while...
Building native extensions. This could take a while...
Successfully installed mongrel-1.1.3
Successfully installed gem_plugin-0.2.3
Successfully installed daemons-1.0.9
Successfully installed fastthread-1.0.1
Successfully installed cgi_multipart_eof_fix-2.5.0
Installing ri documentation for mongrel-1.1.3...
Installing ri documentation for gem_plugin-0.2.3...
Installing ri documentation for daemons-1.0.9...
Installing ri documentation for fastthread-1.0.1...
No definition for dummy_dump
No definition for dummy_dump
No definition for rb_queue_marshal_load
No definition for rb_queue_marshal_dump
Installing ri documentation for cgi_multipart_eof_fix-2.5.0...
Installing RDoc documentation for mongrel-1.1.3...
Installing RDoc documentation for gem_plugin-0.2.3...
Installing RDoc documentation for daemons-1.0.9...
Installing RDoc documentation for fastthread-1.0.1...
No definition for dummy_dump
No definition for dummy_dump
No definition for rb_queue_marshal_load
No definition for rb_queue_marshal_dump
Installing RDoc documentation for cgi_multipart_eof_fix-2.5.0...
Successfully installed mongrel_cluster-1.0.5
</code></pre>
<p>Great, you have almost all of the essentials.. except a database.</p>
<h2>Phase Three</h2>
<p>In this phase, we’re going to get our database server, PostgreSQL, installed and the libraries that Ruby needs to communicate with it.</p>
<h3>Installing PosgreSQL with MacPorts</h3>
<p>At <a href="http://planetargon.com">Planet Argon</a>, we design and develop our applications on top of <a href="http://postgresql.org">PostgreSQL</a>. I’ve been advocating the adoption of this awesome open source database in the Rails community for quite some time now.</p>
<p>The current version available of PostgreSQL via MacPorts is 8.3, which is what we’ll now install with the <code>port</code> command.</p>
<p><code>$ sudo port install postgresql83 postgresql83-server</code></p>
<p>This will download and install the necessary libraries to run PostgreSQL server and the client utilities.</p>
<h3>Configuring PostgreSQL</h3>
<p>When PostgreSQL is finished installing, it’ll tell you to run the following commands to create a new database instance.</p>
<pre><code>
sudo mkdir -p /opt/local/var/db/postgresql83/defaultdb
sudo chown postgres:postgres /opt/local/var/db/postgresql83/defaultdb
sudo su postgres -c '/opt/local/lib/postgresql83/bin/initdb -D /opt/local/var/db/postgresql83/defaultdb'
</code></pre>
<h4>Adding PostgreSQL to launchd</h4>
<p>If you’d like to have PostgreSQL automatically startup after a system restart, you can load it into launchd, which comes with <span class="caps">OS X</span>. By running the following command, PostgreSQL will startup automatically on the next system restart.</p>
<pre><code>sudo launchctl load -w /Library/LaunchDaemons/org.macports.postgresql83-server.plist</code></pre>
<h4>Adding PostgreSQL to your $PATH</h4>
<p>For some reason, the MacPort for PostgreSQL doesn’t get the programs in your path automatically, so we’ll it now.</p>
<p><code>mate /etc/profile</code></p>
<p>Modify the <code>PATH</code> that we changed earlier to include /opt/local/lib/postgresql83/bin@.</p>
<pre><code>export PATH=/opt/local/bin:/opt/local/sbin:/opt/local/lib/postgresql83/bin:$PATH</code></pre>
<p>Save the file and then open a new terminal. To test this, you should get the following output when you run which <code>psql</code>.</p>
<pre><code>
$ which psql
/opt/local/lib/postgresql83/bin/psql
</code></pre>
<h4>Creating a new PostgreSQL user</h4>
<p>When I’m working on Rails applications in my development environment, I really don’t want to have to specify a username and password in every <code>config/database.yml</code> file for each of our ongoing client projects. When PostgreSQL was installed, it created a superuser named <strong>postgres</strong>, which is great, but I’d like one that matches my system username, so that I’m not prompted at all for a username or password to connect to PostgreSQL.</p>
<p>To do this, we’ll use the <code>createuser</code> command, which comes with PostgreSQL. As you can see, I’m creating a new user with <code>superuser</code> privileges (and will hopefully be the last time I have to do a <code>-U postgres</code>).</p>
<pre><code>
$ createuser --superuser robbyrussell -U postgres
CREATE ROLE
</code></pre>
Let’s take a quick moment to test this out.
<pre><code>
# create a new database
$ createdb my_test_db
CREATE DATABASE
# drop the database
$ dropdb my_test_db
DROP DATABASE
</code></pre>
<p>Great, everything looks good here.</p>
<p>We now have a running installation of PostgreSQL with a new user account. All we need to do now is install the appropriate RubyGem to allow our Ruby applications to connect to it.</p>
<h4>Installing PostgreSQL Libraries for Ruby</h4>
<p>You can install postgres gem by running the following command.</p>
<pre><code>$ sudo gem install --include-dependencies postgres</code></pre>
<p>Great. We’ve now built a professional development environment for working with Ruby on Rails. Can you <em>feel</em> the excitement? :-)</p>
<h2>Closing Thoughts</h2>
<p>Like the previous versions, I hope that a few people find this useful. I didn’t have to make a lot of changes from the second edition, but there were enough to warrant a new post. I’ve been setting up my workstation like this for about three years now and I’m looking forward to seeing how a fresh install on Leopard works out for me.</p>
<p>If you have any problems, feel free to ask a question in the comments below.</p>Scofurn:uuid:0b426dfe-a293-44c2-9552-98e4636b4e6a2010-02-05T12:03:22-05:002010-02-06T07:39:11-05:00Comment on Installing Ruby on Rails and PostgreSQL on OS X, Third Edition by Scof<p>I also tried:</p>
<p>sudo port uninstall postgresql83 postgresql83-serverPassword:
Error: port uninstall failed: Registry error: postgresql83 not registered as installed.</p>
<p>but get the above error</p>
<p>But its odd since I did install it exactly as mentioned above through macports. :s</p>Scofurn:uuid:8ac913d3-1d17-48e6-aa80-a26231edaa812010-02-05T12:01:20-05:002010-02-06T07:39:11-05:00Comment on Installing Ruby on Rails and PostgreSQL on OS X, Third Edition by Scof<p>Hi,</p>
<p>I installed Ruby, Macports, Postgresql, etc. everything as according to your tutorial. I have a separate account that says PostgreSQL83\Server. Now I want to uninstall everything but dont know how. All the tutorials I have seen either to you by default installer (which I didnt use) and i can’t get to uninstall it even through <a href="http://www.postgresqlformac.com/news/archived_news/uninstall_835.html" rel="nofollow">http://www.postgresqlformac.com/news/archived_news/uninstall_835.html</a> as the folders in my case don’t exist at all. Please help. I don’t have much experience. Thank you. :)</p>Lance Carlsonurn:uuid:baa67be4-e50e-4ecc-b59e-027ddc3a534c2010-01-19T01:16:13-05:002010-02-06T07:56:11-05:00Comment on Installing Ruby on Rails and PostgreSQL on OS X, Third Edition by Lance Carlson<p>Homebrew is probably the easiest way to install it now.. yes?</p>