2008/12/14

JRuby on Ubuntu

Let me share you a few steps (considered to be a non-standard way) of building a good little dev environment on Ubuntu Linux.

Precautions:
1. Again, this is not the way you should build your environment, I just found it useful
2. Do not rush and copy paste everything to your console, get used to your terminal

To get Jruby on your box you will need Java JDK. I recommend to use SUN Java but JRuby should run on other FLOSS version of Java as well. If fails, use the SUN one.
sudo apt-get install sun-java6-jdk

It will take a while :) To check the Java version type:
java -version

Mine says it is
Java(TM) SE Runtime Environment (build 1.6.0_10-b33)
Java HotSpot(TM) 64-Bit Server VM (build 11.0-b15, mixed mode)


OK. Let's create some space for the sources and the dev environment. I assume you are in your home directory. If not, change to that directory.
mkdir dev dev-sources
cd dev-sources


To get Jruby standard dsitribution, I recommend to get the binary version of 1.1.5. When wget finished, unpack the tarball.
wget http://dist.codehaus.org/jruby/jruby-bin-1.1.5.tar.gz
tar -xf jruby-bin-1.1.5.tar.gz


To move Jruby to the dev environment, let's move it :) You won't need the distrib file so its safe to remove it from the disk.
mv jruby-1.1.5 ../dev/jruby
rm -f jruby-bin-1.1.5.tar.gz


To lazy and easy access to JRuby executables you have (at least) two options. Either add a symbolic link to jruby/bin/jruby, or add the complete bin directory to your path variable. To add a symbolic link do the following - replace $yourusername with your real username (if you don't know your username, ask Linux with the whoami command):
sudo ln -s /home/$yourusername/dev/jruby/bin/jruby /usr/local/bin/jruby

Or if you want to add JRuby executables to your path, open up your .bashrc file with a text editor and set at the end of the file:
export PATH=${PATH}:${HOME}/dev/jruby/bin

Save and reload the rc file
source .bashrc

Now you can check the version from anywhere:
jruby -v

Check the local gems installed too. I think it is worth while to add GitHub to your rubygem source - it is a nice place to store and share your code (free for open source projects).
jruby -S gem list -l
jruby -S gem sources --add http://gems.github.com

Cool, we have JRuby installed! Now we need some kind of a database. I won't use sqlite anymore (if possible) but found a great engine that is faster and knows a lots more.

To get h2 database engine get back to your terminal
cd dev-sources
wget http://www.h2database.com/h2-2008-09-26.zip


Unpack the archive
unzip h2-2008-09-26.zip

Move the entire engine to dev, then remove the source archive.
mv h2 ../dev/h2
rm -f h2-2008-09-26.zip


Change to h2 application directory and fire up the database server.
cd ../dev/h2/bin
java -cp h2.jar org.h2.tools.Server


Create a database of your taste. I recommend to set the jdbc url to jdbc:h2:~/dev/db/test

It will create a few files under /home/$yourusername/dev/db/test and generate dynamically more when running. Play around with the db admin interface a bit to get familiar with it. Have you recognized that you get a built in admin interface? Nice huh?

OK. Let's adds some more gems to JRuby. Cool kids use Rails but if you prefer other frameworks its up to you.
jruby -S gem install jruby-openssl activerecord-jdbch2-adapter rails mongrel

You can test the database via JRuby irb if you prefer.
jruby -S irb
irb(main):001:0> require 'rubygems'
irb(main):002:0> require 'jdbc/h2'
=> true
irb(main):003:0>


If you see this that means the connector working properly.

Nice, let's do something with Rails!
mkdir ~/dev/ruby_apps
cd ruby_apps
rails myapp


Edit your database config file under config/database.yml


To generate some code to your app let's do the scaffolding.
jruby script/generate scaffold post title:string body:text

Don't forget do create the databases and migrate the structure.
rake db:create:all
rake db:migrate


Fire up the server.
mongrel_rails start

And see the result http://localhost:3000/posts

Good job, let's roll :)

0 comments: