- Jun
- 01
- 2007
Get JRuby onto the Rails on Mac OS X
By: Robert Dempsey | Tags:Updated for Version 1.0
I got very excited about JRuby once I heard about it at Railsconf. 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.
Share this post
Random Posts
You can leave a response, or trackback from your own site.


34 Responses to “Get JRuby onto the Rails on Mac OS X”
On June 2nd, 2007 at 2:10 am Ruby on Rails Examples said:
Nice post!
Thank you for sharing
On June 2nd, 2007 at 10:03 am Robert Dempsey said:
Thank you. The next in the series will be released next week.
On June 3rd, 2007 at 9:42 am bryanl said:
This article has potential. There are a few errors though:
- the ANT_HOME is incorrect
- You should run “ant” before “ant test”, so the test directory gets created
- “jruby -S gem install rake” didn’t work for me. I had more luck with “jruby -S gem install –remote rake”
- You have to add “jdbc” to the list of RAILS_CONNECTION_ADAPTER in active_record.rb
- Why not just use “jruby script/generate scaffold_resource”?
Other than that, I found this article informative.
On June 3rd, 2007 at 12:01 pm Robert Dempsey said:
Bryan,
Thank you for the feedback. To answer your errors:
- the ANT_HOME is incorrect
>> I am sure that your development environment is different that mine. When I do “echo $ANT_HOME,” I get “/Developer/Java/ant”. When I do “echo “$JAVA_HOME,” I get “/System/Library/Frameworks/JavaVM.framework/Home.” Your results for ANT_HOME may vary depending on how you installed the Mac developer tools.
- You should run ?¢‚Ǩ?ìant?¢‚Ǩ¬ù before ?¢‚Ǩ?ìant test?¢‚Ǩ¬ù, so the test directory gets created
>> Noted and updated
- ?¢‚Ǩ?ìjruby -S gem install rake?¢‚Ǩ¬ù didn?¢‚Ǩ‚Ñ¢t work for me. I had more luck with ?¢‚Ǩ?ìjruby -S gem install ?¢‚Ǩ‚Äúremote rake?¢‚Ǩ¬ù
>> Interesting. I will look into that one as running “jruby -S gem install mygem” has worked for all gems on my machine. “Worked for me,” - famous last words ;).
- You have to add ?¢‚Ǩ?ìjdbc?¢‚Ǩ¬ù to the list of RAILS_CONNECTION_ADAPTER in active_record.rb
>> THANK YOU. I completely missed the lines to add to environment.rb. I have updated the tutorial accordingly.
- Why not just use ?¢‚Ǩ?ìjruby script/generate scaffold_resource?¢‚Ǩ¬ù?
>> Keeping it simple. That would be the way to go to make it RESTful though.
On June 4th, 2007 at 9:56 am jerry richardson said:
How long should I expect “ant test” to take? I am using RC3, but it has been over an hour now.
On June 4th, 2007 at 10:28 am Robert Dempsey said:
Jerry,
It only took a few minutes on my machine. Try using RC2 and let me know how that works. I will try with RC3 as soon as I have a chance - hopefully today or tomorrow. If you find out anything in the meantime, please let us know. Thank you for the FYI.
On June 4th, 2007 at 12:30 pm jerry richardson said:
i cut it off after three hours. retried it and it took 3 minutes and 23 seconds. odd. but I am now feeling my way through the testapp. thanks so much for this guide.
On June 4th, 2007 at 1:56 pm Robert Dempsey said:
Jerry,
My pleasure. Stay tuned as there are two more on the way. We will next cover installing Glassfish and getting Mephisto running under it. I am also looking into extending Java/Ruby classes using the other. Very exciting stuff!
On June 5th, 2007 at 3:04 pm Ryan Hanks said:
This worked for me with RC3, with only the following stumble. I copied and pasted the database.yml setting into textmate, went back and made sure the tabs were all spaces, and still had the following error:
ScannerImpl.java:1048:in `org.jvyamlb.ScannerImpl.fetchValue’:NativeException: org.jvyamlb.ScannerException: ScannerException null we had this mapping values are not allowed here
my development entry needed to look like this:
development:
adapter: jdbc
driver: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost/testapp_development
username: root
password:
On June 5th, 2007 at 3:06 pm Ryan Hanks said:
ok so that last post didn’t appear as i thought it would. i’ll try it again
development:
adapter: jdbc
driver: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost/testapp_development
username: root
password:
On June 5th, 2007 at 3:09 pm Ryan Hanks said:
and neither did that one. note to blog owner:
tags don't work in comments.bottom line: there only needs to be one level of indentation in the development entry of database.yml (not two as appears in the original post).
On June 5th, 2007 at 3:51 pm Robert Dempsey said:
Ryan,
Thank you for pointing it out. My indentation didn’t come out correctly either. My tutorial did assume a basic understanding of Ruby on Rails so hopefully the folks will know.
On June 9th, 2007 at 10:04 am Atlantic Dominion Solutions » Blog Archive » JRuby Introduction Presentation said:
[...] JRuby and its benefits to both Java and Ruby developers, and include two code demos. Visit the ADS blog for an introduction to getting JRuby running on Mac OS X and creating a Rails app that runs on [...]
On June 9th, 2007 at 11:41 am Atlantic Dominion Solutions » Blog Archive » JRuby 1.0 Released said:
[...] that ThoughtWorks is going to provide commercial support for JRuby. Don’t forget to check out our tutorial on getting JRuby up and running on Mac OS X. We will update the tutorial for version 1.0, and [...]
On June 10th, 2007 at 1:36 pm Atlantic Dominion Solutions » Blog Archive » Deploy Your First JRuby on Rails App to Glassfish said:
[...] we have JRuby installed, have created our first Rails app and have it running under JRuby, the next step is deploy our app [...]
On June 12th, 2007 at 7:56 am omoniaja said:
I folks! anyB tried running jruby with grizzy server. I av bin tryin for dyaz now but jkeep getting dis error msg:
The driver encountered an error: cannot load Java class mysql
C:/ruby-dev/jruby-1.0/lib/ruby/gems/1.8/gems/ActiveRecord-JDBC-0.4/lib/active_record/connection_adapters/jdbc_adapter.rb:211:in `initialize’
C:/ruby-dev/jruby-1.0/lib/ruby/gems/1.8/gems/ActiveRecord-JDBC-0.4/lib/active_record/connection_adapters/jdbc_adapter.rb:10:in `new’
C:/ruby-dev/jruby-1.0/lib/ruby/gems/1.8/gems/ActiveRecord-JDBC-0.4/lib/active_record/connection_adapters/jdbc_adapter.rb:10:in `jdbc_connection’
C:/ruby-dev/jruby-1.0/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/connection_adapters/abstract/connection_specification.rb:262:in `send’
C:/ruby-dev/jruby-1.0/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/connection_adapters/abstract/connection_specification.rb:262:in `connection=’
C:/ruby-dev/jruby-1.0/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/query_cache.rb:54:in `connection=’
C:/ruby-dev/jruby-1.0/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/connection_adapters/abstract/connection_specification.rb:230:in `retrieve_connection’
C:/ruby-dev/jruby-1.0/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/connection_adapters/abstract/connection_specification.rb:78:in `connection’
C:/ruby-dev/jruby-1.0/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1139:in `add_limit!’
C:/ruby-dev/jruby-1.0/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1101:in `construct_finder_sql’
C:/ruby-dev/jruby-1.0/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:998:in `find_every’
C:/ruby-dev/jruby-1.0/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:418:in `find’
C:/ruby-dev/jruby-1.0/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/deprecated_finders.rb:39:in `find_all’
C:/ruby-dev/jruby-1.0/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/deprecation.rb:3:in `find_all_with_deprecation’
#{RAILS_ROOT}/app/controllers/recipe_controller.rb:12:in `list’
On June 12th, 2007 at 7:59 am omoniaja said:
i tried runing d cookbook rails app with grizzy. I run jruby 1.0,jdk1.6. Any help wld be appreciated.
On June 13th, 2007 at 5:11 am li.mn » Blog Archive » SQS works in JRuby said:
[...] These instructions were very helpful. [...]
On June 13th, 2007 at 9:21 am dsuspense said:
You can find some more JRuby and Ruby articles here:
http://www.developer.com/lang/article.php/3669041
On June 15th, 2007 at 9:01 am Robert Dempsey said:
@omoniaja,
I have seen some discussion of jruby and grizzly on the JRuby “users” mailing list. Here is the link: http://xircles.codehaus.org/projects/jruby/lists
On June 20th, 2007 at 8:09 am Don Franke said:
My problem was the step: “Add a column named ?¢‚Ǩ?ìname?¢‚Ǩ¬ù to the migration run it” Some elaboration was needed for myself–I had to figure out that I had to edit the file testapp/db/migrate/001_create_widgets_table.rb and change it to something like the following:
class CreateWidgetsTable
def self.up
create_table :widgets do |table|
table.column :name, :string
end
end
def self.down
drop_table :widgets
end
end
On June 20th, 2007 at 9:06 am Robert Dempsey said:
Don,
Thanks for pointing that out. The little line under assumptions stating a basic understanding of Rails will get folks every time. In other tutorials I will remain cognizant of those who may be working with JRuby but have no experience with Ruby on Rails.
On July 8th, 2007 at 2:53 am Vysnu » JRuby/Glassfish Investigation said:
[...] made a simple widgets application as outlined here and installed JRuby/Glassfish as outlined here (both great articles at AD TechFL) and then [...]
On July 18th, 2007 at 5:08 am Install JRuby and Build a Ruby on Rails Application on Mac OS X » Atlantic Dominion Solutions said:
[...] a series of screencast tutorials. We are starting out with JRuby. In this screencast, we took the first of our JRuby tutorials and made it happen. There is much more to come so stay tuned, and if you like it, let us know. As [...]
On July 22nd, 2007 at 3:39 am productive.dk » JRuby on Rails Screencast said:
[...] Atlantic Dominion Solutions has posted a JRuby on Rails screencast. It is based on their excellent first JRuby tutorial, Get JRuby onto the Rails on Mac OS X. [...]
On July 23rd, 2007 at 10:44 am Matt Margolis said:
To view the Rails logs if you are using Goldspike and Glassfish you can check out
/domains/domain1/applications/j2ee-modules/appname/logs
This was not easy to find so hopefully this comment will help make this knowledge more accessible from Google.
On September 1st, 2007 at 10:22 pm my so-called blog » links for 2007-09-02 said:
[...] Ruby on Rails Website Development Blog from Atlantic Dominion Solutions 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. (tags: jruby java RubyOnRails glassfish ruby WebDev macosx) [...]
On September 6th, 2007 at 6:13 am Manikandan said:
please anybody help how to install jruby
On September 6th, 2007 at 8:27 am Robert Dempsey said:
Manikandan,
What platform are you trying to use? This article pertains specifically to Mac (which is what we use here at ADS).
On October 5th, 2007 at 2:30 am Thomas Fee said:
Thanks for the help. Your title says this is for Mac OS X, but it worked for me for Linux also. I’m running the CentOS 5 edition.
On November 8th, 2007 at 9:28 am Follow-up on my presentation on JRuby « Programblings said:
[...] Getting JRuby and Rails running. Deploying to Glassfish. Theirs posts are pretty detailed, but since things are moving so fast in the JRuby world, some of the details are a bit out of date already
In it they use Goldspike to generate their WAR archive, just like I did. As I mentioned however, a post by Nick Sieger introduced me to warbler as another very promising replacement to Goldspike. To be more accurate, as Nick describes warbler, I fully expect to try it and stick with it instead of Goldspike. Defenitely worth a try. [...]
On March 1st, 2008 at 7:50 pm Programblings » Blog Archive » Follow-up on my presentation on JRuby said:
[...] Getting JRuby and Rails running. Deploying to Glassfish. Theirs posts are pretty detailed, but since things are moving so fast in the JRuby world, some of the details are a bit out of date already
In it they use Goldspike to generate their WAR archive, just like I did. As I mentioned however, a post by Nick Sieger introduced me to warbler as another very promising replacement to Goldspike. In fact, as Nick describes warbler, I fully expect to try it and stick with it instead of Goldspike. Definitely worth a try. [...]
On March 7th, 2008 at 3:50 am Manuel said:
The BitNami team has released the JRubyStack.
A very easy to use installer that provides JRuby, Rails, Java, Tomcat, GlassFish gem, MySQL and Subversion. We worked to make the deployment of Ruby apps on Tomcat and Glassfish as smooth as possible.
JRubyStack is available for Mac OS X, Windows and Linux and you can download it from: http://bitnami.org/stack/jrubystack
Thanks!
On May 14th, 2008 at 4:36 pm Leonhard said:
… just started with JRUBY - thanks for the great - easy to start tut - looking forward to new tuts …