From The Blog

Get JRuby onto the Rails on Mac OS X

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...

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.

Other Posts That Might Interest You

  1. Deploy Your First JRuby on Rails App to Glassfish
  2. How to Create and Deploy a JRuby app to the GlassFish gem in 10 minutes or less on Mac OS X
  3. Installing Ruby, Rails, and MySQL on Mac OS X with Macports

Tags: 

  • Great right up, thanks for this.
  • Gary S. Weaver
    Very helpful!

    Looks like instead of:

    jruby -S gem install activerecord-jdbc --no-rdoc --no-ri

    it should be:

    jruby -S gem install activerecord-jdbc-adapter --no-rdoc --no-ri

    and depending on whether you care what version of mysql JDBC jar you get, instead of:

    "Copy mysql-connector-java-5.0.6-bin.jar to JRUBY_HOME/lib"

    You could just include:

    jruby -S gem install activerecord-jdbcmysql-adapter --no-rdoc --no-ri

    Although I like the idea of keeping the JDBC jars in JRUBY_HOME/lib since that is where the oracle (example: ojdbc14.jar) JDBC drivers jar would go.
  • Leonhard
    ... just started with JRUBY - thanks for the great - easy to start tut - looking forward to new tuts ...
  • 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!
  • Thomas Fee
    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.
  • Manikandan,

    What platform are you trying to use? This article pertains specifically to Mac (which is what we use here at ADS).
  • Manikandan
    please anybody help how to install jruby
  • Matt Margolis
    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.
  • 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.
  • 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
  • @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
  • You can find some more JRuby and Ruby articles here:

    http://www.developer.com/lang/article.php/3669041
  • omoniaja
    i tried runing d cookbook rails app with grizzy. I run jruby 1.0,jdk1.6. Any help wld be appreciated.
  • omoniaja
    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'
  • 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.
  • Ryan Hanks
    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).
  • Ryan Hanks
    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:


  • Ryan Hanks
    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:
  • 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!
  • 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.
  • 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.
  • How long should I expect "ant test" to take? I am using RC3, but it has been over an hour now.
  • 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.
  • 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.
  • Thank you. The next in the series will be released next week.
  • Nice post!

    Thank you for sharing
blog comments powered by Disqus