Mischa Taylor's Coding Blog

On the edge of time and reason

Use Opscode Chef Omnibus Ruby for Writing Cookbooks

If your only use of Ruby is because you want to write cookbooks for Opscode Chef, rather than going through the bother of setting up a full Ruby development environment via rvm or rbenv you can just reuse the Ruby 1.9.x environment that is bundled with the Opscode Omnibus Chef installer.

In this post, I’ll cover how to configure the Ruby 1.9.x interpreter bundled with the Opscode Omnibus Chef installer on Mac OS X, Linux and Windows so it can be used for writing cookbooks.

How to set up Omnibus Chef Ruby on Mac OS X

First install Apple Xcode, which includes a C compiler needed to build the tools required for cookbook development from source (like Berkshelf ). Download and install the latest version of Xcode from the App Store, if you don’t have it installed already. Also make sure you install the Command Line Tools by choosing the menu item Xcode -> Preferences... and click on the Downloads tab. Click on the Install button to download the Command Line Tools. Xcode Command Line Tools

In a web browser, go to the http://www.opscode.com/chef/install page to display the instructions for installing the Chef Client via the Opscode Omnibus Chef installer.

As of this writing, the quick install instructions for Mac OS X are as follows:

$ curl -L https://www.opscode.com/chef/install.sh | sudo bash

The Chef Client installer also installs Ruby 1.9.3 for its own use in the directory /opt/chef/embedded. You can also use this copy of Ruby for your own cookbook development.

WARNING: Don’t try to mix and match the Chef Client’s Ruby 1.9.3 together with a RVM/rbenv Ruby development setup. Choose one or the other. If your Ruby needs go beyond Chef and writing Chef cookbooks, set up a “real” RVM/rbenv Ruby environment.

Now that you have a Ruby 1.9.3 environment via the Chef Client install, you can install any extra gem dependencies needed for writing Chef cookbooks by using the /opt/chef/embedded/bin/gem install command. For example, here’s how to install Berkshelf, a popular cookbook authoring support tool:

$ sudo /opt/chef/embedded/bin/gem install berkshelf --no-ri --no-rdoc
Password:
Fetching: i18n-0.6.1.gem (100%)
Fetching: multi_json-1.7.7.gem (100%)
Fetching: activesupport-3.2.13.gem (100%)
...
Successfully installed safe_yaml-0.9.3
Successfully installed test-kitchen-1.0.0.alpha.7
Successfully installed berkshelf-2.0.3
43 gems installed

Create a soft link to any gem-installed binaries in an existing PATH directory, like /usr/local/bin

# On Mac OS X, /usr/local/bin is not created by default
$ sudo mkdir -p /usr/local/bin
Password:
$ sudo ln -s /opt/chef/embedded/bin/berks /usr/local/bin/berks
$ berks -v
Berkshelf (2.0.3)

Don’t be tempted to add /opt/chef/embedded/bin to your PATH. You still want to keep Opscode’s Ruby install separate from your main system Ruby install.

How to set up Omnibus Chef Ruby on Linux

Make sure all the prerequisite packages are installed for the gems you will be using.

Ubuntu prerequisites:

$ sudo apt-get update
$ sudo apt-get install -y curl
$ sudo apt-get install -y build-essential git
$ sudo apt-get install -y libxml2-dev libxslt-dev libssl-dev

RHEL/CentOS prerequisites:

$ sudo yum update
$ sudo yum install -y curl
$ sudo yum install -y git
$ sudo yum install -y gcc-c++ patch readline readline-devel zlib zlib-devel
$ sudo yum install -y libyaml-devel libffi-devel openssl-devel make bzip2
$ sudo yum install -y autoconf automake libtool bison
$ sudo yum install -y libxml2-devel libxslt-devel

In a web browser, go to the http://www.opscode.com/chef/install page to display the instructions for installing the Chef Client via the Opscode Omnibus Chef installer for your distribution of Linux.

As of this writing, the quick install instructions for Ubuntu/CentOS are as follows:

$ curl -L https://www.opscode.com/chef/install.sh | sudo bash

The Chef Client installer also installs Ruby 1.9.3 for its own use in the directory /opt/chef/embedded. You can also use this copy of Ruby for your own cookbook development.

WARNING: Don’t try to mix and match the Chef Client’s Ruby 1.9.3 together with a RVM/rbenv Ruby development setup. Choose one or the other. If your Ruby needs go beyond Chef and writing Chef cookbooks, set up a “real” RVM/rbenv Ruby environment.

Now that you have a Ruby 1.9.3 environment via the Chef Client install, you can install any extra gem dependencies needed for writing Chef cookbooks by using the /opt/chef/embedded/bin/gem install command. For example, here’s how to install Berkshelf, a popular cookbook authoring support tool:

$ sudo /opt/chef/embedded/bin/gem install berkshelf --no-ri --no-rdoc
Password:
Building native extensions.  This could take a while...
Fetching: httpclient-2.2.0.2.gem (100%)
Fetching: rubyntlm-0.1.1.gem (100%)
Fetching: uuidtools-2.1.4.gem (100%)
...
Successfully installed safe_yaml-0.9.3
Successfully installed test-kitchen-1.0.0.alpha.7
Successfully installed berkshelf-2.0.3
25 gems installed

Create a soft link to any gem-installed binaries in an existing PATH directory, like /usr/local/bin

$ sudo ln -s /opt/chef/embedded/bin/berks /usr/local/bin/berks
$ berks -v
Berkshelf (2.0.3)

Don’t be tempted to add /opt/chef/embedded/bin to your PATH. You still want to keep Opscode’s Ruby install separate from your main system Ruby install.

How to set up Omnibus Chef Ruby on Windows

In a web browser, go to the http://www.opscode.com/chef/install page to display the instructions for installing the Chef Client via the Opscode Omnibus Chef installer for your distribution of Linux.

After you select a Chef verison (pick the latest), you will be provided a download link to the Omnibus Chef Windows installer. After downloading Run the install, choosing the default options.

The Chef Client installer also installs Ruby 1.9.3 for its own use in the directory C:\opscode\chef\embedded. You can also use this copy of Ruby for your own cookbook development.

WARNING: Don’t try to mix and match the Chef Client’s Ruby 1.9.3 together with a RVM/rbenv Ruby development setup. Choose one or the other. If your Ruby needs go beyond Chef and writing Chef cookbooks, set up a “real” RVM/rbenv Ruby environment.

Now that you have a Ruby 1.9.3 environment via the Chef Client install, you can install any extra gem dependencies needed for writing Chef cookbooks by using the c:\opscode\chef\embedded\bin install command. For example, here’s how to install Berkshelf, a popular cookbook authoring support tool:

> c:\opscode\chef\embedded\bin\gem install berkshelf --no-ri --no-rdoc
Fetching: i18n-0.6.1.gem (100%)
Fetching: multi_json-1.7.7.gem (100%)
Fetching: activesupport-3.2.13.gem (100%)
...
Successfully installed safe_yaml-0.9.3
Successfully installed test-kitchen-1.0.0.alpha.7
Successfully installed berkshelf-2.0.3
43 gems installed

Add c:\opscode\chef\embedded\bin to your PATH environment variable: Environments Control Panel

Restart your Command Prompt to pick up the new environment variable setting, and then you can run Berkshelf:

> berks -v
Berkshelf (2.0.3)

Using RVM to Manage Multiple Versions of Ruby

Out of the box, Ruby does not provide a mechanism to support multiple installed versions. Compounding this issue, the default system-installed version of Ruby for most versions of Linux/Mac OS X tend to be quite old. For example, even in the latest version of Mac OS X Mountain Lion, the system-wide version of Ruby is over five years old (ruby 1.8.7). Most developers prefer to use a newer version of Ruby installed in their home directory and to leave the default systemwide version of Ruby untouched.

Ruby Version Manager (RVM) makes installing multiple versions of Ruby easy. It’s a great way to use Ruby 1.9.x for your current development while also being able to play with Ruby 2.0 for newer projects. RVM also supports partitioning of Ruby gem installs via a feature called gemsets in order to avoid versioning conflicts. However, with the advent of Bundler most developers tend to prefer using Bundler as the preferred way for managing gems at the application level.

RVM is supported on Linux and Mac OS X. RVM doesn’t work on Windows. For Windows, Pik is an RVM alternative.

Mac OS X

Install RVM and Ruby 1.9.x - Mac OS X

First, you’ll need to install a C compiler to build Ruby from source. Just download and install the latest version of Xcode from the App Store, if you don’t have it already. Also make sure you install the Command Line Tools by choosing the menu item Xcode -> Preferences... and click on the Downloads tab. Click on the Install button to download the Command Line Tools. Xcode Command Line Tools

Next, you’ll need to install the Homebrew package manager. RVM will automatically download all dependencies if a package manager is installed. Also, the dependencies for installing Ruby are complicated enough that it is helpful to use a package manager to install them, so you can uninstall them easily if necessary. Run the following command to install Homebrew:

ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)"

Run brew doctor and address any issues it discovers. When all is well, you should see:

$ brew doctor
Your system is raring to brew.

After Xcode and Homebrew are installed, you can install RVM with the following commands:

$ \curl -L https://get.rvm.io | bash -s stable
$ source $HOME/.rvm/scripts/rvm

Run rvm requirements to install additional dependencies:

$ rvm requirements
Installing requirements for osx, might require sudo password.
Already up-to-date.
Cloning into '/usr/local/Library/Taps/homebrew-dupes'...
remote: Counting objects: 906, done.
remote: Compressing objects: 100% (495/495), done.
remote: Total 906 (delta 498), reused 803 (delta 411)
Receiving objects: 100% (906/906), 157.67 KiB | 112 KiB/s, done.
Resolving deltas: 100% (498/498), done.
Tapped 41 formula
Installing required packages: autoconf, automake, libtool, pkg-config, apple-gcc42, libyaml, readline, libxml2, libxslt, libksba, openssl...........................................................................................................................................................
Updating certificates in '/usr/local/etc/openssl/cert.pem'.

Tell rvm to automatically install dependencies via Homebrew with the following command:

$ rvm autolibs osx_brew

Next, build and install the latest version of Ruby by running the following (this will take a long time):

$ rvm install 1.9.3

You may get an error message saying “There was an error while trying to resolve rubygems version for ‘latest’. Halting the installation.” Just run the install again like so to fix the issue:

$ rvm reinstall 1.9.3

Verify the RVM install by running the following commands:

$ rvm -h
$ rvm list
$ rvm use 1.9.3
$ rvm rubygems latest

To ensure that the newer Ruby 1.9.3 is used by default instead of the system 1.8.7 version, run the following command:

$ rvm use 1.9.3 --default

If you’d like to manage RVM with a GUI, check out JewelryBox:

Jewelry Box

Install Ruby 1.8.7 - Mac OS X

For legacy GUI support, Ruby 1.8.7 has some dependencies on tcl/tk, which Mountain Lion no longer installs by default (now that X11 is an optional install). To compile Ruby 1.8.7 without tcl/tk support, use the following command overrides on rvm install:

$ rvm install 1.8.7 --with-gcc=clang --without-tcl --without-tk

To compile Ruby 1.8.7 with tcl/tk support, install X11 via http://xquartz.macosforge.org/landing then compile Ruby 1.8.7 with the following:

$ export CPPFLAGS=-I/opt/X11/include
$ CC=/usr/local/bin/gcc-4.2 rvm install 1.8.7

Install Ruby 2.0.0 - Mac OS X

Installing Ruby 2.0 is very straightforward, just run the following:

$ rvm install 2.0.0

Verify the Ruby 2.0 install by running the following commands:

$ rvm use 2.0.0
$ ruby -v
ruby 2.0.0p195 (2013-05-14 revision 40734) [x86_64-darwin12.3.0]

If you want to make Ruby 2.0 your default version of ruby, run the following:

$ rvm use 2.0.0 --default

Remove RVM - Mac OS X

Should you want to uninstall/remove RVM, it’s pretty easy. First, run the following commands:

$ rvm implode
$ gem uninstall rvm

Then just follow the instructions from rvm implode:

  • Delete the following files, if they exist: /etc/rvmrc $HOME/.rvmrc

  • Also, remove the lines sourcing RVM scripts from $HOME/.bash_profile /etc/zprofile and /etc/profile.d/rvm.sh if they exist. (A reboot doesn’t hurt after you do this, just to make sure).

To uninstall Homebrew, run the following:

cd `brew --prefix`
brew install libtool
rm -rf Cellar
rm `git ls-files`
rm -r Library/Homebrew Library/Aliases Library/Formula Library/Contributions
rm -rf .git
rm -rf ~/Library/Caches/Homebrew

Linux

Install RVM and Ruby 1.9.x - Linux

RVM installation is more straightforward on Linux, as the Ruby source was designed for the GCC compiler that ships with any Linux distribution.

First install curl, so that you can fetch the RVM script. For Ubuntu, run the following command:

$ sudo apt-get update
$ sudo apt-get install -y curl

For RedHat/CentOS:

$ sudo yum update
$ sudo yum install -y curl

With curl installed, run the RVM install with the following commands:

$ \curl -L https://get.rvm.io | bash -s stable
$ source $HOME/.rvm/scripts/rvm

On Ubuntu, run rvm requirements to install additional dependencies:

$ rvm requirements

For CentOS, rvm requirements will need to download packages from EPEL, so add the --verify-downloads 1 parameter after the rvm requirements command:

$ rvm requirements --verify-downloads 1

Next build and install Ruby 1.9.3 (this will take awhile):

$ rvm install 1.9.3
$ rvm use 1.9.3
$ rvm rubygems latest

Verify rvm install:

$ rvm -h
$ rvm list
$ rvm use 1.9.3
$ ruby -v
ruby 1.9.3p429 (2013-05-15 revision 40747) [x86_64-linux]

Install Ruby 2.0.0 - Linux

Installing Ruby 2.0 is very straightforward, just run the following:

$ rvm install 2.0.0

Verify the Ruby 2.0 install by running the following commands:

$ rvm use 2.0.0
$ ruby -v
ruby 2.0.0p195 (2013-05-14 revision 40734) [x86_64-linux]

If you want to make Ruby 2.0 your default version of ruby, run the following:

$ rvm use 2.0.0 --default

Remove RVM - Linux

Should you want to uninstall/remove RVM, it’s pretty easy. First, run the following commands:

$ rvm implode
$ gem uninstall rvm

Then just follow the instructions from rvm implode:

  • Delete the following files, if they exist: /etc/rvmrc $HOME/.rvmrc

  • Also, remove the lines sourcing RVM scripts from $HOME/.bash_profile /etc/zprofile and /etc/profile.d/rvm.sh if they exist. (A reboot doesn’t hurt after you do this, just to make sure).

Using Rbenv to Manage Multiple Versions of Ruby

Updated December 15th, 2013:

  • Updated install link for homebrew, it has changed
  • Updated Xcode install instructions for Mac OS X Mavericks
  • Bumped ruby 1.9.3 version from 1.9.3p448 to 1.9.3p484
  • Bumped ruby 2.0.0 version from 2.0.0p247 to 2.0.0p353

Updated July 9th, 2013:

  • Bumped ruby 1.9.3 version from 1.9.3p429 to 1.9.3p448
  • Bumped ruby 2.0.0 version from 2.0.0p195 to 2.0.0p247
  • Removed openssl workaround for compiling ruby 2.0.0 on Mac OS X - this has been fixed

Out of the box, Ruby does not provide a mechanism to support multiple installed versions. Rbenv makes managing multiple versions of Ruby easy. It’s a great way to work on current development projects using Ruby 1.9.x and be able to switch to Ruby 2.0.x for new work. Rbenv is a lightweight alternative to Ruby Version Manager (RVM). Rbenv does not include a way to manage gems, like with RVM, though most people prefer to use Bundler gem management instead.

Rbenv is supported on Linux and Mac OS X. Rbenv doesn’t work on Windows (and isn’t really necessary on Windows as there is no system Ruby on Windows). On Windows, the RubyInstaller and/or Pik are both good alternatives to work with multiple versions of Ruby on the same box.

NOTE: Rbenv is incompatible with RVM because RVM overrides the gem command with a function. If you want to switch to Rbenv, make sure you uninstall RVM first. Run the following commands to uninstall RVM:

$ rvm implode
$ gem uninstall rvm

Then remove/comment out the RVM loader line in .bash_profile and/or .bashrc

Mac OS X

Install Rbenv and Ruby 1.9.x - Mac OS X

First you’ll need to install a C compiler and some other command line tools to build Ruby from source. You’ll need to install the Xcode Command Line Tools:

Mac OS X Mavericks 10.9

If you are using the latest version of Mac OS X Mavericks, it has support for downloading the Xcode command line tools directly from a Terminal window. Run the following on a command line:

$ xcode-select --install

You will be prompted to either click on Install to just install the command line developer tools or click on Get Xcode to install both Xcode and the command line developer tools. It can be handy to have Xcode as well, but it is a rather large multi-gigabyte download and not really necessary for Ruby development. So if you want to get going quickly, just click on the Install button:

xcode-select

Mac OS X Snow Leopard 10.8 (and earlier)

Versions of Mac OS X prior to Mavericks, like Snow Leopard do not have built-in support for downloading the Xcode command line developer tools, but you can download them directly from the Apple Developer web site developer.apple.com/downloads. Here are the current direct download links for the Xcode command line tools as of this writing. Download the applicable package for your version of Mac OS X:

Once you have downloaded the .dmg file, double-click on the .dmg to open it, then double-click on the .mpkg file to run the Command Line Tools installer:

xcode-select

If you have time to install Xcode, it’s handy to have it around, though not really required for Ruby development. If you are running Mac OS X Mountain Lion 10.8, you will need to make sure that you have OS X version 10.8.4 or later to install Xcode, otherwise the App Store will issue the following complaint:

xcode-select

Download and install the latest version of Xcode from the App Store. Then install the Command Line Tools by choosing the menu item Xcode -> Preferences... to display the Downloads dialog. Click on the Downloads tab, then click on the download arrow to the right of the Command Line Tools component to install.

Xcode Command Line Tools

Next, you’ll need to install the Homebrew package manager to get all the dependencies needed to compile Ruby from source. While you could manage these dependencies by hand, using a package manager is a better idea, as package managers know how to uninstall what they install.

Run the following command to install Homebrew:

$ ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"

Run brew doctor and address any issues it discovers. When all is well, you should see:

$ brew doctor
Your system is raring to brew.

Next, install the additional dependencies to compile Ruby from source:

# For update-system
brew update
brew tap homebrew/dupes
# For ruby
brew install apple-gcc42

And now install rbenv and the ruby-build plugin:

brew update
brew install rbenv
brew install ruby-build

Add rbenv init to your shell to enable shims and autocompletion:

$ echo 'eval "$(rbenv init -)"' >> $HOME/.bash_profile
$ source ~/.bash_profile

Restart shell as a login shell so that the PATH changes take effect:

$ exec $SHELL -l

Install the latest version of ruby 1.9.x (at the time of this writing 1.9.3-p484)

$ rbenv install 1.9.3-p484

Rebuild the shim executable

$ rbenv rehash

You’ll need to run rbenv rehash every time you install a new version of Ruby or install a new gem.

Set the latest version of ruby to be the default version of ruby

$ rbenv global 1.9.3-p484

Verify the ruby install. If everything was installed correctly, the ruby -v command should report that version 1.9.3p484 is installed.

$ ruby -v
ruby 1.9.3p484 (2013-11-22 revision 43786) [x86_64-darwin13.0.0]

Install Bundler - Mac OS X

You’ll need to use Bundler to manage gems. Installing a gem is also a good way to ensure that you’ve installed most of the Ruby prerequisites.

First, make sure you update to the latest version of Rubygems:

$ gem update --system
Updating rubygems-update
Fetching: rubygems-update-2.1.11.gem (100%)
Successfully installed rubygems-update-2.1.11
Installing RubyGems 2.1.11
RubyGems 2.1.11 installed
...

Then install the bundler gem. If the gem install command reports Successfully installed you’re good to go:

$ gem install bundler
Successfully installed bundler-1.3.5
Parsing documentation for bundler-1.3.5
1 gem installed
$ rbenv rehash

Install Ruby 2.0.0 - Mac OS X

As of this writing, Ruby 2.0.0-p353 is the latest version of Ruby 2.0.0. Use rbenv install --list to print out the available versions. To install:

$ rbenv install 2.0.0-p353

To verify the install:

$ rbenv local 2.0.0-p353
$ ruby -v
ruby 2.0.0p353 (2013-11-22 revision 43784) [x86_64-darwin13.0.0]

If you want to make Ruby 2.0.0 the global default version of ruby:

$ rbenv global 2.0.0-p353

Upgrade Rbenv - Mac OS X

To upgrade rbenv via homebrew:

$ brew update
$ brew upgrade rbenv
$ brew upgrade ruby-build

Remove Rbenv - Mac OS X

Uninstall the packages you installed via homebrew:

brew uninstall rbenv
brew uninstall ruby-build

Remove the following directory:

rm -rf $HOME/.rbenv

And remember to remove whatever you added to $HOME/.bash_profile

Linux

Install Rbenv and Ruby 1.9.x - Linux

Make sure the prerequisite packages are installed.

Ubuntu prerequisites:

$ sudo apt-get update
$ sudo apt-get install -y build-essential git
$ sudo apt-get install -y libxml2-dev libxslt-dev libssl-dev

RHEL/CentOS prerequisites:

$ sudo yum update
$ sudo yum install -y git
$ sudo yum install -y gcc-c++ patch readline readline-devel zlib zlib-devel
$ sudo yum install -y libyaml-devel libffi-devel openssl-devel make bzip2
$ sudo yum install -y autoconf automake libtool bison
$ sudo yum install -y libxml2-devel libxslt-devel 

Download the rbenv source distribution to $HOME/.rbenv:

$ git clone git://github.com/sstephenson/rbenv.git $HOME/.rbenv

Add $HOME/.rbenv/bin to your $PATH

$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> $HOME/.bashrc

Add rbenv init to your shell to enable shims and autocompletion:

$ echo 'eval "$(rbenv init -)"' >> $HOME/.bashrc

Restart shell as a login shell so that the PATH changes take effect:

$ exec $SHELL -l

Install the ruby-build plugin, which provides an rbenv install command to simplify the process of compiling and install new Ruby versions:

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

Install the latest version of ruby 1.9.x (at the time of this writing 1.9.3-p484)

$ rbenv install 1.9.3-p484

Rebuild the shim executable

$ rbenv rehash

You’ll need to run rbenv rehash every time you install a new version of Ruby or install a new gem.

Set the latest version of ruby to be the default version of ruby

$ rbenv global 1.9.3-p484

Verify the ruby install. If everything was installed correctly, the ruby -v command should report that version 1.9.3p484 is installed.

$ ruby -v
ruby 1.9.3p484 (2013-11-22 revision 43786) [x86_64-linux]

Install Bundler - Linux

You’ll need to use Bundler to manage gems. Installing a gem is also a good way to ensure that you’ve installed most of the Ruby prerequisites.

First, make sure you update to the latest version of Rubygems:

$ gem update --system

Then install the bundler gem. If the gem install command reports Successfully installed you’re good to go:

$ gem install bundler
Successfully installed bundler-1.3.5
Parsing documentation for bundler-1.3.5
1 gem installed
$ rbenv rehash

Install Ruby 2.0.0 - Linux

As of this writing, Ruby 2.0.0-p353 is the latest version of Ruby 2.0.0. Use rbenv install --list to print out the available versions. To install:

$ rbenv install 2.0.0-p353

To verify the install:

$ rbenv local 2.0.0-p353
$ ruby -v
ruby 2.0.0p353 (2013-11-22 revision 43784) [x86_64-linux]

If you want to make Ruby 2.0.0 the global default version of ruby (but currently Chef requires Ruby 1.9.x):

$ rbenv global 2.0.0-p353

Upgrade Rbenv - Linux

Since Rbenv is a Git repository, upgrading is just a matter of refreshing the source:

$ cd $HOME/.rbenv
$ git pull

Remove Rbenv - Linux

To uninstall/remove Rbenv, remove the following directory:

$ rm -rf $HOME/.rbenv

And remember to remove whatever you added to $HOME/.bash_profile

Using Pik to Manage Multiple Versions of Ruby on Windows

Out of the box, Ruby does not provide a mechanism to support multiple installed versions. pik is a third-party tool that can be used to switch between multiple versions of the Ruby interpreter on Windows.

Install Ruby

Install at least one Ruby interpreter environment using the RubyInstaller from http://rubyinstaller.org. While pik can install some versions of Ruby, the versions of Ruby it knows to install is quite limited, and it is just much easier to use RubyInstaller for Windows to install them.

The choice of Ruby environments to install is up to you. I have the latest version of 1.9.3 installed as my main development environment, and Ruby 2.0.0 to play around with the next generation of the language. When you install Ruby, do not add the Ruby executables to your PATH or associate .rb/.rbw files with the Ruby installation (the default choice) - pik will manage this setting:

Ruby Installer

Installing pik

Download the latest .MSI installer from pik downloads on github (as of this writing 0.3.0) and run the Pik Setup Wizard - the default options are fine:

Pik Installer

You can verify that pik installed properly by running the following in a Command Prompt:

> pik --version
pik 0.3.0.pre on Microsoft Windows [Version 6.1.7601]
by Gordon Thiesfeld (gthiesfeld@gmail.com)

Registering Ruby environments with pik

Use the pik add command to register your installed ruby environments with pik. For example, I have Ruby 1.8.7, 1.9.3, and 2.0.0 installed on my Windows development box, so I typed the following in a Command Prompt:

> pik add C:\Ruby187\bin
INFO: Adding:  [ruby-]1.8.7-p371
      Located at:  C:\Ruby187\bin

> pik add C:\Ruby193\bin
INFO: Adding:  [ruby-]1.9.3-p392
      Located at:  C:\Ruby193\bin

> pik add C:\Ruby200\bin
INFO: Adding:  [ruby-]2.0.0-p0
      Located at:  C:\Ruby2000\bin

Use the pik list command to list all the ruby interpreters registered with pik:

> pik list
   ruby-1.8.7-p371
   ruby-1.9.3-p392
   ruby-2.0.0-p0

Set a default Ruby

The pik use command will allow you to switch between your registered ruby interpreters:

> pik use ruby-2.0.0-p0
> ruby -v
ruby 2.0.0p0 (2013-02-024) [i386-mingw32]

Add the --default parameter to set one version as the default:

> pik use ruby-1.9.3-p392 --default

You can use the pik default command to switch to this version:

> pik default
> ruby -v
ruby 1.9.3p392 (2013-02-022) [i386-mingw32]

Unfortunately when you open a new command prompt, pik is not automatically loaded, so you will notice that there is no default ruby loaded:

Ruby not found'

Either run the pik default command to load pik (and the default ruby interpreter) or add one particular Ruby interpreter to your user or system PATH environment variable (via Control Panel -> System -> Advanced -> Environment Variables….):

Try Out Sensu Monitoring Using Virtual Box, Vagrant and Chef

Updated September 2nd, 2013

  • Updated to use the sensu-chef 0.5.5 cookbook

I’ve been using Sensu Monitoring in production for about three to four months now. It’s a nice, lightweight monitoring framework, designed with the cloud in mind and for use with modern configuration management frameworks like Chef and Puppet. For more information on Sensu, check out the article Why Sonian created Sensu (by Sean Porter) and the associated articles and links on the Sensu Wiki

In this article, I’m going to present a quick overview on how to test and evaluate Sensu using the Sensu Chef Cookbook Through the magic of Oracle VirtualBox and Vagrant, combined with Chef, you can quickly deploy Sensu to a local virtual machine instance, and kick the tires on Sensu to evaluate whether or not it is a good monitoring solution for you. These instructions apply to Mac OS X, Linux, and/or Windows for the host operating system.

Install VirtualBox

VirtualBox is an open source virtualization platform, similar to VMWare Fusion/Workstation that runs on Mac OS X, Linux and Windows (and a few more platforms). While I personally prefer VMWare Fusion to VirtualBox (VirtualBox can be a bit flakey at times), a lot of automation around VirtualBox has been developed within the Chef community, which impossible to ignore. Fortunately on both Mac OS X and Linux, VirtualBox can peacefully coexist with VMWare Fusion/Workstation (NOTE: If you use KVM virtualization on Linux, VirtualBox can also coexist, but you need to be careful not to run VirtualBox and KVM images simultaneously).

Just download and run the VirtualBox installer from the VirtualBox download page. Just download and install the latest 4.2.x version of VirtualBox. Make sure you save the VirtualBox installer as it comes with an uninstall tool, should you wish to remove VirtualBox at some point in the future.

Install Vagrant

After installing VirtualBox, next install Vagrant. Vagrant is an automation framework for VirtualBox. Grab the latest Vagrant installer for your OS from the Vagrant Downloads page and run install. The Vagrant install will automatically add the Vagrant binaries to your PATH.

Download the sensu-chef cookbook

Grab the latest version of the sensu-chef cookbook from GitHub by running the following command:

git clone https://github.com/sensu/sensu-chef.git

Install Ruby & RubyGems

The sensu-chef cookbook requires Ruby & RubyGems.

On Mac OS X and Linux, I strongly recommend that you use either RVM or Rbenv to make sure that you are using the latest version of Ruby instead of whatever version of Ruby your system installs by default.

See my previous articles on RVM or Rbenv. NOTE: If you don’t know whether or not to decide between RVM or Rbenv, go with RVM.

Install Ruby DevKit (Windows)

The sensu-chef recipe is dependent on the json gem. On Windows, you will get the following error if you do not have the proper Ruby DevKit installed:

Installing json (1.7.7)
Gem::InstallError: The 'json' native gem requires installed build tools.

Go to http://rubyinstaller.org/downloads and refer to the Which Development Kit? section of the web page about which DevKit you need to install.

Download the appropriate DevKit toolkit, extract it and run the following in a Command Prompt:

> ruby dk.rb init
[INFO] found RubyInstaller v1.9.3 at C:/Ruby193

Initialization complete! Please review and modify the auto-generated
'config.yml' file to ensure it contains the root directories to all
of the installed Rubies you want enhanced by the DevKit.

> ruby dk.rb install
[INFO] Updating convenience notice gem override for 'C:/Ruby193'
[INFO] Installing 'C:/Ruby193/lib/ruby/site_ruby/devkit.rb'

Create the sensu-chef virtual machine

Run the following commands to create the sensu-chef virtual machine:

cd sensu-chef/examples
gem install bundler
# On Windows, restart the command prompt before running 'bundle install' as
# gem install will reset the PATH
bundle install
librarian-chef install
vagrant up

If all goes well, the chef-solo run should have succeeded, and you should be able to view the Sensu dashboard by going to the following URL with the username admin and the password secret: http://localhost:8080

Sensu Dashboard

If this is successful, just run the following command to log in to your newly-created virtual machine instance:

vagrant ssh

And refer to the Sensu wiki on how to experiment with various configuration options.

When you are done playing with the test VM, run the following command to destroy the VM:

vagrant destroy