13 May 2013

Installing ruby 2 + Rails 4 on Ubuntu 12.04 LTS

Installing Ruby 2 + Rails 4 on Ubuntu 12.04 LTS

Update Dec 2013: You may also wish to read benkwok's blog post on installing ruby and rails. I've also posted my notes from installing for an existing project which doesn't entirely replace this post but reflects my more recent learnings.

There's a few of these blog posts around, but here's mine for my own benefit (I'm sure this won't be the last time I do it!).

If you have a packaged ruby / rails / rvm / rbenv etc installed, get rid of them all, eg:

$ ruby --version
ruby 1.8.7 (2011-06-30 patchlevel 352) [x86_64-linux]
$ sudo apt-get remove ruby

Don't use rvm; and make sure it's been literally purged from your system. It's a pain to remove as it gets into all sorts of places and even apt-get purge doesn't undo changes to the profile etc. If you want to know more about the reason for not using it then read the rbenv "why" page, it's persuasive stuff.

My recommendation from experience so far is to use rbenv to install the latest and greatest RoR (Ruby on Rails). Don't bother with the ubuntu packaged version of rbenv (from apt etc) as you'll be off the beaten track and will have to figure out the ruby-build plugin installation yourself. The local user install is painless and works well. The instructions say to make sure rvm is removed first as it's incompatible.

rbenv installation

Install rbenv into your home directory:
$ git clone git://github.com/sstephenson/rbenv.git ~/.rbenv

Set up the environment as per the (ubuntu specific) rbenv installation instructions:

$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.profile
$ echo 'eval "$(rbenv init -)"' >> ~/.profile

Unless you've done anything before, there is no ~/.profile file before hand, so the contents will then be:

$ cat ~/.profile
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"

Restart the login shell:

$ exec $SHELL -l

Check rbenv is now available:

$ rbenv
rbenv 0.4.0-45-g060f141
Usage: rbenv <command> [<args>]
...

Set up the ruby-build plugin (as linked in the rbenv readme)

$ git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build

Install the necessary ssl library:

$ sudo apt-get install libssl-dev

If you don't install the openssl development libraries you get this:

BUILD FAILED
...
The Ruby openssl extension was not compiled. Missing the OpenSSL lib?




Ruby installation

Install the latest ruby (version name obtained from release info on ruby blog), takes 5-10 mins

$ rbenv install 2.0.0-p0

Now select the installed ruby as the default for your user (ref: https://github.com/sstephenson/rbenv#choosing-the-ruby-version)

$ rbenv global 2.0.0-p0 
tim@atom:~$ ruby --version
ruby 2.0.0p0 (2013-02-24 revision 39474) [x86_64-linux]


Rails installation

Now as per the Rails 4 RC1 announcement install the release candidate of Rails 4 (this was the latest at time of writing). Takes 5-10 mins.
$ gem install rails --version 4.0.0.rc1 --no-ri --no-rdoc

Tell rbenv to create the new shims and see the installed rails:
$ rbenv rehash
$ rails --version
Rails 4.0.0.rc1



All done! That wasn't so hard, it was all the blind alleys that took the time.

Now use bundler as recommended in the rbenv readme to set up an app etc.

Thanks for listening :-)

Footnote: It pains me somewhat to have to use installations outside of the Ubuntu package manager, however it seems there are some grumblings about the packaged versions of the above software. Add into this that I wish to use the latest RoR on an LTS release of Ubuntu which seeing as the Rails community don't seem to provide debs / repos etc leaves a packaged version out of the question for now. I've learned previously the hard way the destructive effect of randomly installing everything you find outside the package management system of a distro so have tread carefully when creating the above information.

Further reading http://benkwok.wordpress.com/2013/08/15/install-rails-on-ubuntu-12-04-lts-with-rbenv/

3 comments:

Jason Cain said...

Thanks for a great guide!

benkwok said...

Thank you Tim. I borrowed your steps of gem install rails in my post, which you can check at: http://benkwok.wordpress.com/2013/08/15/install-rails-on-ubuntu-12-04-lts-with-rbenv/

Tim Abell said...

@benkwok - You're welcome, glad it helped you. Thanks for linking back.