Ruby on Rails
HowtoInstallOnOSXTigerUsingDarwinPorts (Version #36)

Note: Works with both Mac OS X 10.4 Client and Server (perhaps even Mac OS X 10.3 as well).

Installing MacPorts (formerly DarwinPorts)?

The DarwinPorts project has been renamed to MacPorts. There are several different ways that MacPorts can be installed. Perhaps the easiest is installing via package. MacPorts 1.5.0 is available as a Universal Binary at MacPorts.

After installation, fire up the Terminal. By default, MacPorts installs into /opt/local/bin so we need to add that to our path. Even more so, we want the software installed by MacPorts to be used before those installed by the system (thusly avoiding the broken ruby problem). To do this, in the Terminal enter (if you are using the default bash shell):

 $ sudo vi /etc/profile

Prepend /opt/local/bin: to PATH, so it reads:

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

Press the escape key to move from writing mode to command mode. Enter :wq
(The colon charcter followed by ‘w’ and ’q’)

Close the Terminal window, and open a new one.

Now that we have changed the default path to include /opt/local/bin, we need to update the MacPorts port database. Back in the shell, enter:

 $ sudo port selfupdate

Installing RubyGems

The following command will install RubyGems and all of its dependencies (including a non-broken ruby).

 $ sudo port install rb-rubygems

Installing Rails

 $ sudo gem install rails

Whenever your’re installing a Rails app, you should take care to change all she-bang lines in the /script dir and the /public/dispatch.* to /opt/local/bin/ruby

Or, use the --ruby argument to the rails command. (Find out all the options by invoking rails without an argument.)

Optional installs

MySQL

 $ sudo port install mysql4 +server

Don’t forget to: ( postinstallation )


cd /opt/local/
sudo bin/mysql_install_db -user=mysql


$ sudo gem install mysql - —with-mysql-config=/opt/local/bin/mysql_config


I had a heck of a time figuring out the little differences between MySQL4 install and MySQL5. I installed from scratch on a new Tiger install using all the DarwinPorts defaults and this is what I’ve noticed:

  • Anything with a “4” at the end obviously change to “5”. Most of the binaries and libraries also have a “5” at the end, even those that didn’t have a number before like the mysql client is “mysql5” and the mysql_config file is “mysql_config5”
  • The missing piece to my puzzle was sudo gcc_select 4.0 before installing the gem. I’m pretty sure it’s specific to MySQL5

— Ryan Briones

Lighttpd

 $ sudo port install lighttpd

or with ssl and mysql support

 $ sudo port install lighttpd +ssl +mysql

See this for more on the topic of DarwinPorts and lighttpd.

mod_fastcgi for Apache

 $ sudo gem install fcgi
 $ cd /tmp
 $ curl -O <a href="http://andreas-s.net/download/mod_fastcgi-panther-2.4.2.tar.gz">http://andreas-s.net/download/mod_fastcgi-panther-2.4.2.tar.gz</a>
 $ tar xvfz mod_fastcgi-panther-2.4.2.tar.gz
 $ sudo cp mod_fastcgi-panther-2.4.2/mod_fastcgi.so /usr/libexec/httpd/

You need to add the following to your /etc/httpd/httpd.conf file to get a working fastcgi-enabled virtual host:

LoadModule fastcgi_module libexec/httpd/mod_fastcgi.so
AddModule mod_fastcgi.c

<IfModule mod_fastcgi.c>
  FastCgiIpcDir /tmp/fcgi_ipc/
  FastCgiConfig -idle-timeout 1800
  AddHandler fastcgi-script .fcgi
</IfModule>

<VirtualHost *:80>
  Servername localhost
  DocumentRoot (path-to-your-rails-app)/public
  <Directory (path-to-your-rails-app)/public>
    AllowOverride All
  </Directory>
</VirtualHost>

Also, the last RewriteRule in your (path-to-your-rails-app)/public/.htaccess file to read:

  RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]

Also, set the correct directory permissions (this setup would be for development using Apache/mod_fastcgi):

  $ cd (path-to-your-rails-app)
  $ chmod 777 log

Subversion

 $ sudo port install subversion

PostgreSQL

Use the following to install a fully functioning PostgreSQL 8 Server setup. A user called ‘postgresql8’ will be automatically created.


$ sudo port install postgresql8 +server
$ sudo port -b install rb-postgres

IMPORTANT INFORMATION As of 2005/07/27 the most current version of the openssl port (0.9.8_0) breaks the rb-postgres port. You need to install 0.9.7g_0 to make it work. Alas, there is no way AFAIK to force darwinports to use a specific port version on the ‘install’ target. I’ll update this if i find more info… :( You can grab the old version of the portfile on CVS here: http://cvs.opendarwin.org//cgi-bin/cvsweb.cgi/proj/darwinports/dports/devel/openssl/Portfile?rev=1.15&content-type=text/x-cvsweb-markup — you’ll also need the patches, but this is where my darwinports knowledge sadly ends. (8 jan 2006 – is this still still accurate and pertinent?)

If you want to install the ruby-postgres adaptor as a gem, instead of out of DarwinPorts, do this (all on one line):

$ sudo gem install postgres -- 
  --with-pgsql-include-dir=/opt/local/include/pgsql8 
  --with-pgsql-lib-dir=/opt/local/lib/pgsql8

Creating a database:

 $ sudo mkdir <db-dir>
 $ sudo chown postgres8 <db-dir>

The following must be entered on one line:


$ sudo su postgres8 -c ‘/opt/local/bin/initdb -D
—locale=en_US.UTF-8 —encoding=UNICODE

RMagick

sudo port install ghostscript
sudo port install ImageMagick
gem install rmagick

This seemed to work, but I haven’t actually used it with JPEGs. Just found this blog posting with (probably better) instructions using Darwin Ports: Making RMagick suck less with Tiger

You might also try editing the Magick-conf file as described here HowtoInstallRMagickOnOSXTiger

Another Method For Installing via MacPorts

This is a guide to walk you through setting up Ruby on Rails in OS X Tiger 10.4.8. It was written on 9/27/2006.

The steps below utilize the DarwinPorts application for ease of use, but also because everything ends up installed in the /opt/ directory which means it is extremely easy to take this off if anything fails. You will not end up with nearly as cluttered a system this way as if you try to install from source. This article aims at non-techie people who want to get a development environment up and running to try Ruby on Rails!

Please note, that if you wait until OS X 10.5 Leopard is released. You won’t have to do any of this, but will instead be greeted with Ruby on Rails pre-installed on your system so you’ll just have to add your favorite flavor of database to the mix!

DISCLAIMER: No support is provided or guarantees made that these instructions will work on every system. These instructions were written for an Intel Macbook and were installed from a fresh install of OS X Tiger with all system updates applied. Do not email the author if anything doesn’t work as intended as no technical support will be provided.

Pre-requisite:

DO NOT SKIP THIS STEP!
Install X-Code Tools from your OS X installation CDs. You need to be at version 2.0 at least to continue!!!

After you’ve installed X-Code Tools you may continue to step 1 and begin the installation process.

Getting Started:

1. Download and install MacPorts from http://svn.macports.org/repository/macports/downloads/MacPorts-1.5.0/
a. There is a specific .dmg for Tiger and one for Panther
2. Open up a terminal window (Applications→Utilities→Terminal)

3. Now configure it to work from the terminal
a. add the following line to ~/.profile
export PATH=$PATH:/opt/local/bin
NOTE: If you are unsure how to do this you can type sudo pico ~/.profile. Type in the line and then CTRL+X to leave pico. You will be asked to save it first, so press y to save.

4. Type sudo source ~/.profile to reload your ~/.profile file to allow the changes to take effect.

5. Download the Readline-5.1-patches from: ftp://ftp.cwru.edu/pub/bash/readline-5.1-patches (get all 4)

6. Use Finder to put them in your “/opt/local/var/db/dports/distfiles/readline” directory (You will need to create this readline directory)

(Steps 6 → 10 should all be typed into the open terminal window. These steps can take up to 5 minutes a piece depending on your internet connection and the speed of your computer. The install ruby command takes particularly long!)

7. Type

sudo port install readline

8. Type

sudo port install ruby

9. Type

sudo port install rb-rubygems

You now have everything for ruby with rubygems on your system to begin installing the development environment.
You have a “working” version of Ruby, Readline (think of it as the flux-capacitor for OS X & Ruby on Rails), and Ruby Gems.

10. Type

sudo gem update

11. Type

sudo gem install rails —include-dependencies

12. Type

sudo gem update rake
(for some reason you get rake 0.7.0 when you install rails 1.1.6 and this will update it to the latest version which is 0.7.1 at the time of this writing)

Great! You now have Ruby on Rails installed on your system as well. Now it’s time to isntall a database. In this example we’ll grab MySQL, though there are plenty of tutorials around the ’net that can give you more complete information on using Postgres or another flavor of database with your system.

12. Visit http://dev.mysql.com/downloads/mysql/4.1.html
NOTE: (5.0 has been released as of this writing, but 4.1 is a known quantity and is what most web hosting providers are currently supporting which is why I’m advocating its use in this tutorial!)

13. Continue scrolling down the page until you find the Mac OS X downloads. You can find them about 3/4ths of the way down the screen. Just don’t give up hope. Download the appropriate version for your hardware as there are both PowerPC, 64-bit, and x86 versions to choose from)

14. Follow the appropriate installation procedures for your version that comes included with the MySQL packages. Typically they are pretty straight forward like running 2 pkg files and then a control panel file that adds functinoality to the OS X control panel to start & stop mySQL.
NOTE: DO NOT FORGET TO UPDATE YOUR PATH as instructed in the installation notes on the mySQL website! Make sure you create the alias’ for both mysql and mysql admin!!!

15. Set the password for your root account before moving forward:
(NOTE: STOP mySQL BEFORE DOING THIS!!!)
a. Type mysql -u root mysql (NOTE: If your system doesn’t give you a mysql> prompt you haven’t installed the alias’ yet. Go back and install them or run this from the bin directory where mysql was installed on your system!
b. Type SET PASSWORD FOR root@localhost=PASSWORD;
c. You should get a confirmation that looks like this: “Query OK, 0 rows affected (0.06 sec)”
d. Type quit

At this point I like to download a GUI for mySQL. If you want to purchase one I’d recommend Navicat. If you want a free solution, I’d get CocoaMySQL GUI (http://cocoamysql.sourceforge.net/).

So now you have a complete development environment set up on your machine assuming you aren’t hosting you won’t need FastCGI, or Lighttpd, or apache. The CGI version of Rails runs fast enough for a development environment that loading FastCGI on at this point would be overkill. There is a DarwinPort for FastCGI (fcgi and rb-fcgi) that you can install if you find you need it later. You should be able to run everything from Rails’ built in webrick server.

You may at this point want an IDE. I have used RADRails (http://www.radrails.org) which is built off the eclipse framework and have been very pleased with it. Though TextMate is what you’ll see in the all the Ruby on Rails screencasts and it too is a great tool I’m sure.

Note: Works with both Mac OS X 10.4 Client and Server (perhaps even Mac OS X 10.3 as well).

Installing MacPorts (formerly DarwinPorts)?

The DarwinPorts project has been renamed to MacPorts. There are several different ways that MacPorts can be installed. Perhaps the easiest is installing via package. MacPorts 1.5.0 is available as a Universal Binary at MacPorts.

After installation, fire up the Terminal. By default, MacPorts installs into /opt/local/bin so we need to add that to our path. Even more so, we want the software installed by MacPorts to be used before those installed by the system (thusly avoiding the broken ruby problem). To do this, in the Terminal enter (if you are using the default bash shell):

 $ sudo vi /etc/profile

Prepend /opt/local/bin: to PATH, so it reads:

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

Press the escape key to move from writing mode to command mode. Enter :wq
(The colon charcter followed by ‘w’ and ’q’)

Close the Terminal window, and open a new one.

Now that we have changed the default path to include /opt/local/bin, we need to update the MacPorts port database. Back in the shell, enter:

 $ sudo port selfupdate

Installing RubyGems

The following command will install RubyGems and all of its dependencies (including a non-broken ruby).

 $ sudo port install rb-rubygems

Installing Rails

 $ sudo gem install rails

Whenever your’re installing a Rails app, you should take care to change all she-bang lines in the /script dir and the /public/dispatch.* to /opt/local/bin/ruby

Or, use the --ruby argument to the rails command. (Find out all the options by invoking rails without an argument.)

Optional installs

MySQL

 $ sudo port install mysql4 +server

Don’t forget to: ( postinstallation )


cd /opt/local/
sudo bin/mysql_install_db -user=mysql


$ sudo gem install mysql - —with-mysql-config=/opt/local/bin/mysql_config


I had a heck of a time figuring out the little differences between MySQL4 install and MySQL5. I installed from scratch on a new Tiger install using all the DarwinPorts defaults and this is what I’ve noticed:

  • Anything with a “4” at the end obviously change to “5”. Most of the binaries and libraries also have a “5” at the end, even those that didn’t have a number before like the mysql client is “mysql5” and the mysql_config file is “mysql_config5”
  • The missing piece to my puzzle was sudo gcc_select 4.0 before installing the gem. I’m pretty sure it’s specific to MySQL5

— Ryan Briones

Lighttpd

 $ sudo port install lighttpd

or with ssl and mysql support

 $ sudo port install lighttpd +ssl +mysql

See this for more on the topic of DarwinPorts and lighttpd.

mod_fastcgi for Apache

 $ sudo gem install fcgi
 $ cd /tmp
 $ curl -O <a href="http://andreas-s.net/download/mod_fastcgi-panther-2.4.2.tar.gz">http://andreas-s.net/download/mod_fastcgi-panther-2.4.2.tar.gz</a>
 $ tar xvfz mod_fastcgi-panther-2.4.2.tar.gz
 $ sudo cp mod_fastcgi-panther-2.4.2/mod_fastcgi.so /usr/libexec/httpd/

You need to add the following to your /etc/httpd/httpd.conf file to get a working fastcgi-enabled virtual host:

LoadModule fastcgi_module libexec/httpd/mod_fastcgi.so
AddModule mod_fastcgi.c

<IfModule mod_fastcgi.c>
  FastCgiIpcDir /tmp/fcgi_ipc/
  FastCgiConfig -idle-timeout 1800
  AddHandler fastcgi-script .fcgi
</IfModule>

<VirtualHost *:80>
  Servername localhost
  DocumentRoot (path-to-your-rails-app)/public
  <Directory (path-to-your-rails-app)/public>
    AllowOverride All
  </Directory>
</VirtualHost>

Also, the last RewriteRule in your (path-to-your-rails-app)/public/.htaccess file to read:

  RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]

Also, set the correct directory permissions (this setup would be for development using Apache/mod_fastcgi):

  $ cd (path-to-your-rails-app)
  $ chmod 777 log

Subversion

 $ sudo port install subversion

PostgreSQL

Use the following to install a fully functioning PostgreSQL 8 Server setup. A user called ‘postgresql8’ will be automatically created.


$ sudo port install postgresql8 +server
$ sudo port -b install rb-postgres

IMPORTANT INFORMATION As of 2005/07/27 the most current version of the openssl port (0.9.8_0) breaks the rb-postgres port. You need to install 0.9.7g_0 to make it work. Alas, there is no way AFAIK to force darwinports to use a specific port version on the ‘install’ target. I’ll update this if i find more info… :( You can grab the old version of the portfile on CVS here: http://cvs.opendarwin.org//cgi-bin/cvsweb.cgi/proj/darwinports/dports/devel/openssl/Portfile?rev=1.15&content-type=text/x-cvsweb-markup — you’ll also need the patches, but this is where my darwinports knowledge sadly ends. (8 jan 2006 – is this still still accurate and pertinent?)

If you want to install the ruby-postgres adaptor as a gem, instead of out of DarwinPorts, do this (all on one line):

$ sudo gem install postgres -- 
  --with-pgsql-include-dir=/opt/local/include/pgsql8 
  --with-pgsql-lib-dir=/opt/local/lib/pgsql8

Creating a database:

 $ sudo mkdir <db-dir>
 $ sudo chown postgres8 <db-dir>

The following must be entered on one line:


$ sudo su postgres8 -c ‘/opt/local/bin/initdb -D
—locale=en_US.UTF-8 —encoding=UNICODE

RMagick

sudo port install ghostscript
sudo port install ImageMagick
gem install rmagick

This seemed to work, but I haven’t actually used it with JPEGs. Just found this blog posting with (probably better) instructions using Darwin Ports: Making RMagick suck less with Tiger

You might also try editing the Magick-conf file as described here HowtoInstallRMagickOnOSXTiger

Another Method For Installing via MacPorts

This is a guide to walk you through setting up Ruby on Rails in OS X Tiger 10.4.8. It was written on 9/27/2006.

The steps below utilize the DarwinPorts application for ease of use, but also because everything ends up installed in the /opt/ directory which means it is extremely easy to take this off if anything fails. You will not end up with nearly as cluttered a system this way as if you try to install from source. This article aims at non-techie people who want to get a development environment up and running to try Ruby on Rails!

Please note, that if you wait until OS X 10.5 Leopard is released. You won’t have to do any of this, but will instead be greeted with Ruby on Rails pre-installed on your system so you’ll just have to add your favorite flavor of database to the mix!

DISCLAIMER: No support is provided or guarantees made that these instructions will work on every system. These instructions were written for an Intel Macbook and were installed from a fresh install of OS X Tiger with all system updates applied. Do not email the author if anything doesn’t work as intended as no technical support will be provided.

Pre-requisite:

DO NOT SKIP THIS STEP!
Install X-Code Tools from your OS X installation CDs. You need to be at version 2.0 at least to continue!!!

After you’ve installed X-Code Tools you may continue to step 1 and begin the installation process.

Getting Started:

1. Download and install MacPorts from http://svn.macports.org/repository/macports/downloads/MacPorts-1.5.0/
a. There is a specific .dmg for Tiger and one for Panther
2. Open up a terminal window (Applications→Utilities→Terminal)

3. Now configure it to work from the terminal
a. add the following line to ~/.profile
export PATH=$PATH:/opt/local/bin
NOTE: If you are unsure how to do this you can type sudo pico ~/.profile. Type in the line and then CTRL+X to leave pico. You will be asked to save it first, so press y to save.

4. Type sudo source ~/.profile to reload your ~/.profile file to allow the changes to take effect.

5. Download the Readline-5.1-patches from: ftp://ftp.cwru.edu/pub/bash/readline-5.1-patches (get all 4)

6. Use Finder to put them in your “/opt/local/var/db/dports/distfiles/readline” directory (You will need to create this readline directory)

(Steps 6 → 10 should all be typed into the open terminal window. These steps can take up to 5 minutes a piece depending on your internet connection and the speed of your computer. The install ruby command takes particularly long!)

7. Type

sudo port install readline

8. Type

sudo port install ruby

9. Type

sudo port install rb-rubygems

You now have everything for ruby with rubygems on your system to begin installing the development environment.
You have a “working” version of Ruby, Readline (think of it as the flux-capacitor for OS X & Ruby on Rails), and Ruby Gems.

10. Type

sudo gem update

11. Type

sudo gem install rails —include-dependencies

12. Type

sudo gem update rake
(for some reason you get rake 0.7.0 when you install rails 1.1.6 and this will update it to the latest version which is 0.7.1 at the time of this writing)

Great! You now have Ruby on Rails installed on your system as well. Now it’s time to isntall a database. In this example we’ll grab MySQL, though there are plenty of tutorials around the ’net that can give you more complete information on using Postgres or another flavor of database with your system.

12. Visit http://dev.mysql.com/downloads/mysql/4.1.html
NOTE: (5.0 has been released as of this writing, but 4.1 is a known quantity and is what most web hosting providers are currently supporting which is why I’m advocating its use in this tutorial!)

13. Continue scrolling down the page until you find the Mac OS X downloads. You can find them about 3/4ths of the way down the screen. Just don’t give up hope. Download the appropriate version for your hardware as there are both PowerPC, 64-bit, and x86 versions to choose from)

14. Follow the appropriate installation procedures for your version that comes included with the MySQL packages. Typically they are pretty straight forward like running 2 pkg files and then a control panel file that adds functinoality to the OS X control panel to start & stop mySQL.
NOTE: DO NOT FORGET TO UPDATE YOUR PATH as instructed in the installation notes on the mySQL website! Make sure you create the alias’ for both mysql and mysql admin!!!

15. Set the password for your root account before moving forward:
(NOTE: STOP mySQL BEFORE DOING THIS!!!)
a. Type mysql -u root mysql (NOTE: If your system doesn’t give you a mysql> prompt you haven’t installed the alias’ yet. Go back and install them or run this from the bin directory where mysql was installed on your system!
b. Type SET PASSWORD FOR root@localhost=PASSWORD;
c. You should get a confirmation that looks like this: “Query OK, 0 rows affected (0.06 sec)”
d. Type quit

At this point I like to download a GUI for mySQL. If you want to purchase one I’d recommend Navicat. If you want a free solution, I’d get CocoaMySQL GUI (http://cocoamysql.sourceforge.net/).

So now you have a complete development environment set up on your machine assuming you aren’t hosting you won’t need FastCGI, or Lighttpd, or apache. The CGI version of Rails runs fast enough for a development environment that loading FastCGI on at this point would be overkill. There is a DarwinPort for FastCGI (fcgi and rb-fcgi) that you can install if you find you need it later. You should be able to run everything from Rails’ built in webrick server.

You may at this point want an IDE. I have used RADRails (http://www.radrails.org) which is built off the eclipse framework and have been very pleased with it. Though TextMate is what you’ll see in the all the Ruby on Rails screencasts and it too is a great tool I’m sure.