Company > Overview > White Papers & Articles > Get JRuby onto the Rails on Mac OS X
With more than 20 years of combined experience in web development, data warehousing, and business analysis, ADS knows how to take your business to the next level.
Get JRuby onto the Rails on Mac OS X
I got very excited about JRuby once I heard about it at Railsconf 2007. I became more excited when I spoke with a number of the JRuby core team members. I spent more than a few days with it, figuring out how to work with it on my Mac (that is used for some serious Rails development mind you), and preparing for a talk I am giving to the Gainesville Java Users Group on June 13th.
Whenever I learn something new, I always like the "for dummies" version, which is what I have attempted to produce in this tutorial series. This first tutorial will cover installing JRuby, creating a test Rails application, and running it under JRuby. Our next tutorial will cover installing Glassfish, a Java application server with a super small footprint. From there, we will learn about deploying a Rails app to Glassfish. Exciting times I tell you!
I asked the JRuby team at Sun to check over the tutorials and have heard that they work. Give them a whirl and then give me some feedback. If you are using JRuby, let me know how you are using it (as the tutorials only cover a few of the uses) and the results you are finding. I look forward to hearing about your experiences.
Before Starting – Assumptions
- You are using a Mac for development
- You have Ruby 1.8.4 or better installed (though not required for the tutorials)
- You have a current version of Java installed
- You are using MySQL as your database server and have it deployed on your development machine
- You have a basic understanding of Ruby on Rails applications and how to create a new Rails application
Requirements
- RubyGems – ActiveRecord-JDBC
- JDK – version 5 or better
- JRuby – 1.0
Setting up JRuby on Mac OS X
To begin, we need to get JRuby installed on our development machine. Easy enough.
- Download JRuby 1.0 (jruby-bin-1.0.tar.gz) from the JRuby downloads page
- Install the Developer Tools that came with your Mac (if you haven’t already)
- Fire up your favorite editor and add the following lines to your ~/.bash_profile (change accordingly for the actual paths on your machine)
export JAVA_HOME='/System/Library/Frameworks/JavaVM.framework/Home' export ANT_HOME='/Developer/Java/ant
- Extract the JRuby binaries and copy the jruby-1.0 folder to wherever you want your JRUBY_HOME to be. As we downloaded the binaries there is no reason to compile.
And there you have it, JRuby on the Mac. Let’s take it a step further, and get MySQL and Rails going as well. Perform the following steps to make it happen:
- Download and unpack the MySQL connector for Java (JDBC driver)
- Copy mysql-connector-java-5.0.6-bin.jar to JRUBY_HOME/lib. This will allow our JRuby apps to connect to a MySQL database.
- In order to use Rails with JRuby, we need to install Ruby on Rails and all of our necessary gems using JRuby. All of the gems installed will be kept in JRUBY_HOME/lib/ruby. One little suggestion is to add the JRUBY_HOME/bin directory to your $PATH, where JRUBY_HOME is the root directory of wherever you put JRuby. Let’s first install Rails:
jruby -S gem install rails -y --no-rdoc --no-ri
- Next, install the rake gem
jruby -S gem install rake
- Finally, install the ActiveRecord-JDBC gem
jruby -S gem install activerecord-jdbc --no-rdoc --no-ri
Done! Let’s get to the fun – setting up and deploying a test Rails app using JRuby.
Set Up and Deploy a Test Rails Application Using JRuby
Getting a Rails app up and going is as easy as pie, and for the most part, follows the same steps as creating a standard Rails app. Let’s dive in!
We are going to be running our commands using “jruby [-S] my_command.” The -S option will run commands in the JRUBY_HOME/lib directory.
- Open up a terminal window, cd to a desired directory, and create a Rails app named, inventively enough, testapp:
jruby -S rails testapp
- Create the develop database in mysql. Following the Rails conventions, name it “testapp_development”
- Fire up your editor of choice and pop open config/database.yml, and change the development database config to the following:
development:
adapter: jdbc
driver: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost/testapp_development
username: root
password:
- Now, to ensure that we are using the proper adapter, we add the following to the top of our environment.rb file underneath the “require File.join…” statement
if RUBY_PLATFORM =~ /java/
require 'rubygems'
RAILS_CONNECTION_ADAPTERS = %w(jdbc)
end
- Let’s have fun and play with some widgets. Keep your comments to yourself and generate your migration:
jruby script/generate migration CreateWidgetsTable
- Add a column named “name” to the migration run it:
jruby -S rake db:migrate
- Next we keep it simple and generate the scaffold code:
jruby script/generate scaffold widget
- Now, bring Webrick to life under JRuby:
jruby script/server
- Fire up your favorite browser (mine is Firefox) and stand (or sit) in awe of the JRuby goodness by browsing: http://localhost:3000/widgets
Happy? I hope so. It doesn’t get much easier than that. Practically 0 learning curve and your fresh cup of coffee isn’t even cold.
What’s next?
A logical question. We aren’t going to be running Rails apps under JRuby on a development machine all the time. In the next tutorial we will cover Glassfish, a small footprint Java app server. Stay tuned.
