Archive for the ‘ruby’ Category

Prepping for @MadisonRuby with RVM JRuby and Rails

@MadisonRuby is finally here. I’m excited to attend the conference at my home state’s capital. It will be a unique opportunity to meet up with many of the contributors and characters known in the Ruby community, and to finally put a voice and face to the name. There will be plenty to learn and do at the conference, and lots of kick-ass people to meet while we’re there.

Before we get to the conference, I figured it would be wise to get my local environment prepped and ready for some mid-conference hacking sessions so I’m not scrambling to install stuff I need while I’m at the conference.

I find that searching through a ton of different blogs to find the right steps to complete can be a pain, especially when under a time crunch at a conference, when you just want to Get Things Done.

If you don’t have a jruby environment installed yet, hopefully you will find my bash script reusable for your environment. The goal is to simply install RVM, JRuby and Rails with one simple step. In addition, I have added the installation of essential Ruby gems that you will need to use in order to develop rails apps in JRuby, like the jdbc activerecord sqlite3 adapter, and even deployment/debugging gems.

This script is intended to be used from your user/home directory. If you encounter any issues using the script, please let me know as soon as possible and we can work towards fixing it before the conference this week. Enjoy! I welcome any kind of feedback. I’ll see you on Thursday, Friday and Saturday in Mad Town!

# Install RVM

curl -s;

# Place RVM in .bash_profile so that it's recognized as a command
echo "[[ -s "/Users/`echo $USER`/.rvm/scripts/rvm" ]] && source "/Users/`echo $USER`/.rvm/scripts/rvm"  # This loads RVM into a shell session." >> .bash_profile

source ~/.bash_profile;

# Install JRuby
rvm install jruby-1.6.5;

# Set JRuby paths in your .bash_profile
echo "export JRUBY_HOME=/Users/`echo $USER`/.rvm/src/jruby-1.6.5" >> .bash_profile
echo "export PATH=$JRUBY_HOME/bin:$PATH" >> .bash_profile

source ~/.bash_profile;

# Activate JRuby
rvm jruby-1.6.5;
rvm use jruby-1.6.5;

# Update the core gem system
gem update --system

# Install core gems needed for rails / jruby development
gem install rails;
gem install jruby-openssl;
gem install activerecord-jdbcsqlite3-adapter;

# Deploy with Capistrano
gem install 'capistrano'

# To use debugger (ruby-debug for Ruby 1.8.7+, ruby-debug19 for Ruby 1.9.2+)
gem install 'ruby-debug'
gem install 'ruby-debug19', :require => 'ruby-debug'

# Bundle the extra gems:
gem install 'bj'
gem install 'nokogiri'


Avoid Dependency Whack-a-mole with Bundler

We’ve all been there. At one time or another, we’ve installed a gem that has other dependencies that conflict with other gems on our system, and we end up playing dependency whack-a-mole or monkey-patching our way to a working environment. Along comes Bundler, a tool with a goal to be the solution to this problem.

Bundler assumes you have a Gemfile at the root of your project; this applies not only rails projects, but to other Ruby projects or gems as well. Entries in the Gemfile look like the following:

#Gemfile format: gem , (, :require => ) (, :git=>repo)
gem ‘activemerchant’, ‘1.4.2’
gem ‘nokogiri’
gem ‘faker’, ‘> 0.3’
gem ‘decent_exposure’, ‘~> 1.0.0.rc1’
gem ‘rspec’, ‘2.0.0.beta.20’
gem ‘sqlite3-ruby’, :require => ‘sqlite3’
gam ‘paperclip’, :git => ‘git://’
gem ‘deep_merge’, ‘1.0’, :git => ‘git://’

# specify a git repo should use a particular branch as options to git directive
git ‘git://’, :branch => ‘2-3-stable’

# install from a specific branch when installing a gem inline
gem ‘nokogiri’, :igt => ‘git://’, :ref => ‘0eec4’

#install from local code
gem ‘nokogiri’, :path => ‘~/code/nokogiri’

Once you have your Gemfile ready to go and all dependencies listed, you can run:
bundle install
to ensure all dependencies will be installed and available to your application. You may notice that this will start to install more gems than you’ve listed in your Gemfile. This is because it ensures that any dependencies of your listed dependencies are installed, and so on. Bundler is as conservative in its installation as possible, only installing dependencies with versions that do not conflict with other dependencies.

If you have listed libraries that should only be installed in certain environments, you may run:
bundle install –without development test

The default location for bundler installations is ~/.bundler. To change this, use:
bundle install

To force installation and not use previously installed gems, use:
bundle install –disable-shared-gems

Once you’ve installed gems or used bundle update, the dependency tree is stored in Gemfile.lock.
It is good practice to check in your lock file into the repository so that everyone who gets your project can be sure to also install the same exact versions of dependencies that worked most recently.

If you’re developing a Rails application, you can go even one step further to make sure all of your dependencies are packaged up with your rails application structure under vendor/cache:
bundle package

This is especially useful at deploy time, or where you need to depend on private gems not in a public repo.
Bundler has become popular with the advent of Rails 3.0 and later, but you may also want to use this handy tool with a ‘legacy’ Rails 2.3 application. Luckily, the Bundler team has documented the steps you’ll need to take to make this happen for your project.