Boxstarter guide for Windows development, 2018 edition

It’s been over 3 years since I wrote Automating setup of your windows development environment for fun and profit and a lot has changed in that time.

For instance, <sobs>

But enough about that, let’s talk about Boxstarter. The core concepts I described in the original post have not changed, but I did have to do some tweaking of my sample script in order to account for new bugs and updated software packages.

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 UWP development, you’ll have to run a local VM, so grab the latest Windows 10 ISO and start installing.

A note on Azure VMs

Spinning up a Windows 10 Virtual Machine in Azure is ridiculously easy right now. If you can afford it (and auto-shutdown your VM every night) then it is a very low-impact and low-maintenance way to get a dev VM up and running. If you want to know more then i’ll walk you through the process, otherwise skip to the next section.

  1. Assuming you already have an Azure account, go to the Virtual Machines blade, and “Create virtual machine”.
  2. I’d suggest creating a new resource group for the Virtual Machine, just to make it easy to tear down
  3. Select the “Windows 10 Pro, Version {latest}” Image
  4. Select “Allow selected ports”, and check “RDP (3389)”
  5. Set the auto-shutdown time that works for you
  6. Set any other options as you need, then create the VM – it’ll probably take a few minutes before you can connect to it

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 (use cup, see below) 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:
* 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. It was last updated in 2014, but many of the recommendations are still relevant.

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, and 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 boxstarter script 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.

I now recommend using choco upgrade instead of choco install (AKA cup instead of cinst), as upgrade will install or upgrade an older existing version. If you’re starting with a pre-made VM image, then it might already have some items installed.

IMPORTANT BUG WORKAROUND

At the time of writing, there is a bug with Boxstarter & Chocolatey where the temp file directory keeps getting larger and larger, eventually failing the installs because the path gets too long. To fix it,

Add this code once, prior to any chocolatey calls
$ChocoCachePath = "$env:USERPROFILE\AppData\Local\Temp\chocolatey"
New-Item -Path $ChocoCachePath -ItemType Directory -Force

Add the cacheLocation parameter to all choco install or upgrade commands
choco upgrade --cacheLocation="$ChocoCachePath" visualstudio2017enterprise

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 & Boxstarter

Powershell (administrator)
Set-ExecutionPolicy Unrestricted -Force
. { iwr -useb https://boxstarter.org/bootstrapper.ps1 } | iex; get-boxstarter -Force

Exit the current Powershell window, and run “Boxstarter Shell” – it’s Powershell with the Boxstarter modules already loaded.

5. 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).

6. 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.

7. 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 5, right? Just restore that checkpoint and re-run the script, until everything is juuuuust right.

8. Check the results

  • The complete run with an Azure Win 10 VM with SSD took 1h20m.
    • VS 2017 with 2 workloads took 28:23 minutes
    • 6 Windows updates took 19:01 minutes
  • Only 4 reboots (all unattended)

The final tally is 19 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.

One thought on “Boxstarter guide for Windows development, 2018 edition

  1. Pingback: Automating setup of your windows development environment for fun* and profit** | PhilChuang.com

Leave a Reply