Skip to content

Blog

Ubuntu and Chef with Vagrant and VirtualBox on Windows

In this post we show how you can use VirtualBox and Vagrant to launch an Ubuntu 14.04 guest and install the latest Chef client on it using Windows 10 as a host.

Prerequisites

You need VirtualBox and Vagrant installed. To do that, you can follow the steps described in Ubuntu with Vagrant and VirtualBox on Windows.

Chef

Install Chef Development Kit (ChefDK)

In PowerShell as Administrator:

choco install chefdk

Install Chef on the guest

Before you can use Chef Solo, Chef Zero, or Chef Client for provisioning, you need to install the latest Chef client on the guest.

Open Vagrantfile and add the following code at the end of the configuration:

# Provision Chef Client with a shell script that runs the Chef Omnibus Installer
# For more information see https://docs.chef.io/install_omnibus.html
# 
config.vm.provision "shell", inline: <<-SHELL
  sudo apt-get update -y
  sudo apt-get install curl -y
  curl -L https://www.opscode.com/chef/install.sh | sudo bash
SHELL

The complete Vagrantfile file should look like this:

# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
#
Vagrant.configure(2) do |config|

  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://atlas.hashicorp.com/search.
  #
  config.vm.box = "ubuntu/trusty64"

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  #
  config.vm.provider "virtualbox" do |vb|
    # For a complete reference, please see the online documentation at
    # https://docs.vagrantup.com/v2/virtualbox/configuration.html

    # Name used in Oracle VM VirtualBox Manager GUI
    vb.name = "Ubuntu-x64-Vagrant"

    # Customize the amount of memory on the VM (in MB):
    vb.memory = "2048"

    # Customize the amount of video memory on the VM (in MB):
    vb.customize ["modifyvm", :id, "--vram", "128"]
  end

  # Provision Chef Client with a shell script that runs the Chef Omnibus Installer
  # For more information see https://docs.chef.io/install_omnibus.html
  # 
  config.vm.provision "shell", inline: <<-SHELL
    sudo apt-get update -y
    sudo apt-get install curl -y
    curl -L https://www.opscode.com/chef/install.sh | sudo bash
  SHELL
end

Test

Reload and provision the guest virtual machine. This should install the latest Chef Client:

vagrant reload
vagrant provision

How to Install Jekyll on Windows

This post shows how to install Ruby and Jekyll via Chocolatey on Windows 8.1 or Windows Server 2012 R2 Update 1.

Chocolatey

If you do not have Chocolatey, you can install it by following the instructions on chocolatey.org. You may also see this post for instructions on how to set the Chocolatey cache location.

Ruby

Install

Open PowerShell as Administrator and run the following command:

choco install ruby

This will install Ruby 2.1.6 in C:\tools\ruby21\bin.

Configure

Close PowerShell and open it again, so it can pick the changes to the PATH environment.

Test

ruby --version
ruby 2.1.6p336 (2015-04-13 revision 50298) [x64-mingw32]

Ruby DevKit (2.0+)

Install

Open PowerShell as Administrator and run the following command:

choco install ruby2.devkit

This will install Ruby DevKit 2.0+ and its dependencies- in C:\tools\DevKit2.

Configure

Open C:\tools\DevKit2\config.ymland add C:\tools\ruby21 path at the end. The complete config.yml should look like this:

# This configuration file contains the absolute path locations of all
# installed Rubies to be enhanced to work with the DevKit. This config
# file is generated by the 'ruby dk.rb init' step and may be modified
# before running the 'ruby dk.rb install' step. To include any installed
# Rubies that were not automagically discovered, simply add a line below
# the triple hyphens with the absolute path to the Ruby root directory.
#
# Example:
#
# ---
# - C:/ruby19trunk
# - C:/ruby192dev
#
---
 - C:/tools/ruby21

Run the DevKit installer:

cd C:/tools/DevKit2
ruby dk.rb install

Jekyll

Install Bundler

gem install bundler

Create Jekyll Site

Create a jekyll directory under your home:

cd ~
mkdir jekyll

Create a New Bundle

cd ~/jekyll
bundle init
Jekyll Gem

For Jekyll we need the jekyll gem.

Syntax Highlighter Gem

We will use rouge as a syntax highlighter. This comes in the rouge gem.

Auto-generation Gem

Jekyll has a built-in auto-regeneration feature that watches your source folder for changes and then re-builds your site. On Windows, you need to install the wdm gem to enable this functionality.

Gemfile

Open the Gemfile file and add jekyll, rouge, and wdm packages. Here is the final file:

# A sample Gemfile
source "https://rubygems.org"

gem "jekyll"
gem "rouge"
gem "wdm"
Install Gems

Install gems locally:

bundle install --binstubs --path vendor

Create Site

cd ~/jekyll
bundle exec jekyll new site

Configure

Syntax Highlighter

Open ~/jekyll/site/_config.yml. Add this line at the end to set rouge as a syntax highlighter:

highlighter: rouge

Test

Build
cd ~/jekyll/site
chcp 65001
bundle exec jekyll build --watch
Serve

Open a second PowerShell window

cd ~/jekyll/site
chcp 65001
bundel exec jekyll serve

Open a browser and navigate to http://localhost:4000. You should see a basic Jekyll site running.

Auto-generation

Go to ~/jekyll/site and open About.md with a text editor capable of using UTF-8 encoding, e.g. Sublime Text. Change the title from About to About Me. Save the file. Refresh your browser and navigate to http://localhost:4000/about/. You should see the page has the new About Me title.

Ubuntu with Vagrant and VirtualBox on Windows

In this post I show how you can use VirtualBox and Vagrant to create and launch an Ubuntu 14.04 virtual machine on Windows 8.1 host.

Preparation

Chocolatey

We will use Chocolatey to install all the required software. If you do not have it, you can install it by following the instructions on chocolatey.org. You may also see this post for instructions on how to set the Chocolatey cache location.

VirtualBox

In order to use VirtualBox on Windows 10 you first need to disable Hyper-V.

Install VirtualBox

In PowerShell as Administrator:

choco install virtualbox

IMPORTANT: After the installation, run Oracle VM VirtualBox once and when prompted, follow the instructions to install the Oracle VM VirtualBox Extension Pack. The version of the installed extension pack should match the version of Oracle VM VirtualBox.

Add VirtualBox To System Path

Vagrant will need access to VBoxManage.exe. For that you need to add C:\Program Files\Oracle\VirtualBox to your path. You can do that easily in PowerShell, by using the Environment.SetEnvironmentVariable .NET method.

[Environment]::SetEnvironmentVariable("Path", "C:/Program Files/Oracle/VirtualBox;" + $env:Path, "Machine")

Close the PowerShell terminal and open it again, so it can pick up the PATH to VirtualBox.

Git

We want Git for ssh.exe and other Linux tools, which are needed later by Vagrant. If you have installed ssh.exe using some other way, you may skip this step.

Install Git

In PowerShell as Administrator:

choco install git --params "/GitAndUnixToolsOnPath /NoAutoCrlf"

Now, besides git, you should have ssh, rsync, touch, clear and a number of other useful Linux tools.

Verify Git and SSH

Close and open PowerShell, so it can pick the updated PATH environment.

Verify you have git:

git --version
git version 2.6.4.windows.1

Verify you have ssh:

ssh
usage: ssh [-1246AaCfGgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec]
           [-D [bind_address:]port] [-E log_file] [-e escape_char]
           [-F configfile] [-I pkcs11] [-i identity_file]
           [-L address] [-l login_name] [-m mac_spec]
           [-O ctl_cmd] [-o option] [-p port]
           [-Q cipher | cipher-auth | mac | kex | key]
           [-R address] [-S ctl_path] [-W host:port]
           [-w local_tun[:remote_tun]] [user@]hostname [command]

Vagrant

Install Vagrant

In PowerShell as Administrator:

choco install vagrant
Set VAGRANT_HOME (Optional)

The Vagrant home directory is where things such as boxes are stored, so it can actually become quite large on disk. By default, this is set to C:\Users\yourusername\.vagrant.d.

In PowerShell as Administrator:

mkdir C:/VagrantHome
[Environment]::SetEnvironmentVariable("VAGRANT_HOME", "C:/VagrantHome", "Machine")

You are now ready to launch virtual machines.

Initialize an Ubuntu 14.04 Box and Launch it

Create a directory for your project, e.g. C:\Ubuntu. Open PowerShell in the new directory and run the following commands:

vagrant init ubuntu/trusty64
vagrant up

That creates a Vagrantfile file from the ubuntu/trusty64 box. Every Vagrant development environment requires a box. You can search for boxes at https://atlas.hashicorp.com/search.

VirtualBox Guest Additions

During virtual machine boot you may get a warning about the VirtualBox Guest Additions version, something similar to this:

==> default: Checking for guest additions in VM...
    default: The guest additions on this VM do not match the installed version of
    default: VirtualBox! In most cases this is fine, but in rare cases it can
    default: prevent things such as shared folders from working properly. If you see
    default: shared folder errors, please make sure the guest additions within the
    default: virtual machine match the version of VirtualBox you have installed on
    default: your host and reload your VM.
    default:
    default: Guest Additions Version: 4.3.10
    default: VirtualBox Version: 5.0

There is a Vagrant plugin for that. The vagrant-vbguest plugin will check for and install the correct guest additions automatically on startup. To get the vagrant-vbguest plugin:

vagrant plugin install vagrant-vbguest

Now restart the VM:

vagrant reload

Login with SSH

vagrant ssh

Install Git on Windows via Chocolatey

This post shows how to use Chocolatey to install Git.

Chocolatey

If you do not have Chocolatey, you can install it by following the instructions on chocolatey.org. You may also see this post for instructions on how to set the Chocolatey cache location.

Git

Install Git

In PowerShell as Administrator:

choco install git --params "/GitAndUnixToolsOnPath /NoAutoCrlf"

Close and open PowerShell, so it can pick the updated PATH environment.

Verify

Verify you have git:

git --version
git version 2.6.4.windows.1

Verify you have ssh:

ssh
usage: ssh [-1246AaCfGgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec]
           [-D [bind_address:]port] [-E log_file] [-e escape_char]
           [-F configfile] [-I pkcs11] [-i identity_file]
           [-L address] [-l login_name] [-m mac_spec]
           [-O ctl_cmd] [-o option] [-p port]
           [-Q cipher | cipher-auth | mac | kex | key]
           [-R address] [-S ctl_path] [-W host:port]
           [-w local_tun[:remote_tun]] [user@]hostname [command]

How to use a different cache directory for Chocolatey packages

About Chocolatey

In short, Chocolatey is a package manager for Windows which saves time by automating otherwise manual installation tasks. You can install Chocolatey by following the instructions on chocolatey.org.

Configure Chocolatey's Cache Folder

For security reasons, Chocolatey installs in C:\ProgramData\chocolatey by default. See Why does Chocolatey install where it does? for more information.

Also by default, Chocolatey will download packages in C:\Users\USERNAME\AppData\Local\Temp\chocolatey which might work or not for you. If you need to use a different folder, you can set the cacheLocation key in C:\ProgramData\chocolatey\config\chocolatey.config to a folder of your choice.

For example, to use C:\ChocolateyCache as a cache folder, add <add key="cacheLocation" value="C:\ChocolateyCache" description="Cache location if not TEMP folder." /> to the chocolatey.config file like this:

<?xml version="1.0" encoding="utf-8"?>
<chocolatey xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <containsLegacyPackageInstalls>false</containsLegacyPackageInstalls>
  <commandExecutionTimeoutSeconds>0</commandExecutionTimeoutSeconds>
  <config>
    <add key="cacheLocation" value="C:/ChocolateyCache" description="Cache location if not TEMP folder." />
    <add key="containsLegacyPackageInstalls" value="true" description="Install has packages installed prior to 0.9.9 series." />
    <add key="commandExecutionTimeoutSeconds" value="2700" description="Default timeout for command execution." />
    <add key="proxy" value="" description="Explicit proxy location." />
    <add key="proxyUser" value="" description="Optional proxy user." />
    <add key="proxyPassword" value="" description="Optional proxy password. Encrypted." />
  </config>
  <sources>
    <source id="chocolatey" value="https://chocolatey.org/api/v2/" disabled="false" priority="0" />
  </sources>
  <features>
    <feature name="checksumFiles" enabled="true" setExplicitly="false" description="Checksum files when pulled in from internet (based on package)." />
    <feature name="autoUninstaller" enabled="false" setExplicitly="false" description="Uninstall from programs and features without requiring an explicit uninstall script." />
    <feature name="allowGlobalConfirmation" enabled="false" setExplicitly="false" description="Prompt for confirmation in scripts or bypass." />
    <feature name="failOnAutoUninstaller" enabled="false" setExplicitly="false" description="Fail if automatic uninstaller fails." />
  </features>
  <apiKeys />
</chocolatey>