Build Awesome Command-Line Applications in Ruby 2

Control Your Computer, Simplify Your Life

Setting up Your Machine

I’d meant to include this with the book, but I couldn’t get things working right on Windows. If you’ve been able to, please post about and I’ll try to replicate and include the instructions here. For those of you on Mac OS or Linux, read on!

MacOS & Linux

It’s likely that your system came installed with a version of Ruby. It’s also likely that it came with a very old version of Ruby. For example, Mac OS X Lion, the latest operating system produced by Apple at the time of this writing, ships with a version of Ruby 1.8.7, built in January of 2010.

While we could install a more recent version, there’s usually a good reason why operating system vendors install specific versions, and updating the system’s version of Ruby could cause unforseen problems. Fortunately, the Ruby Version Manager (RVM for short) is a tool that can fix this. RVM will allow us to setup a completely isolated Ruby environment, outside of the system’s Ruby, with complete control over the gems we install. In fact, we can set up completely different sets of gems, and switch between them.

We’ll walk you through installing RVM, and creating a gemset for the purposes of running some example code that will verify that your environment is working. These will be very explicit instructions, but you should check the RVM website for the latest RVM installation instructions and documentation.

Installing General Development Tools

Before setting up RVM, you’ll need to make sure you have appropriate developer tools installed. It’s likely you have these installed already, especially if you are using Linux. You can do a quick check via:

Find out if GCC is installed
1
2
$ type gcc
gcc is /usr/bin/gcc

It doesn’t matter where gcc (the open-source Gnu C Compiler) is installed, just so long as it is. If it’s not, and you are running Linux, consult your distribution’s instructions for installing development tools. If you are running Mac OS X, you’ll need to install XCode, which should’ve been included with your computer, but is also available as a free download from the Mac App Store.

Installing RVM

Now that you have development tools installed, we need to install RVM, which is done by executing a remote bash script.

Installing RVM
1
$ bash < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)

If you are concerned about blindly executing code from the Internet, you can examine the installation script via

Examining the RVM install script
1
$ curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer | less

When RVM installs, it will generate a large amount of output. At the very end will be some instructions for configuring RVM in your environment. It should be a command like so:

Configuring your .bashrc for RVM
1
$ echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function' >> ~/.bash_profile

This concatenates some code into your .bash_profile. If you aren’t using bash as your prompt, or don’t configure it in .bash_profile, you’ll need to replace the >> ~/.bash_profile with the correct file name.

Once you’ve done this, you need to reload your environment. It’s simplest to exit your current shell and open up a new one. You can check that things are working via:

Verifying that RVM was installed
1
2
$ type rvm | head -1
rvm is a function

If you do not see the output above, something’s gone wrong and you’ll need to retrace your steps. RVM’s installation instructions will help, and there is additional information on the website for troubleshooting. Next, you’ll need to install a version of Ruby.

Installing Ruby

Now that RVM is installed, we can use it to install any version of Ruby in a way that will not affect our system’s Ruby install. At the time of this writing the latest version of Ruby is 1.9.3, so let’s use that:

Installing Ruby 1.9.3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$ rvm install 1.9.3
Installing Ruby from source to: /Users/davec/.rvm/rubies/ruby-1.9.3-p0, this may take a while depending on your cpu(s)...

ruby-1.9.3-p0 - #fetching 
ruby-1.9.3-p0 - #extracted to /Users/davec/.rvm/src/ruby-1.9.3-p0 (already extracted)
Fetching yaml-0.1.4.tar.gz to /Users/davec/.rvm/archives
Extracting yaml-0.1.4.tar.gz to /Users/davec/.rvm/src
Configuring yaml in /Users/davec/.rvm/src/yaml-0.1.4.
Compiling yaml in /Users/davec/.rvm/src/yaml-0.1.4.
Installing yaml to /Users/davec/.rvm/usr
ruby-1.9.3-p0 - #configuring 
ruby-1.9.3-p0 - #compiling 
ruby-1.9.3-p0 - #installing 
Removing old Rubygems files...
Installing rubygems-1.8.10 for ruby-1.9.3-p0 ...
Installation of rubygems completed successfully.
ruby-1.9.3-p0 - adjusting #shebangs for (gem irb erb ri rdoc testrb rake).
ruby-1.9.3-p0 - #importing default gemsets (/Users/davec/.rvm/gemsets/)
Install of ruby-1.9.3-p0 - #complete 

The output might not exactly match, but you should see something along these lines. It may take a while, since it first downloads the source code for Ruby 1.9.3, and then builds it (this is why we needed a C compiler installed in the first step). To check that things are working:

Using Ruby 1.9.2
1
2
3
$ rvm use 1.9.3
$ ruby --version
ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-darwin11.2.0]

Again, the string might not match exactly, but you should see “1.9.3” in the version string. We’re almost ready. The last thing we need to do is learn about using Gemsets. Since we are using several open-source gems throughout the book, it’s handy to be able to install them in a “sandbox” that allows us to play around with them, but without installing them in our main, global set of gems.

Gemsets allow us to do that. To show an example, do the following:

Creating and Using a Gemset
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
$ rvm use 1.9.3
$ gem list
*** LOCAL GEMS ***

rake (0.8.7 ruby)
$ rvm gemset create testing
'testing' gemset created (/Users/davec/.rvm/gems/ruby-1.9.3-p0@testing).
$ rvm gemset use testing
$ gem install gli
Fetching: gli-1.3.6.gem (100%)
Successfully installed gli-1.3.6
1 gem installed
Installing ri documentation for gli-1.3.6...
Installing RDoc documentation for gli-1.3.6...
$ gem list
*** LOCAL GEMS ***

gli (1.3.6)
rake (0.8.7)
$ gli help
gli allows you to create the scaffolding for a GLI-powered application

usage: gli [global options] command [command options]

Version: 1.3.6

Global Options:
    -n             - Dry run; dont change the disk
    -r, --root=arg - Root dir of project (default: .)
    -v             - Be verbose

Commands:
    help           - Shows list of commands or help for one command
    init, scaffold - Create a new GLI-based project
$ rvm gemset create testing2
'testing2' gemset created (/Users/davec/.rvm/gems/ruby-1.9.3-p0@testing2).
$ rvm gemset use testing2
$ gem list

*** LOCAL GEMS ***

rake (0.8.7)
$ gli help
-bash: gli: command not found

As you can see, we created two gemsets, named testing and testing2 and used rvm gemset use to switch between them. In doing so, gem only sees the gems listed in the current gemset. Further, we can see that by installing gli in one gemset and not the other, RVM also manipulates our PATH so that the gli executable is available only in the gemset where gli is installed.

I would recommend that you use a gemset for following along with the examples in the book. By doing this, if you get into a weird situation where your environment isn’t working, you can clear out your gemset with rvm gemset delete and start fresh.

Windows

As mentioned, I was unable to get a good Windows install working. At the very least you will need to use the Ruby Installer to get Ruby on your system.

The specific troubles I had were around installing native gems using the DevKit. If you can get everything installed on a Windows 7 box, and execute the examples, please share it. Note that, at this time, Aruba doesn’t work with Windows, however everything else should.