How Chocolatey saves me time

Reading Time: 6 minutes

CodeSnippets #3

I want to share an application I use when setting up a new machine or recovering from a re-install of Windows, called Chocolatey.

Since I started using Chocolatey, I find a 50/50 split of people I talk to that have actually heard of it. Until very recently I fell into the “I have no idea what you are talking about but I’ll nod knowingly” bucket.

via GIPHY

My co-speaker and friend, Paul Broadwith (b|t) is a long time contributor to the Chocolatey community. He introduced me to it shortly before he started working with them last year and I was blown away by how easy to use it was and the time it saved me.

So, what is Chocolatey then?

In my own words, it’s the quickest way to get a machine up and running, ready to start working. Chocolatey is a package manager for Windows that allows you to script out the installation of (what feels like) almost anything. I was able to get up and running on a new laptop in just a few hours, while I worked on something else. Forget constant Next > Next > Next installation wizards. I had all the tools I needed to do my job, not just apps but PowerShell modules too, all installed automatically.

How do I use it then?

It’s pretty simple to get started and I know I’m not using it to it’s full potential but here’s how I use it:

  • Have a look on the Chocolatey site for fantastic Installation instructions. I installed using PowerShell with the following command from an elevated prompt:
  • Once installed, you can run commands to install any of the packages listed on the Chocolatey community repository. Paul showed me how to create my own package so I could list out all of the apps that I wanted then all I have to do is execute the package. Start by creating a package in the directory of your choice:
  • This creates a a folder structure and files used for actually publishing a package but I just want to use this to manage my own machine and apps so I don’t need to worry about all the other components.


You can delete these but you’ll also need to comment or remove the reference to the tools folder in the nuspec file:

Now we need to open that file and add some minimal required info to use it and we can get started listing out all the apps I use. You can get a comprehensive list of the required components from Microsoft’s nuspec reference.

Now we need to open up the nuspec file and make the required edits:

Becomes…

 

We need to set the version so that the file will package up correctly. As this is for our own use we can set this to anything.

The title will already be set but you can change that if you wish. I’ll set mine to “Craig’s Choco package”. We finally need to un-comment the dependencies section. This is where we’ll add all of our apps using the names on Chocolatey.org. There is code their to define versions but I’m happy to grab the latest version of all my apps so I’ll discard those parts. Here’s an example:

 

After you’ve made those edits your nuspec file should look like this, with a lot of commented out code:

 

The last thing we need to do before we can use it is to package it up by running:

This created the NUPKG file in the same folder as your nuspec file. You’ll need to increment the version number and re-run this command everytime you add or remove dependencies (apps you want to install).

The easy bit

To Install all these lovely apps, with no effort at all simply run the following command:

Or in our case this would be:

To break this command down a little, the -s flag will try to find the app in the local directory first, then go to Chocolatey to get it if it’s not found or up to date. That means, you can just run this package regularly and not have to worry about it re-installing already up-to-date apps. It’s so simple and robust that I mourn all the hours I’ve spent searching, downloading and clicking through wizards all these years!

It’s worth noting at this point that if you remove one of those apps from your dependency list, it won’t uninstall the app next time you run the package, it’s just going to ignore it.

Now, this isn’t the only way to install apps using Chocolatey but it’s the way I found the easiest. I could fire it off and go do something else. More info can be found on Chocolatey’s Install command help page. I hope this saves others as much time as it’s saved me.

 

 

Photo by Monique Carrati on Unsplash

You may also like...

Leave a Reply