Robby on Rails: Installing Ruby on Rails, Passenger, PostgreSQL, MySQL, Oh My Zsh on Snow Leopard, Fourth Editionthoughts.sort_by{|t| t[:topic]}.collect tag:www.robbyonrails.com,2005:TypoTypo2010-02-08T13:26:46-05:00Robby Russellurn:uuid:2387d63c-c145-4f7e-ad77-e3aa9aabacef2010-02-08T13:14:00-05:002010-02-08T13:26:46-05:00Installing Ruby on Rails, Passenger, PostgreSQL, MySQL, Oh My Zsh on Snow Leopard, Fourth Edition<p>Welcome to what seems like my tenth installment (actually, it’s the fourth) of showing you how I setup my development environment on a fresh <span class="caps">OSX</span> install. In this case, I’m actually getting a MacBook setup for a new employee with Snow Leopard.</p>
<p>Over the years, I’ve evolved these following steps and they’ve helped our team maintain a consistent and stable envirnment for Ruby on Rails development. I know that there are a few other ways to approaching this and I’m sure you’ll get similar results, but this approach has allowed me to maintain a hassle-free setup for the last five years.</p>
<p>As with all things… your milage may vary.</p>
<h2>Phase One</h2>
<p>During this initial phase, we’re going to install the primary dependencies and setup our environment.</p>
<h3>XCode</h3>
<p>The first thing that you’ll need to do is install XCode, which almost everything depends upon as this will install developer-friendly tools for you. Apple has been kind enough to ship this on your Snow Leopard <span class="caps">DVD</span>.</p>
<p><img src="http://img.skitch.com/20100208-kmamssde4apw1qxcs6u991p518.jpg" alt="" /></p>
<p>Go ahead and install XCode from the <strong>Optional Installs</strong> folder.</p>
<p><img src="http://img.skitch.com/20100208-1up5s7ahybmryrerrxxwn1d7tu.jpg" alt="" /></p>
<p>(might require a reboot)</p>
<p>You can also <a href="http://developer.apple.com/technology/xcode.html">download it</a> online.</p>
<h3>MacPorts</h3>
<p>Now we’ll install MacPorts, which the web site describes itself as, “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.”</p>
<p>As I’ve said in past versions of this guide, this tool is about to become one of the most important tools on your operating system. 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: <code>apt-get</code>, <code>port</code>, and <code>yum</code>.</p>
<p>You’ll want to download the latest stable version from <a href="http://www.macports.org">http://www.macports.org/</a>. Once downloaded, you can install it.</p>
<p><img src="http://img.skitch.com/20100208-fd4h7rd16437yd63hf45mkn592.jpg" alt="" /></p>
<p>Once this is installed, you’ll be able to use the <code>port</code> command from your console.</p>
<h3>Wget</h3>
<p>Let’s test out your MacPorts install by installing a useful tool called wget, which we’ll use to install oh-my-zsh.</p>
<p><code>sudo port install wget</code></p>
<h3>Git and Subversion</h3>
<p>Every development environment should have some source code management tools available. We’ll install both of these with one command.</p>
<p><code>sudo port install git-core +svn</code></p>
<p>This will install git and subversion.</p>
<h3>oh-my-zsh</h3>
<p><a href="http://github.com/robbyrussell/oh-my-zsh">Oh My Zsh</a> is the most amazing thing to happen to shells since… well since I said so. It’s one of my open source projects that I encourage you to give a whirl.</p>
<p><code>wget http://github.com/robbyrussell/oh-my-zsh/raw/master/tools/install.sh -O - | sh</code></p>
<p>That’s it. The next time you open up your terminal, you’ll be running zsh with a bunch of stuff available. For more information, visit <a href="http://github.com/robbyrussell/oh-my-zsh">http://github.com/robbyrussell/oh-my-zsh</a>.</p>
<h3>Terminal theme (optional)</h3>
<p>I never understood why the icon for Terminal has a black background but when you start it up the default theme is black on white.</p>
<p><img src="http://img.skitch.com/20100208-1b92cbx8gtuttqsbdd49qspq7f.jpg" alt="" /></p>
<p>versus</p>
<p><img src="http://img.skitch.com/20100208-8dnmpswyumfk73h942g6u4fih4.jpg" alt="" /></p>
<p>Anyhow, I’m a fan of the the dark background. To change this, open up preferences in Terminal. Select <strong>Pro</strong>, then click on the <strong>Default</strong> window so that this sticks around.</p>
<p><img src="http://img.skitch.com/20100208-mr4q4y5btq7sscuh7ceky3ahgx.jpg" alt="" /></p>
<p>Let’s now open up a new Terminal window..</p>
<p>You should be looking at something like this:</p>
<p><img src="http://img.skitch.com/20100208-psmj7mqsx38j1i2nj75n6hbcg7.jpg" alt="" /></p>
<p>Much better… let’s continue.</p>
<h2>Phase Two</h2>
<p>We’re now going to start installing everything we need to get this running.</p>
<h3>Ruby 1.8.7.x</h3>
<p>First up, Ruby.</p>
<p>Snow Leopard includes Ruby and Rails already installed, but we’re going to back these up for a rainy day. Just issue these commands:</p>
<pre><code>
$ 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>
<p><img src="http://img.skitch.com/20100208-b7cxbt8andg3t7ntn17r1wahfg.jpg" alt="" /></p>
<p>Now we’ll go ahead and install a fresh copy of Ruby and RubyGems via MacPorts.</p>
<p><code>sudo port install ruby rb-rubygems</code></p>
<p>You should now see something like this for a bit…</p>
<p><img src="http://img.skitch.com/20100208-n9xxiaqbe6bw72qr88dgurxqdp.jpg" alt="" /></p>
<p>Let’s watch a video about bumble bees.</p>
<object width="560" height="340"><param name="movie" value="http://www.youtube.com/v/R-o6e57AEGo&hl=en_US&fs=1&color1=0x5d1719&color2=0xcd311b"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/R-o6e57AEGo&hl=en_US&fs=1&color1=0x5d1719&color2=0xcd311b" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="560" height="340"></embed></object>
<p>When it finishes installing, you should check that Ruby is available to you and installed in <code>/opt/local/bin</code>.</p>
<p><img src="http://img.skitch.com/20100208-tfc3ifeaau15295r9sfmfhbnm1.jpg" alt="" /></p>
<p>We’ll also take a second to create a symlink for this as some tools seem to rely on <code>/usr/bin/ruby</code> being there.</p>
<p><code>sudo ln -s /opt/local/bin/ruby /usr/bin/ruby</code></p>
<p>Great, let’s move on.</p>
<h3>Passenger (mod_rails)</h3>
<p>Now that we have Ruby installed, we’re going to take a quick detour to setup Passenger with the Apache server already available on your machine. I’ve been a big fan of using Passenger for your development for over a year now.</p>
<p><code>sudo gem install passenger</code></p>
<p>Once the gem is finished installing, you’ll need to install the apache2 module with the following command:</p>
<p>It’ll ask you to continue by pressing Enter. At this point, it’ll check that you have all the necessary dependencies and then compile everything needed for Apache2.</p>
<p><img src="http://img.skitch.com/20100208-x4rcmr6m5q44etk7rad2kqpmir.jpg" alt="" /></p>
<p>Now I’ll force you to watch a highlights reel of Fernando Torres… <em>the best striker in the world!</em></p>
<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/jRtq6TtSxbE&hl=en_US&fs=1&color1=0x5d1719&color2=0xcd311b"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/jRtq6TtSxbE&hl=en_US&fs=1&color1=0x5d1719&color2=0xcd311b" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>
<p>The passenger install will then show you this output, which you’ll want to stop and read for a moment and highlight the following:</p>
<p><img src="http://img.skitch.com/20100208-ms13ub2srx2axk6ti43ifiyut4.jpg" alt="" /></p>
<p>Then using vi or emacs, you’ll want to create a new file with the following content:</p>
<p><code>vi /etc/apache2/other/passenger.conf</code></p>
<p>Then paste in the following (what you highlighted and copied above.)</p>
<pre><code>
LoadModule passenger_module /opt/local/lib/ruby/gems/1.8/gems/passenger-2.2.9/ext/apache2/mod_passenger.so
PassengerRoot /opt/local/lib/ruby/gems/1.8/gems/passenger-2.2.9
PassengerRuby /opt/local/bin/ruby
</code></pre>
<p>You’ll also want to include the following below what you just pasted.</p>
<pre><code>
# Set the default environment to development
RailsEnv development
# Which directory do you want Apache to be able to look into for projects?
<Directory "/Users/ryangensel/development">
Order allow,deny
Allow from all
</Directory>
</code></pre>
<p>You’ll want to quickly start up your web sharing, which will start Apache2 up via your System Preferences.</p>
<p><img src="http://img.skitch.com/20100208-ekr2g6f5seb6iif35fteyjfi74.jpg" alt="" /></p>
<p><img src="http://img.skitch.com/20100208-rm96gmde4d8ffwjbdyqusiqr8f.jpg" alt="" /></p>
<p>Simple enough… moving forward.</p>
<h3>Passenger Pref Pane</h3>
<p>To make things as simple as possible, I’d encourage you to install the <a href="http://www.fngtps.com/2009/09/new-os-more-pane-passenger-preference-pane-v1-3">Passenger Preference Pane</a> (view this post for a download).</p>
<p><img src="http://img.skitch.com/20100208-ra8p88abxtnxbq3m9711gbpryb.jpg" alt="" /></p>
<h3>Development directory</h3>
<p>I have a directory named <code>development/</code> in my home directory, which is where I end up storing all of my projects. This should match whatever you put above in the apache configuration (<code><Directory "/Users/ryangensel/development"></code>).</p>
<p><code>mkdir development; cd development;</code></p>
<h3>Installing Ruby on Rails via RubyGems</h3>
<p>Now we’ll use RubyGems to install the latest version of Ruby on Rails (and all of it’s dependencies).</p>
<p><code>sudo gem install rails</code></p>
<p><img src="http://img.skitch.com/20100208-eyhg4t9sscbeiccs5rxkb5p35.jpg" alt="" /></p>
<p>While this is installing, you can watch a video from my old band that ended around the time that business started picking up for <a href="http://planetargon.com">Planet Argon</a>.</p>
<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/SJvncb3bVdg&hl=en_US&fs=1&color1=0x5d1719&color2=0xcd311b"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/SJvncb3bVdg&hl=en_US&fs=1&color1=0x5d1719&color2=0xcd311b" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>
<p>Great, let’s test out the install of Rails…</p>
<h3>Test Rails and Passenger</h3>
<p>In your development directory, let’s quickly a new Rails app…</p>
<p><code>rails testapp</code></p>
<p>This will generate a new Rails application in a <code>testapp/</code> directory.</p>
<p>Now open up the <strong>Passenger Preferences Pane</strong> and add this directory as a new application.</p>
<p><img src="http://img.skitch.com/20100208-83rjan794eqcmy62e1u4aujtiy.jpg" alt="" /></p>
<p><img src="http://img.skitch.com/20100208-tms89ssutthhq8rkpjm392qfpb.jpg" alt="" /></p>
<p>Press Apply…</p>
<p>You should now fire up your browser of choice and head to <code>http://testapp.local</code>. If all has worked, you’ll see a, “Welcome aboard” screen from the Ruby on Rails application.</p>
<p><img src="http://img.skitch.com/20100208-c9phjdfq8ybcu29uc5k9qiuha9.jpg" alt="" /></p>
<p>Assuming that this worked for you, let’s take a quick break to make some tea…</p>
<h2>Phase Three</h2>
<p>In this last phase, we’re going to install a few database servers and corresponding rubygems so that you can get to work.</p>
<h3>PostgreSQL</h3>
<p>At <a href="http://planetargon.com">Planet Argon</a>, we build our web applications on top of <a href="http://postgresql.org">PostgreSQL</a>. I’ve been a long-time advocate of it and hope you consider using it yourself.</p>
<p>At this point in time, the current stable version of PostgreSQL via MacPorts is 8.4.x. Let’s install that now…</p>
<p><code>sudo port install postgresql84 postgresql84-server</code></p>
<p>Once this finishes compiling, you’ll need to run the following commands to setup a new PostgreSQL database.</p>
<pre><code>
sudo mkdir -p /opt/local/var/db/postgresql84/defaultdb
sudo chown postgres:postgres /opt/local/var/db/postgresql84/defaultdb
sudo su postgres -c '/opt/local/lib/postgresql84/bin/initdb -D /opt/local/var/db/postgresql84/defaultdb'
</code></pre>
<p>Assuming that you want PostgreSQL to always be running, you can run:</p>
<p><code>sudo launchctl load -w /Library/LaunchDaemons/org.macports.postgresql84-server.plist</code></p>
<p>...and to start it right now, run:</p>
<p><code>sudo launchctl start org.macports.postgresql84-server</code></p>
<p>Before you can start using it, we’ll need to make sure that the PostgreSQL executables are available in your shell path. Since you’re now using <a href="http://github.com/robbyrussell/oh-my-zsh">oh-my-zsh</a>, you’ll want to edit <code>~/.zshrc</code> with your favorite editor.</p>
<p><code>vi ~/.zshrc</code></p>
<p>Just append this to <code>export PATH=</code> line in the file.</p>
<p><code>:/opt/local/lib/postgresql84/bin</code></p>
<p>Your <span class="caps">PATH</span> might look something like the following now:</p>
<p>@# Customize to your needs…
export <span class="caps">PATH</span>=/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:/opt/local/lib/postgresql84/bin@</p>
<h4>Setup database user</h4>
<p>To setup a new database (with superuser credentials), just run:</p>
<p><code>createuser --superuser ryangensel -U postgres</code></p>
<p>We’ll now test creating a database:</p>
<p><code>createdb test_db</code></p>
<p>Let’s test that we can access it…</p>
<pre><code>
➜ ~ psql test_db
psql (8.4.2)
Type "help" for help.
test_db=# \q
</code></pre>
<p>Great, let’s drop it now.</p>
<pre><code>
➜ ~ dropdb test_db
➜ ~ psql test_db
psql: FATAL: database "test_db" does not exist
➜ ~
</code></pre>
<p>Okay, we’ll now install the library that will allow Ruby to talk to PostgreSQL.</p>
<p>Just run: <code>sudo gem install pg</code></p>
<p><img src="http://img.skitch.com/20100208-b4cf1pm1utkf2ihd9up432etwm.jpg" alt="" /></p>
<p>Voila… let’s move on to the inferior database…</p>
<h3>MySQL</h3>
<p>We’re going to run through the installation of MySQL really quickly because you might need it.</p>
<p><code>sudo port install mysql5 mysql5-server</code></p>
<p>This took ages on my machine… so let’s watch a video.</p>
<object width="560" height="340"><param name="movie" value="http://www.youtube.com/v/LOZIjWJpiBk&hl=en_US&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/LOZIjWJpiBk&hl=en_US&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="560" height="340"></embed></object>
<p>We’ll now setup the database and make sure it starts on system boot.</p>
<pre><code>
sudo -u _mysql mysql_install_db5
sudo launchctl load -w /Library/LaunchDaemons/org.macports.mysql5.plist
sudo launchctl start org.macports.mysql5
</code></pre>
<p>Let’s test that we can create a database now (and that it’s running.)</p>
<pre><code>
➜ ~ mysql5 -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.1.43 Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create database test1;
Query OK, 1 row affected (0.00 sec)
mysql> \q
</code></pre>
<p>Great, we’ll now install the library that will allow Ruby to talk to MySQL.</p>
<p><code>sudo gem install mysql -- --with-mysql-config=/opt/local/lib/mysql5/bin/mysql_config</code></p>
<p>That should be it!</p>
<h3>Phase Four, next steps</h3>
<p>Okay… so we’ve installed XCode, MacPorts, Ruby, Rails, PostgreSQL, MySQL… and I’ve also got you to switch your default terminal shell from bash to zsh. You might take a look over the <a href="http://wiki.github.com/robbyrussell/oh-my-zsh/themes">available themes for Oh My Zsh</a> so that you can personalize your terminal experience even further.</p>
<p>You also now have a handful of gems installed as you can see with <code>gem list</code>.</p>
<p><img src="http://img.skitch.com/20100208-p2kkjnng2b2kebeenkwkihn2p5.jpg" alt="" /></p>
<h3>Closing thoughts…</h3>
<p>This is the fourth version of this guide and I’ve appreciated the hundreds of comments, questions, and emails that I have received… let’s not forget all those beers that people buy me when I’m at conferences. :-)</p>
<p>I hope you have found some of this useful. If you have any problems and/or questions, don’t hesitate to post them in the comments section below.</p><p>Welcome to what seems like my tenth installment (actually, it’s the fourth) of showing you how I setup my development environment on a fresh <span class="caps">OSX</span> install. In this case, I’m actually getting a MacBook setup for a new employee with Snow Leopard.</p>
<p>Over the years, I’ve evolved these following steps and they’ve helped our team maintain a consistent and stable envirnment for Ruby on Rails development. I know that there are a few other ways to approaching this and I’m sure you’ll get similar results, but this approach has allowed me to maintain a hassle-free setup for the last five years.</p>
<p>As with all things… your milage may vary.</p>
<h2>Phase One</h2>
<p>During this initial phase, we’re going to install the primary dependencies and setup our environment.</p>
<h3>XCode</h3>
<p>The first thing that you’ll need to do is install XCode, which almost everything depends upon as this will install developer-friendly tools for you. Apple has been kind enough to ship this on your Snow Leopard <span class="caps">DVD</span>.</p>
<p><img src="http://img.skitch.com/20100208-kmamssde4apw1qxcs6u991p518.jpg" alt="" /></p>
<p>Go ahead and install XCode from the <strong>Optional Installs</strong> folder.</p>
<p><img src="http://img.skitch.com/20100208-1up5s7ahybmryrerrxxwn1d7tu.jpg" alt="" /></p>
<p>(might require a reboot)</p>
<p>You can also <a href="http://developer.apple.com/technology/xcode.html">download it</a> online.</p>
<h3>MacPorts</h3>
<p>Now we’ll install MacPorts, which the web site describes itself as, “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.”</p>
<p>As I’ve said in past versions of this guide, this tool is about to become one of the most important tools on your operating system. 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: <code>apt-get</code>, <code>port</code>, and <code>yum</code>.</p>
<p>You’ll want to download the latest stable version from <a href="http://www.macports.org">http://www.macports.org/</a>. Once downloaded, you can install it.</p>
<p><img src="http://img.skitch.com/20100208-fd4h7rd16437yd63hf45mkn592.jpg" alt="" /></p>
<p>Once this is installed, you’ll be able to use the <code>port</code> command from your console.</p>
<h3>Wget</h3>
<p>Let’s test out your MacPorts install by installing a useful tool called wget, which we’ll use to install oh-my-zsh.</p>
<p><code>sudo port install wget</code></p>
<h3>Git and Subversion</h3>
<p>Every development environment should have some source code management tools available. We’ll install both of these with one command.</p>
<p><code>sudo port install git-core +svn</code></p>
<p>This will install git and subversion.</p>
<h3>oh-my-zsh</h3>
<p><a href="http://github.com/robbyrussell/oh-my-zsh">Oh My Zsh</a> is the most amazing thing to happen to shells since… well since I said so. It’s one of my open source projects that I encourage you to give a whirl.</p>
<p><code>wget http://github.com/robbyrussell/oh-my-zsh/raw/master/tools/install.sh -O - | sh</code></p>
<p>That’s it. The next time you open up your terminal, you’ll be running zsh with a bunch of stuff available. For more information, visit <a href="http://github.com/robbyrussell/oh-my-zsh">http://github.com/robbyrussell/oh-my-zsh</a>.</p>
<h3>Terminal theme (optional)</h3>
<p>I never understood why the icon for Terminal has a black background but when you start it up the default theme is black on white.</p>
<p><img src="http://img.skitch.com/20100208-1b92cbx8gtuttqsbdd49qspq7f.jpg" alt="" /></p>
<p>versus</p>
<p><img src="http://img.skitch.com/20100208-8dnmpswyumfk73h942g6u4fih4.jpg" alt="" /></p>
<p>Anyhow, I’m a fan of the the dark background. To change this, open up preferences in Terminal. Select <strong>Pro</strong>, then click on the <strong>Default</strong> window so that this sticks around.</p>
<p><img src="http://img.skitch.com/20100208-mr4q4y5btq7sscuh7ceky3ahgx.jpg" alt="" /></p>
<p>Let’s now open up a new Terminal window..</p>
<p>You should be looking at something like this:</p>
<p><img src="http://img.skitch.com/20100208-psmj7mqsx38j1i2nj75n6hbcg7.jpg" alt="" /></p>
<p>Much better… let’s continue.</p>
<h2>Phase Two</h2>
<p>We’re now going to start installing everything we need to get this running.</p>
<h3>Ruby 1.8.7.x</h3>
<p>First up, Ruby.</p>
<p>Snow Leopard includes Ruby and Rails already installed, but we’re going to back these up for a rainy day. Just issue these commands:</p>
<pre><code>
$ 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>
<p><img src="http://img.skitch.com/20100208-b7cxbt8andg3t7ntn17r1wahfg.jpg" alt="" /></p>
<p>Now we’ll go ahead and install a fresh copy of Ruby and RubyGems via MacPorts.</p>
<p><code>sudo port install ruby rb-rubygems</code></p>
<p>You should now see something like this for a bit…</p>
<p><img src="http://img.skitch.com/20100208-n9xxiaqbe6bw72qr88dgurxqdp.jpg" alt="" /></p>
<p>Let’s watch a video about bumble bees.</p>
<object width="560" height="340"><param name="movie" value="http://www.youtube.com/v/R-o6e57AEGo&hl=en_US&fs=1&color1=0x5d1719&color2=0xcd311b"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/R-o6e57AEGo&hl=en_US&fs=1&color1=0x5d1719&color2=0xcd311b" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="560" height="340"></embed></object>
<p>When it finishes installing, you should check that Ruby is available to you and installed in <code>/opt/local/bin</code>.</p>
<p><img src="http://img.skitch.com/20100208-tfc3ifeaau15295r9sfmfhbnm1.jpg" alt="" /></p>
<p>We’ll also take a second to create a symlink for this as some tools seem to rely on <code>/usr/bin/ruby</code> being there.</p>
<p><code>sudo ln -s /opt/local/bin/ruby /usr/bin/ruby</code></p>
<p>Great, let’s move on.</p>
<h3>Passenger (mod_rails)</h3>
<p>Now that we have Ruby installed, we’re going to take a quick detour to setup Passenger with the Apache server already available on your machine. I’ve been a big fan of using Passenger for your development for over a year now.</p>
<p><code>sudo gem install passenger</code></p>
<p>Once the gem is finished installing, you’ll need to install the apache2 module with the following command:</p>
<p>It’ll ask you to continue by pressing Enter. At this point, it’ll check that you have all the necessary dependencies and then compile everything needed for Apache2.</p>
<p><img src="http://img.skitch.com/20100208-x4rcmr6m5q44etk7rad2kqpmir.jpg" alt="" /></p>
<p>Now I’ll force you to watch a highlights reel of Fernando Torres… <em>the best striker in the world!</em></p>
<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/jRtq6TtSxbE&hl=en_US&fs=1&color1=0x5d1719&color2=0xcd311b"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/jRtq6TtSxbE&hl=en_US&fs=1&color1=0x5d1719&color2=0xcd311b" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>
<p>The passenger install will then show you this output, which you’ll want to stop and read for a moment and highlight the following:</p>
<p><img src="http://img.skitch.com/20100208-ms13ub2srx2axk6ti43ifiyut4.jpg" alt="" /></p>
<p>Then using vi or emacs, you’ll want to create a new file with the following content:</p>
<p><code>vi /etc/apache2/other/passenger.conf</code></p>
<p>Then paste in the following (what you highlighted and copied above.)</p>
<pre><code>
LoadModule passenger_module /opt/local/lib/ruby/gems/1.8/gems/passenger-2.2.9/ext/apache2/mod_passenger.so
PassengerRoot /opt/local/lib/ruby/gems/1.8/gems/passenger-2.2.9
PassengerRuby /opt/local/bin/ruby
</code></pre>
<p>You’ll also want to include the following below what you just pasted.</p>
<pre><code>
# Set the default environment to development
RailsEnv development
# Which directory do you want Apache to be able to look into for projects?
<Directory "/Users/ryangensel/development">
Order allow,deny
Allow from all
</Directory>
</code></pre>
<p>You’ll want to quickly start up your web sharing, which will start Apache2 up via your System Preferences.</p>
<p><img src="http://img.skitch.com/20100208-ekr2g6f5seb6iif35fteyjfi74.jpg" alt="" /></p>
<p><img src="http://img.skitch.com/20100208-rm96gmde4d8ffwjbdyqusiqr8f.jpg" alt="" /></p>
<p>Simple enough… moving forward.</p>
<h3>Passenger Pref Pane</h3>
<p>To make things as simple as possible, I’d encourage you to install the <a href="http://www.fngtps.com/2009/09/new-os-more-pane-passenger-preference-pane-v1-3">Passenger Preference Pane</a> (view this post for a download).</p>
<p><img src="http://img.skitch.com/20100208-ra8p88abxtnxbq3m9711gbpryb.jpg" alt="" /></p>
<h3>Development directory</h3>
<p>I have a directory named <code>development/</code> in my home directory, which is where I end up storing all of my projects. This should match whatever you put above in the apache configuration (<code><Directory "/Users/ryangensel/development"></code>).</p>
<p><code>mkdir development; cd development;</code></p>
<h3>Installing Ruby on Rails via RubyGems</h3>
<p>Now we’ll use RubyGems to install the latest version of Ruby on Rails (and all of it’s dependencies).</p>
<p><code>sudo gem install rails</code></p>
<p><img src="http://img.skitch.com/20100208-eyhg4t9sscbeiccs5rxkb5p35.jpg" alt="" /></p>
<p>While this is installing, you can watch a video from my old band that ended around the time that business started picking up for <a href="http://planetargon.com">Planet Argon</a>.</p>
<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/SJvncb3bVdg&hl=en_US&fs=1&color1=0x5d1719&color2=0xcd311b"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/SJvncb3bVdg&hl=en_US&fs=1&color1=0x5d1719&color2=0xcd311b" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>
<p>Great, let’s test out the install of Rails…</p>
<h3>Test Rails and Passenger</h3>
<p>In your development directory, let’s quickly a new Rails app…</p>
<p><code>rails testapp</code></p>
<p>This will generate a new Rails application in a <code>testapp/</code> directory.</p>
<p>Now open up the <strong>Passenger Preferences Pane</strong> and add this directory as a new application.</p>
<p><img src="http://img.skitch.com/20100208-83rjan794eqcmy62e1u4aujtiy.jpg" alt="" /></p>
<p><img src="http://img.skitch.com/20100208-tms89ssutthhq8rkpjm392qfpb.jpg" alt="" /></p>
<p>Press Apply…</p>
<p>You should now fire up your browser of choice and head to <code>http://testapp.local</code>. If all has worked, you’ll see a, “Welcome aboard” screen from the Ruby on Rails application.</p>
<p><img src="http://img.skitch.com/20100208-c9phjdfq8ybcu29uc5k9qiuha9.jpg" alt="" /></p>
<p>Assuming that this worked for you, let’s take a quick break to make some tea…</p>
<h2>Phase Three</h2>
<p>In this last phase, we’re going to install a few database servers and corresponding rubygems so that you can get to work.</p>
<h3>PostgreSQL</h3>
<p>At <a href="http://planetargon.com">Planet Argon</a>, we build our web applications on top of <a href="http://postgresql.org">PostgreSQL</a>. I’ve been a long-time advocate of it and hope you consider using it yourself.</p>
<p>At this point in time, the current stable version of PostgreSQL via MacPorts is 8.4.x. Let’s install that now…</p>
<p><code>sudo port install postgresql84 postgresql84-server</code></p>
<p>Once this finishes compiling, you’ll need to run the following commands to setup a new PostgreSQL database.</p>
<pre><code>
sudo mkdir -p /opt/local/var/db/postgresql84/defaultdb
sudo chown postgres:postgres /opt/local/var/db/postgresql84/defaultdb
sudo su postgres -c '/opt/local/lib/postgresql84/bin/initdb -D /opt/local/var/db/postgresql84/defaultdb'
</code></pre>
<p>Assuming that you want PostgreSQL to always be running, you can run:</p>
<p><code>sudo launchctl load -w /Library/LaunchDaemons/org.macports.postgresql84-server.plist</code></p>
<p>...and to start it right now, run:</p>
<p><code>sudo launchctl start org.macports.postgresql84-server</code></p>
<p>Before you can start using it, we’ll need to make sure that the PostgreSQL executables are available in your shell path. Since you’re now using <a href="http://github.com/robbyrussell/oh-my-zsh">oh-my-zsh</a>, you’ll want to edit <code>~/.zshrc</code> with your favorite editor.</p>
<p><code>vi ~/.zshrc</code></p>
<p>Just append this to <code>export PATH=</code> line in the file.</p>
<p><code>:/opt/local/lib/postgresql84/bin</code></p>
<p>Your <span class="caps">PATH</span> might look something like the following now:</p>
<p>@# Customize to your needs…
export <span class="caps">PATH</span>=/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:/opt/local/lib/postgresql84/bin@</p>
<h4>Setup database user</h4>
<p>To setup a new database (with superuser credentials), just run:</p>
<p><code>createuser --superuser ryangensel -U postgres</code></p>
<p>We’ll now test creating a database:</p>
<p><code>createdb test_db</code></p>
<p>Let’s test that we can access it…</p>
<pre><code>
➜ ~ psql test_db
psql (8.4.2)
Type "help" for help.
test_db=# \q
</code></pre>
<p>Great, let’s drop it now.</p>
<pre><code>
➜ ~ dropdb test_db
➜ ~ psql test_db
psql: FATAL: database "test_db" does not exist
➜ ~
</code></pre>
<p>Okay, we’ll now install the library that will allow Ruby to talk to PostgreSQL.</p>
<p>Just run: <code>sudo gem install pg</code></p>
<p><img src="http://img.skitch.com/20100208-b4cf1pm1utkf2ihd9up432etwm.jpg" alt="" /></p>
<p>Voila… let’s move on to the inferior database…</p>
<h3>MySQL</h3>
<p>We’re going to run through the installation of MySQL really quickly because you might need it.</p>
<p><code>sudo port install mysql5 mysql5-server</code></p>
<p>This took ages on my machine… so let’s watch a video.</p>
<object width="560" height="340"><param name="movie" value="http://www.youtube.com/v/LOZIjWJpiBk&hl=en_US&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/LOZIjWJpiBk&hl=en_US&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="560" height="340"></embed></object>
<p>We’ll now setup the database and make sure it starts on system boot.</p>
<pre><code>
sudo -u _mysql mysql_install_db5
sudo launchctl load -w /Library/LaunchDaemons/org.macports.mysql5.plist
sudo launchctl start org.macports.mysql5
</code></pre>
<p>Let’s test that we can create a database now (and that it’s running.)</p>
<pre><code>
➜ ~ mysql5 -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.1.43 Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create database test1;
Query OK, 1 row affected (0.00 sec)
mysql> \q
</code></pre>
<p>Great, we’ll now install the library that will allow Ruby to talk to MySQL.</p>
<p><code>sudo gem install mysql -- --with-mysql-config=/opt/local/lib/mysql5/bin/mysql_config</code></p>
<p>That should be it!</p>
<h3>Phase Four, next steps</h3>
<p>Okay… so we’ve installed XCode, MacPorts, Ruby, Rails, PostgreSQL, MySQL… and I’ve also got you to switch your default terminal shell from bash to zsh. You might take a look over the <a href="http://wiki.github.com/robbyrussell/oh-my-zsh/themes">available themes for Oh My Zsh</a> so that you can personalize your terminal experience even further.</p>
<p>You also now have a handful of gems installed as you can see with <code>gem list</code>.</p>
<p><img src="http://img.skitch.com/20100208-p2kkjnng2b2kebeenkwkihn2p5.jpg" alt="" /></p>
<h3>Closing thoughts…</h3>
<p>This is the fourth version of this guide and I’ve appreciated the hundreds of comments, questions, and emails that I have received… let’s not forget all those beers that people buy me when I’m at conferences. :-)</p>
<p>I hope you have found some of this useful. If you have any problems and/or questions, don’t hesitate to post them in the comments section below.</p>