russell.cardullo

Setting up Chef and Vagrant on Windows 7 under Cygwin

Jul 15 2012

At a previous job we were restricted to Windows PCs only. This made setting up certain development tools more annoying than it should be.

Recently I started using Vagrant and Chef which both come with native Windows MSIs, except using these would require using the Windows command shell.  But with a few additional steps I was able to get these running under Cygwin using the native MSIs.

First: Chef!

  1. Download and install the native Windows installer

  2. If you don’t have it already, install git under Cygwin.  Use the Cygwin installer for this and select the ‘git’ package.

  3. The main issue I ran into after this point was that Chef uses a version of Ruby native to Windows, which means that running the various Chef scripts under Cygwin fail since the native Ruby program doesn’t understand the Cygwin style paths.  But this is easy enough to work around by creating a few aliases in your bash profile. Change the paths below to match wherever you installed Chef:

alias knife='/cygdrive/c/opscode/chef/embedded/bin/ruby C:/opscode/chef/bin/knife'
alias chef-client='/cygdrive/c/opscode/chef/embedded/bin/ruby C:/opscode/chef/bin/chef-client'
alias chef-solo='/cygdrive/c/opscode/chef/embedded/bin/ruby C:/opscode/chef/bin/chef-solo'
alias shef='/cygdrive/c/opscode/chef/embedded/bin/ruby C:/opscode/chef/bin/shef'
  1. At this point you should be able to run chef-client -v without any errors.

  2. Next setup a repository for your cookbooks. I’d recommend just cloning the opscode repository: shell git clone git://github.com/opscode/chef-repo.git

  3. Finally configure knife

mkdir chef-repo/.chef 
knife configure -r chef-repo

If you’re using Hosted Chef or Chef Server include your validation keys in chef-repo/.chef/ as well.

Note: when running knife cookbook site install X the process will fail to unzip the cookbook. However you can just manually unzip it to get it working.

Now: Vagrant!

  1. If you don’t have it, get Virtualbox

  2. Download and install the MSI installer for Vagrant

  3. I had an issue running Vagrant out of the box due to some wonky line breaks so do this:

dos2unix /cygdrive/c/bin/vagrant/vagrant/bin/vagrant
  1. Test with vagrant

  2. Vagrant defaults to creating Virtual Box VMs in Cygwin home and not Windows home. This will cause a problem if these are two seperate locations on your machine. After setting up a VM under Vagrant it will appear as if you’ve lost all VMs created outside of Vagrant.

You can fix this by creating a link in your Cygwin home before creating any Vagrant VMs. The links can be created through Windows command shell (but that’s as much as you’ll ever need to do in Windows shell for Vagrant/Chef). These commands should be run as administrator:

cd C:\Windows\system32
mklink C:\bin\cygwin\home\russellcardullo\.VirtualBox C:\Users\russellcardullo\.VirtualBox
mklink "C:\bin\cygwin\home\russellcardullo\VirtualBox VMs" "C:\Users\russellcardullo\VirtualBox VMs"

Or if you hate links you could just relocate your Cygwin home directory to match your Windows home directory. Or relocate your Virtualbox folders from your Windows home to Cygwin home.

You should now have a fully working Vagrant and Chef setup that can be invoked from Cygwin.