Automating setup of your windows development environment for fun* and profit**

With the release of Windows 10 Technical Preview, I started thinking seriously about trying it out and begin migrating my apps. But on the other hand, I didn’t want to take my existing dev/play machine and potentially break something that’s working well. So I figured i’d spin up a new VM in order to try it out.

Then I began thinking about all the time I would have to spend just installing and configuring my apps, SDKs, editors, what have you – Visual Studio alone takes multiple hours, not to mention the plethora of Windows updates.

ain’t nobody got time for that.

Luckily, I remembered seeing in Scott Hanselman’s Ultimate Dev Tools List 2014 a solution for automating installation & configuration: Enter Boxstarter and Chocolatey.

TL;DR

Install Chocolatey & Boxstarter, write scripts, press enter, and voilà! Fresh dev machine.

Chocolatey

Chocolatey

You know NuGet, right? It’s a package (3rd-party library) manager for software development. Chocolatey is built on NuGet, but the difference is that it’s an application manager for your computer. If you’re familiar with *nix systems, it’s essentially apt-get for Windows.

A command-line interface for installing apps might not seem like a big deal – until you realize that most apps delivered via Chocolatey can be A) installed unattended (does not require user input) and B) can all be updated with a single command. It’s way better than googling an app, visiting the official website, finding & downloading the latest version, installing & clicking through multiple dialogs, and ultimately forgetting to delete the install.

I’d go into more detail, but (again) Scott Hanselman has a nice write-up on Chocolatey, and the official Chocolatey.org has clear instructions as well.

Boxstarter

Boxstarter

If Chocolatey is the creamy filling, then Boxstarter is the Oreo cookie that binds everything together. Boxstarter provides a framework for scriptable, repeatable installs. It’ll handle reboots, windows/microsoft updates, and you can even remotely execute these scripts on Hyper-V/Azure VMs.

Yum
not-so-subliminal advertising

Let’s get started

1. Prepare the VM

Since this will take some time, you’ll want to start by creating your VM and begin the installation of your Windows OS. Create a Hyper-V VM, or spin one up from the Azure VM Gallery. For Windows 10 development, you’ll have to run a local VM, so grab the latest Windows 10 ISO or if you already have a Windows 8 VM you can upgrade it to Windows 10. If you want to create an Azure VM it doesn’t appear that you can do Windows 10, so I’d suggest picking a Visual Studio VM from the image gallery (that way you can skip a few hours on this install).

So while that’s running…

2. Write the install script

Now it’s time to think about what applications and tools you want installed. The difficulty here lies in that some of your apps may not be available in Chocolatey – and even if they are, they might not be completely up-to-date. Unless the app publisher is also publishing the Chocolatey package, then odds are it’s someone in the Chocolatey community manually creating packages for these apps. Therein lies the main caveat for using Chocolatey – there is no freshness guarantee. The more popular, the better your chances; the more esoteric, the less likely so. Search the Chocolatey Package Gallery for your apps, and then compare it against the official site – also check the package comments and for yet-to-be-approved updates.

Save the chocolatey install commands in a new powershell script file (.ps1) – this file needs to be accessible from the VM, so I recommend putting it in something like your Dropbox public folder, or as a Github Gist.

Besides installing apps, you can also do some other neat commands:
* Install-ChocolateyPinnedTaskBarItem – Pin programs to your task bar
* Enable-MicrosoftUpdate and Install-WindowsUpdate
* Enable-RemoteDesktop
* Set-StartScreenOptions
* Set-WindowsExplorerOptions
* TZUTIL – Set the OS time zone
* Disable-UAC and Enable-UAC

Feel free to take my scripts and adapt them to your own needs.

And if you’re looking for new dev apps/tools, you can refer to Scott Hanselman’s Ultimate Dev Tools List 2014.

Once you’ve figured out what you’re going to install, you should figure out what order to install them in.
I recommend that apps that require a reboot be placed early in the script, and non-rebooting apps afterwards. The reason for this is because Boxstarter repeats the entire script after every reboot, and for already-installed packages, Chocolatey will take at least a few seconds each to verify the installation. This can get tedious, so in order to minimize repetition and delay, put rebooting apps first. The usual culprits here are Visual Studio, .NET frameworks, SQL Server, Chrome.

The finishing touches on the install script are the steps to take before and after Chocolatey. Prior to Chocolatey, I recommend using Disable-UAC so that UAC won’t pop up on you during the script. After Chocolatey, you’ll want to perform Windows Updates, then Windows OS configuration, junk file cleanup, and then Enable-UAC. Again, see my gist for examples.

If there are any apps that don’t have a good Chocolatey package, then at the very end is a good point to launch URLs to the app’s download page. Simply put in something like start http://codesector.com/teracopy and it should launch a web browser.

3. Write the execution script

Now you need a script to kick off Boxstarter. In order for Boxstarter to perform unattended installs, it’ll need to cache your credentials. Here’s my execution script:

$cred=Get-Credential MicrosoftAccount{your_ms_account_here}
Install-BoxstarterPackage -PackageName {public_location_of_your_boxstarter_install_script} -Credential $cred

There are also ways to execute this script from outside the VM, but I haven’t had luck doing so. It just saves you the step of logging into the VM. Powershell will ask for your credentials when you execute this command.

4. Install Chocolatey

Might as well install it now, so that you can install Boxstarter via Chocolatey (as opposed to downloading the boxstarter zip):

Powershell
iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))

5. Install Boxstarter

Powershell
choco install boxstarter

Also note that you may have to change the execution policy:

Powershell (administrator)
Set-ExecutionPolicy Unrestricted -Force

See Using Boxstarter for more details.

6. Create a checkpoint/Backup your VHD

Just in case something goes wrong, create a checkpoint/snapshot your VM so that you don’t have to start over from scratch if things go wrong (and it probably will the first few times).

7. Run the execution script

Take the script you created in step 3, and either execute it directly from a public share or mapped drive, or copy & paste it into the Boxstarter Shell (launch it from the Start menu or desktop link). It’ll ask you for your login credentials, then start the installation process.

8. Sit back and relax

Well, not just yet. The first few times you run the script, you’ll want to keep an eye on it and see how well it works. You can either login after every reboot and watch the command window scroll by, or you can read the boxstarter log file which can be found at %LOCALAPPDATA%\Boxstarter\Boxstarter.log.

You may see some Chocolatey installation failure messages, but usually that’s nothing to be concerned about. Most of the time these failure messages simply indicate that the install requires a reboot, and after the reboot, Chocolatey will report a successful install. For example, the VS2013.4 update package requires 3 reboots. If Chocolatey moves past the package and doesn’t retry it then that’s more indicative of an installation failure.

You’ll likely go through a dozen iterations of the install script tweaking it to get everything just right. You did create that checkpoint in step 6, right? Just restore that checkpoint and re-run the script, until everything is juuuuust right.

9. Check the results

For me, a complete run of my script took 3 1/4 hours. The longest installs were:
* VS 2013 Ultimate – 29 minutes
* VS 2013 Update 4 – 88 minutes
* SSMS 2014 Express – 18 minutes
* 59 windows/microsoft updates – 41 minutes

The final tally is 24 applications installed, a fully updated and configured windows, and all I had to do was copy & paste a command into powershell. Not too shabby.

And one last step: Using Enable-UAC requires a reboot but Boxstarter doesn’t do it for you, so you’ll have to restart manually. Also, you’ll probably have to “Update and Restart” several times before all the Windows Updates are complete.

Closing thoughts

Sure, it probably took longer to automate this whole process as opposed to doing it manually once (even twice). It’s probably not worth it for the average computer user – but as developers, we’re likely repaving dev machines all the time (or doing everything we can to avoid it).

Taking a bit of extra time now is definitely worth it in the long run – now that it’s easy and less time-consuming, you’ll find yourself spinning up new instances all the time, and spend more time doing the things you actually want to be doing. Y’know, fun and profit.

* subjective
** not guaranteed, but this should save you time, and time => money

Leave a Reply