I’ve seen several guides to installing eRuby on OSX - none of which I could get to work for Leopard. But after much trial and error, I’ve managed to get eRuby up and running on Leopard, using a special mix’n’match blend, culled from various bits of other tutorials on the web.
So here is my guide to installing eRuby on OSX Leopard. As I say, this worked for me, when other guides didnae, but I’m not guaranteeing anything. 'Your mileage' - as they say
- 'may vary' and if you completely blow up your computer, following these instructions, you’re on your own!
[If you’re the impatient type, you can bypass the waffle and jump straight to a precis’ed code listing at the end by clicking here. The rest of you read on…]
First things first; fire up Terminal.app so you can grab yourself a copy of eRuby. At the time of writing the latest version is 1,0,5 but you can check modruby.org’s archives before downloading, to see if there’s a newer version and if necessary, adapt the following terminal commands:
download eRuby to DOWNLOADS folder and unarchive it
cd ~/Downloads
curl -O http://www.modruby.org/archive/eRuby-1.0.5.tar.gz
tar -xzvf eRuby-1.0.5.tar.gz
cd eRuby-1.0.5/
Run the 'configure', 'make' and 'install' commands [as root]
./configure.rb
make
sudo make install
Alias eRuby into Leopard’s existing Ruby frameworks
sudo ln -s /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/eRuby /usr/local/bin/eRuby
[NOTE: the above is all one line - not two separate commands!]
You should now have eRuby working at a system level. Close your terminal window and open a new one [to refresh your session] and then type eRuby ---version. The terminal should greet you with something along these lines:
eRuby version 1.0.5.
If not - yep. You guessed it - go back to the beginning and start again. Do not pass go. Do not collect £200!
Assuming eRuby is up and running, let’s test it on a file containing some Ruby code. Fire up your favourite text editor and create a new plaintext file with the following content:
hello world! the time is now <%= Time.now %>
Save the file as rubytest.rhtml or rubytest.erb [either extension is valid] into your ~/Sites folder.
Now switch back to the terminal and let’s see what eRuby makes of that file:
eRuby ~/Sites/rubytest.erb
hello world! the time is now Thu Mar 27 19:47:53 +0000 2008
Pretty cool! - now let’s see what happens if we view it in a browser. Make sure you’ve enabled websharing in 'SYSTEM PREFERENCES > SHARING', so that your Apache server is running..
…and then goto the following URL in your browser [rem to replace 'username' with your username for your OSX account]- http://127.0.0.1/~username/rubytest.erb
Whoops! - what’s going on?
Well, you’ve wired up Leopard for eRuby, but you havenae told Apache what to do with Ruby embedded in HTML files yet. So let’s take care of that now.
Back to the terminal. You need to edit Apache’s configuration file which, in Leopard is found at /etc/apache2/httpd.conf. you can either edit this directly in the terminal using sudo nano /etc/apache2/httpd.conf - or, if you prefer to stay out of the terminal, use a texteditor which will allow you to authenticate on saving [You dinnae own /etc/apache2/httpd.conf, so you’ll have to enter an administrator password to save changes to it].
My own particular favourite text editor is TextMate.
Open /etc/apache2/httpd.conf and make sure that the following line is not commented out. ie. If there’s a # at the start of the line, remove it:
ScriptAliasMatch ^/cgi-bin/((?!(?i:webobjects)).\*$) “/Library/WebServer/CGI-Executables/$1”
From what i’ve read, this line can also look like this:
ScriptAlias /cgi-bin/ “/Library/WebServer/CGI-Executables/”
But the first version is what I have. I suspect the latter may be the Tiger version.
This tells Apache to treat any files it finds in 'library/webServer/CGI-executables' as if they were in 'cgi-bin' and therefore as executable scripts.
After making sure that line is uncommented you need to search for a block which looks like this:
<Directory “/Library/WebServer/CGI-Executables”>
AllowOverride None
Options None
Order allow,deny
Allow from all
</Directory>
When you’ve found that, change the 'Options' line so the block reads as follows:
<Directory “/Library/WebServer/CGI-Executables”>
AllowOverride None
Options FollowSymLinks
Order allow,deny
Allow from all
</Directory>
This tells Apache to recognise aliases in the 'library/webServer/CGI-executables' directory. So if you place an alias to eRuby in there, Apache will be able to find eRuby itself. You can do that in a bit. For now there’s a couple more tweaks to make while you’ve got httpd.conf at your mercy. You need to tell Apache to hand off any files ending in .rhtml or .erb to eRuby in the apache cgi-bin
[Only eRuby will actually be an alias inside a directory which is in itself an alias for the cgi-bin - phew!]
The following lines need to be added to httpd.conf somewhere between the <IfModule mime_module> and </IfModule> tags. Those two tags are spaced pretty far apart, with loads of comments and other lines of code in between, but as long as you make sure and put the two ruby lines somewhere between these two tags, you should be OK
<IfModule mime\_module>
—— lots of code and comments —-
AddHandler rubypage .erb .rhtml
Action rubypage /cgi-bin/eRuby
—— lots of code and comments —-
</IfModule>
Save your changes to httpd.conf and you’re almost done. You’ve just got to put that alias to eRuby, that I mentioned above, into Apache’s library/webServer/CGI-executables directory. So back into the terminal with you and put in the following:
ln -s /usr/local/bin/eRuby /Library/WebServer/CGI-Executables/eRuby
Now, you just need to restart Apache and you should be good to go. Before I restart Apache, I always like to check I havenae ballsed anything up while messing with the httpd.conf file - by using apachectl configtest:
apachectl configtest
Syntax OK
If you didnae get a 'Syntax OK' from Apache, you’ve cocked up somewhere, while editing /etc/apache2/httpd.conf and you’re going to have to backtrack and fix it.
Assuming apache’s given you the green light, it’s time to restart the webserver [as root].
sudo apachectl restart
Now switch back to your browser again and reload that rubytest.erb [or rubytest.rhtml] page from before. if all’s gone according to plan, you should now see some proper ruby output in your browser
Woohoo! - That’s more like it! Now all you’ve got to do is learn some feckin’ Ruby to stick in there!
Now. here’s the précis version for folks who just want the commands, without the waffle
[TERMINAL: download, make, install, alias, test eRuby]
cd ~/Downloads
curl -O [http://www.modruby.org/archive/eRuby-1.0.5.tar.gz](http://www.modruby.org/archive/eRuby-1.0.5.tar.gz)
tar -xvf eRuby-1.0.5.tar.gz
cd eRuby-1.0.5/
./configure.rb
make
sudo make install
sudo ln -s /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/eRuby /usr/local/bin/eRuby
eRuby –-version
[TEXT EDITOR: edit/etc/apache2/httpd.conf]
ScriptAliasMatch ^/cgi-bin/((?!(?i:webobjects)).\*$) “/Library/WebServer/CGI-Executables/$1”
<Directory “/Library/WebServer/CGI-Executables”>
AllowOverride None
Options FollowSymLinks
Order allow,deny
Allow from all
</Directory>
<IfModule mime\_module>
——
AddHandler rubypage .erb .rhtml
Action rubypage /cgi-bin/eRuby
——
</IfModule>
[TERMINAL: alias eRuby to apache cgi-bin]
ln -s /usr/local/bin/eRuby /Library/WebServer/CGI-Executables/eRuby
[TERMINAL: test apache config and restart]
apachectl configtest
sudo apachectl restart