Ruby

How to install RVM on Ubuntu 10.04

by Irish on August 25, 2010

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. 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. And I’m assuming you’ve already setup SSH.

$ ssh root@173.XXX.XXX.XXX
<-- Output Snipped -->
root@173.XXX.XXX.XXX'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 -s stable < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer )

<-- 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 doesn’t work you may have to source your .bashrc file to have it reloaded by the current terminal session

$ source ~/.bashrc

After running `rvm notes` if it 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.

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

Need web application development, maintenance for your existing app, or a third party code review?

Velocity Labs can help.

Click here to get started!

{ 4 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 :-)

{ 1 comment }