How to install RVM on Ubuntu 10.04

by Irish on August 25, 2010

Recently I’ve heard people describe frustrations they had in getting Rails 3 setup on Ubuntu 10.04 with Ruby 1.9.2. I’m going to outline the steps I’ve taken to do this, with this being the first part. Let’s get started installing RVM!

It should be noted that I created a fresh slice on my Rackspace Cloud account to create this tutorial.

$ ssh root@173.XXX.XXX.XXX
<-- Output Snipped -->
root@173.203.108.222's password:
<-- Output Snipped -->

Alright, so lets confirm we don’t have ruby installed

$ which ruby
$

Cool, nada. Let’s grab the basic dependencies

$ sudo apt-get install curl git-core ruby
Reading package lists... Done
Building dependency tree
<-- Output Snipped -->

Now we’ll install RVM as per their instructions

http://rvm.beginrescueend.com/rvm/install/

$ bash < <( curl http://rvm.beginrescueend.com/releases/rvm-install-head )

<-- Output Snipped -->

RVM: shell scripts which allow management of multiple ruby interpreters and environments.
RTFM: http://rvm.beginrescueend.com/
HELP: http://webchat.freenode.net/?channels=rvm (#rvm on irc.freenode.net)

* Installing rvm to /usr/local/rvm/
Correct permissions for base binaries in /usr/local/bin...
Copying manpages into place.

Notes for Linux ( DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=10.04
DISTRIB_CODENAME=lucid
DISTRIB_DESCRIPTION="Ubuntu 10.04 LTS" )

<-- Output Snipped -->

* For JRuby (if you wish to use it) you will need:
$ aptitude install curl sun-java6-bin sun-java6-jre sun-java6-jdk
* For MRI & ree (if you wish to use it) you will need (depending on what you are installing):
$ aptitude install build-essential bison openssl libreadline5 libreadline-dev curl git-core zlib1g zlib1g-dev libssl-dev vim libsqlite3-0 libsqlite3-dev sqlite3 libreadline-dev libxml2-dev git-core subversion autoconf
* For IronRuby (if you wish to use it) you will need:
$ aptitude install curl mono-2.0-devel

<-- Output Snipped -->

WARNING: you have a 'return' statement in your .bashrc, likely this will cause untold havoc.
This means that if you see '[ -z "$PS1" ] && return' then you must change this line to:
if [[ -n "$PS1" ]] ; then
... original content that was below the && return line ...
fi # <= be sure to close the if.
#EOF .bashrc
Even if you use zsh you should still adjust the .bashrc as above.
If you have any questions about this please visit #rvm on irc.freenode.net.

Installation of RVM to /usr/local/rvm/ is complete.

The last part in there about the .bashrc file is important! Using your editor of choice, open the .bashrc file that resides in your user directory.

You need to replace the line that says

[ -z "$PS1" ] && return

with

if [[ -n "$PS1" ]]; then

Now add this to the last line of the file

if [[ -s $HOME/.rvm/scripts/rvm ]] ; then source $HOME/.rvm/scripts/rvm ; fi

fi

And yes there needs to be that last fi as it closes the one we added earlier. Save this file with our changes. Now we can check if RVM is setup correctly.

$ rvm notes

If this gives you installation notes about RVM then you’re good to continue, otherwise double check you edited your .bashrc file correctly.

In the notes output RVM tells you what packages you’re gonna need to install for various flavors of Ruby. Since we’re going with 1.9.2 we want the packages it lists under the MRI & ree section. Let’s install those now.

$ sudo aptitude install build-essential bison openssl libreadline5 libreadline-dev curl git-core zlib1g zlib1g-dev libssl-dev vim libsqlite3-0 libsqlite3-dev sqlite3 libreadline-dev libxml2-dev git-core subversion autoconf

<-- A really long bit of Output Snipped -->

Now we can look at all the RVM known Ruby packages

$ rvm list known
# MRI Rubies
(ruby-)1.8.6(-p399)
(ruby-)1.8.6-head
(ruby-)1.8.7(-p302)
(ruby-)1.8.7-head
(ruby-)1.9.1-p243
(ruby-)1.9.1-p376
(ruby-)1.9.1(-p429)
(ruby-)1.9.1-head
(ruby-)1.9.2-preview1
(ruby-)1.9.2-preview3
(ruby-)1.9.2-rc1
(ruby-)1.9.2-rc2
(ruby-)1.9.2(-p0)
(ruby-)1.9.2-head
ruby-head

# JRuby
jruby-1.2.0
jruby-1.3.1
jruby-1.4.0
jruby-1.5.1
jruby(-1.5.2)
jruby-head

<-- Output Snipped -->

Next we install the version we want and set it as the default. You’re gonna see RVM install ruby-1.8.7-p302 first, be patient as it will need to also install rubygems-1.3.7, before it finally gets to that 1.9.2.

$ rvm install 1.9.2-head
<-- Output Snipped -->
info: Installing ruby-1.8.7-p302
<-- Output Snipped -->
info: Installing rubygems dedicated to ruby-1.8.7-p302...
<-- Output Snipped -->
info: Installing ruby-1.9.2-head
<-- Output Snipped -->
info: Updating rubygems for /usr/local/rvm/gems/ruby-1.9.2-head
$ rvm --default 1.9.2-head
$ ruby -v
$ ruby 1.9.2p0 (2010-08-18 revision 29034) [x86_64-linux]

Good to go. In the next post I’ll cover setting up Rails 3.

{ 5 comments }

On a project I’m currently working on, we use ActiveRecord callbacks heavily. From simplying creating other associated objects, to recording the historic activity about the object in question, and more.

The drawback I find from the scope of AR callbacks though is that you only have direct access to data from the database. And since you can’t pass arguments to a AR callback, you can’t easily get the request params for example or know which controller action fired off the the AR chain that triggered the callback you’re in, or know who the current user logged in is.

But it turns out that there is a way, however it is neither straight forward or obvious. Say for example you have the following model and controller:

    1 class Post < ActiveRecord::Base
    2   belongs_to :user
    3   belongs_to :group
    4   has_one    :alert, :as => 'alertable'
    5
    6   after_update :note_activity
    7
    8   private
    9   def note_activity
   10     group.admins.each do |admin|
   11       # What user is doing this update?
   12       admin.notifications.create(:user => "Someone",
   13                                  :action => 'did something')
   14     end
   15   end
   16 end
    1 class Admin::PostsController < ApplicationController
    2
    3   def update
    4     @post = Post.find(params[:id])
    5
    6     if @post.update_attributes(params[:post])
    7       redirect_to admin_posts_path
    8     else
    9       render :action => 'edit'
   10     end
   11   end
   12 end

What we want to accomplish here is that all admins of a group get a notification when another admin user edits a post. The post in question here has no context in it’s callback of which admin was logged in at the time the update happened. So what we’re gonna do is put the admin user’s id onto the current thread of the request in a before filter. Then, we’ll pull that id off in the callback to look up the admin and create the notifications.

So we’ll add a simple before filter:

    1 class ApplicationController < ActionController::Base
    2   before_filter :store_user
    3
    4   def store_user
    5     User.current = current_user
    6   end
    7 end

And create a couple class methods on User to store the currently logged in admin for the duration of the request:

    1 class User < ActiveRecord::Base
    2
    3   def self.current=(user)
    4     Thread.current['user'] = user.try(:id)
    5   end
    6
    7   def self.current
    8     @@current ||= User.find_by_id(Thread.current['user'])
    9   end
   10 end

Now we can alter our note_activity method in Post to get the admin who did the edit:

    1 def note_activity
    2   group.admins.each do |admin|
    3     admin.notifications.create(:user => User.current,
    4                                :action => 'did something')
    5   end
    6 end

This works well in my project. I’d like to know if there is a less “hacky” way of doing this but I’m not currently aware of one.

{ 0 comments }

The Ruby %r{} expression

by Irish on August 12, 2010

Tonight I was reading over some Ruby code in a gem I’m currently using and came across this line:

    1 string = mode.to_s
    2 string.gsub!(%r{(^.)|(_.)}) { |m| m[m.length-1,1].upcase }
    3 

Two things struck me about that second line. For one I’ve never used gsub and passed it a block, thats cool. And two, what is %r{} ? I’ve never seen that before. It turns out that in Ruby, the /…/ or %r… literals, and using the Regexp.new constructor are all the same thing. All ways of created a regular expression. I still think I prefer the look of the simple slashes /…/ though.

{ 0 comments }

CSS Tricks

by Irish on August 11, 2010

Seeing as I’m not a UX/Designer guy, I’m always learning new css tricks and over coming css/browser gotchas in my day to day development work. My coworker Billy showed me this helpful site:

15 CSS Tricks that must be learned

Thought I’d pass it along, you may learn a thing or two as did I.

{ 0 comments }

How to install Sphinx on OS X

by Irish on August 10, 2010

Getting a local development environment setup to run Sphinx on your Mac is pretty easy. You can boil it down to the following quick commands.

$ wget http://www.sphinxsearch.com/downloads/sphinx-0.9.9.tar.gz
$ tar xvzf sphinx-0.9.9.tar.gz
$ cd sphinx-0.9.9
$ ./configure
$ make
$ sudo make install

This assumes you’re using a default MySQL installation. If however you’re like me and using MySQL installed from macports, you’ll need to pass the location of the MySQL configuration directories to Sphinx. The configure command would then change like so:

$ ./configure
$ ./configure --with-mysql-includes=/opt/local/include/mysql5/mysql/ --with-mysql-libs=/opt/local/lib/mysql5/mysql/

On a side note, Sphinx 1.10-beta has also just recently been released (July 10′). This release has real-time indexes support, a welcome addition I have been waiting for. Definately check it out.

{ 0 comments }

How to setup Textmate to use RVM

by Irish on June 28, 2010

Alright, so you’ve switched to using rvm but when you run cmd+r on a Ruby file in TextMate you’re still using your old system install of Ruby… Luckily it only takes a few easy steps to setup TextMate to use your rvm environment.

1) First make sure your install of TextMate if updated
Textmate -> Preferences -> Software Update -> Check Now

2) Next make sure all your bundles are up to date. The creator of rvm, Waynee Seguin, provides a small bash script to automate this process. You can check it out at this GitHub gist

3) Now get the rvm name of the Ruby version you want to use in TextMate. In my case I’m going to use my currently selected version “ruby-1.8.7-tv1_8_7_174″

$ rvm list
rvm rubies

   jruby-1.5.1 [ i386-java ]
   ruby-1.8.7-tv1_8_7_173 [ i386 ]
=> ruby-1.8.7-tv1_8_7_174 [ i386 ]
   ruby-1.9.1-tv1_9_1_378 [ i386 ]
   ruby-1.9.2-head [ i386 ]

4) Then run the rvm command to wrap this ruby version for TextMate

$ rvm wrapper ruby-1.8.7-tv1_8_7_174 textmate

5) Now set a TM_RUBY variable in your TextMate Preferences
Textmate -> Preferences -> Advanced -> Shell Variables

To the wrapper command generated by rvm for you, in my case it was found here:

/Users/cirish/.rvm/bin/textmate_ruby

6) Since TextMate will use it’s own builder, by removing it, we can use TM_RUBY as described above.

$ cd /Applications/TextMate.app/Contents/SharedSupport/Support/lib/
mv Builder.rb Builder.rb.backup

7) Last but not least, quit TextMate and re-open it to load these settings. You should now be all good :)

{ 1 comment }

OS X “Safe Sleep” Mode

by Irish on June 22, 2010

I seem to continually have the problem of my laptop waking up on its own in my backpack. I’m not sure what exactly is the cause, but I’m gonna assume it’s from the screen not staying completely locked/closed. However, I found this OS X command on Andrew Dupont’s blog, which puts OS X into a “safe sleep” mode. This causes OS X to write memory state to a hibernation image at sleep time. Now for me to awaken my Macbook Pro, opening the lid is not enough. Now it takes a press of the power button, and a few more seconds than before, but no more accidental awakening, or half-dead battery upon arriving at my destination.

$ sudo pmset -a hibernatemode 1

Now just put your computer to sleep like you normally would. I for one prefer to use the quick keys (cmd + option + eject). Check the man page for more options.

$ man pmset

{ 0 comments }

Ruby 1.8.7 openssl Bus Error

by Irish on June 21, 2010

Recently I switched from using my local ruby install to using RVM for managing all my ruby installations. RVM is pretty awesome and I highly recommend it. I went ahead and installed a few versions of ruby that I’d like to use including, ruby 1.8.7 (for work projects) and 1.9.2 (for playing with rails 3).

I came across an issue though with my 1.8.7 install when trying to run a rake db:migrate:reset command with my current work project. It was along the lines of this:

$ rake db:migrate:reset
(in /Users/cirish/Projects/so-ch)
/Users/cirish/.rvm/rubies/ruby-1.8.7-tv1_8_7_173/lib/ruby/1.8/openssl/ssl.rb:31: [BUG] Bus Error
ruby 1.8.7 (2009-06-08 patchlevel 173) [i686-darwin9.8.0]

Abort trap
$

Well that’s no bueno. It turns out that this is happening because, I have two openssl installations. A system one and other that I installed through macports. The reason the bus error is being thrown is that my eventmachine gem wasn’t compiled with the same openssl lib as my ruby 1.8.7 install. We can confirm this by running the following:

$ ruby -rubygems -e" require 'openssl' "; echo $? 0
0 0

$ ruby -rubygems -e" require 'eventmachine'; require 'openssl' "; echo $?
/Users/cirish/.rvm/rubies/ruby-1.8.7-tv1_8_7_173/lib/ruby/1.8/openssl/ssl.rb:31: [BUG] Bus Error
ruby 1.8.7 (2009-06-08 patchlevel 173) [i686-darwin9.8.0]

Abort trap
134

At this point you could either recompile ruby and point it to the correct openssl lib or recompile eventmachine to use the system’s openssl. Since this was a new ruby install anyways, I chose to just recompile ruby.

$ rvm install ruby-1.8.7-tv1_8_7_174 --configure --enable-shared=true,--with-openssl-dir=/opt/local --debug

Now let’s check the install again

$ ruby -rubygems -e" require 'eventmachine'; require 'openssl' "; echo $?
0

Fixed :-)

{ 0 comments }

This is more of a reminder to myself than anything. To install the mysql gem on OS X Leopard and use a macports MySQL install.

First remove any previously installed mysql gem

$ gem uninstall mysql

Then install, giving the correct Leopard archflag and macports mysql_config directory

$ env ARCHFLAGS="-arch i386" gem install mysql -- --with-mysql-config=/opt/local/lib/mysql5/bin/mysql_config

{ 0 comments }

Installing RMagick on OS X has at times been really smooth for me, and other times, a royal pain in the ass. The issues usually come from having to get ImageMagick installed, but I’ve also had problems with the RMagick gem not being able to build the native extensions. I’ve successfully installed ImageMagick by using MacPorts and by compiling the sources by hand. Hopefully, some of these tips will help you with your installation if a problem occurs.

Tip #1: Keep MacPorts and your installed packages up to date!

MacPorts allows for easy installation of packages. But beware, MacPort’s packages are a bit of a dependency nightmare at times. If your installed packages are old and you use them to compile new packages, things can get messed up in the linking/compiling process, or simply just be completely incompatible. Be sure to run these commands often and always before you trying installing/upgrading anything.

$ sudo port selfupdate
$ sudo port upgrade outdated

Tip #2: MacPorts isn’t your only option

There are other’s out there that have felt the install pain. I came across this installer script that’s well maintained and as of this writing has been updated to work with ImageMagick-6.6.1-5

http://github.com/maddox/magick-installer/blob/master/magick-installer.sh

Simply grab it with Git

$ git clone http://github.com/maddox/magick-installer.git
$ cd magick-installer
$ ./magick-installer.sh

I’ve also heard good things about the OS X homebrew package management system, but have no personal experience in using it. Check it out here, and give it a go if it sounds up your alley.

http://mxcl.github.com/homebrew/

Tip #3: Ask for help

Check the Installation FAQS
They have solutions for some of the more common issues that tend to happen
http://rmagick.rubyforge.org/install-faq.html#loaderror

Search Online
Take any error messages you receive and try googling for answers to them. You can usually find blog posts from other people that have run into the same issues as you. Hit up the mailing lists or you can even send an email to the project maintainers at rmagick@rubyforge.org

Tip #4: Use the verbose flag when building the gem

This will show you what’s going on as far as the gem building making external requests for libs, what’s building/being written, and that can give you more details than just what the simple stacktrace will show you, when things go wrong.

$ sudo gem install rmagick -V

Tip #5: Be sure to test it after successful install

As noted on the RMagick site, the “Successfully installed” message does not mean that RMagick was successfully installed. The RMagick installation can encounter error conditions that gem can’t detect. The following irb session is a better indicator of a successful install.

$ irb -rubygems -r RMagick
irb(main):001:0> puts Magick::Long_version
This is RMagick 2.13.1 Copyright (C) 2009 by Timothy P. Hunter
Built with ImageMagick 6.6.1-5 2010-05-11 Q8 http://www.imagemagick.org
Built for ruby 1.8.7
Web page: http://rmagick.rubyforge.org
Email: rmagick@rubyforge.org
=> nil

{ 1 comment }