Ruby Intro

SENG2021

Installing Ruby

When I was first starting out in Ruby, this is what gave me the biggest problems. One can install from their operating system’s package manager easily enough (likely be installed already with the system), but because of how quickly the Ruby community moves, using system Ruby is generally not a good idea. Every now and then, you’ll come across version incompatibility issues and if you need to work on different apps with different versions of Ruby, then you’re in a bit of a mess. Not to mention the permission issues.

To get around this, we use a Ruby version manager – one named simply rvm, and another, rbenv.

I will be going through installing Ruby with rbenv on Bash with you simply because I’ve personally had fewer issues with it integrating with our Bamboo continuous integration environment, but that’s a story for another day. You’ll no doubt come across problems that I didn’t foresee. If it tells you of a missing dependency, just install it. Setting up Ruby was a great exercise in google-fu for me; don’t be discouraged if you end up having to reinstall several times.

Please read the OSX comments even if you don’t have OSX!

OSX

If you have a Mac, you’re in luck – you have Homebrew. If you don’t, just install it with ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)"

1
2
brew update
brew install rbenv

Unfortunately, a plugin is required to install Ruby with rbenv

1
brew install ruby-build

‘Turn on’ rbenv

1
if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi

Set up bash to always enable rbenv if available

1
echo 'if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi' >> ~/.profile

Add rbenv’s executables to your PATH

1
2
3
# My rbenv is installed in /usr/loca/opt/rbenv, but yours may be in ~/.rbenv
# Change the line below appropriately
echo 'export PATH=/usr/local/opt/rbenv/shims:$PATH'

Finally install Ruby 2.0.0-p247

1
2
3
4
rbenv install 2.0.0-p247
# Command below refreshes rbenv's executables.
# Make sure you run this every time you (un)install a ruby or a gem - more on this later
rbenv rehash

Debian/Ubuntu/Linux Mint

For fun times, install these dependencies

1
apt-get install build-essential openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev automake libtool bison subversion pkg-config nodejs

Install rbenv + ruby-build

1
2
3
4
5
6
7
8
git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
exec $SHELL -l
mkdir -p ~/.rbenv/plugins
git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
rbenv install 2.0.0-p247
rbenv rehash

Windows

Ruby does work on Windows, but I don’t have any experience with it and I’m not familiar with the toolset for developing on Windows so these posts don’t really work on Windows.

I haven’t tried any of this but apparently…

Download and install the relevant 2.0.0-p247 installer from http://rubyinstaller.org/downloads/, but make note of where you install it. Press Windows+R, and type in cmd. Then cd to the directory where you installed Ruby and everything else should be the same.

If you’re using Windows, open fxri from the Ruby section of your Start Menu.

Other than that, you’re on your own :(

Keeping legacy code consistent

You may have Ruby scripts before installing rbenv that ran with system Ruby, so it’s best to keep it that way. To ensure that this happens, we can make system ruby the default for rbenv.

1
2
3
rbenv global system
rbenv version
# system (set by /usr/local/opt/rbenv/version)

You can obviously set the global default to 2.0.0-p247 if you wish too.

Make rbenv use a specific version

Whenever you interact with rbenv, it will look in your current directory for a file named .rbenv-version before falling back to the global default. Try the following:

1
2
3
4
5
6
cd /tmp
rbenv version
# system (set by /usr/local/opt/rbenv/version)
echo '2.0.0-p247' > .rbenv-version
rbenv version
# 2.0.0-p247 (set by /tmp/.rbenv-version)

s

The best thing about setting versions with the file is that you can commit this file to your code repository, and it will ensure that every developer on every computer using rbenv will be using the version of Ruby that is intended (unless they really don’t want to).

  1. rvm
  2. rbenv
  3. Wikipedia on Continuous Integration
  4. Atlassian Bamboo
  5. Homebrew
  6. Installing rbenv
  7. ruby-build rbenv plugin