How Chocolatey saves me time
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.
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:
Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
- 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:
choco new NameofmyPackage
- 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:
<file src="tools\**" target="tools" />
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:
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:
<dependencies> <dependency id="firefox" /> <dependency id="googlechrome" /> <dependency id="powershell" /> <dependency id="pester" /> <dependency id="visualstudiocode" /> <dependency id="sql-server-management-studio" /> <dependency id="git" /> <dependency id="visualstudio2017enterprise" /> <dependency id="ssdt17" /> <dependency id="logitech-presentation" /> </dependencies>
After you’ve made those edits your nuspec file should look like this, with a lot of commented out code:
<?xml version="1.0" encoding="utf-8"?> <!-- Read this before creating packages: https://chocolatey.org/docs/create-packages --> <!-- It is especially important to read the above link to understand additional requirements when publishing packages to the community feed aka dot org (https://chocolatey.org/packages). --> <!-- Test your packages in a test environment: https://github.com/chocolatey/chocolatey-test-environment --> <!-- This is a nuspec. It mostly adheres to https://docs.nuget.org/create/Nuspec-Reference. Chocolatey uses a special version of NuGet.Core that allows us to do more than was initially possible. As such there are certain things to be aware of: * the package xmlns schema url may cause issues with nuget.exe * Any of the following elements can ONLY be used by choco tools - projectSourceUrl, docsUrl, mailingListUrl, bugTrackerUrl, packageSourceUrl, provides, conflicts, replaces * nuget.exe can still install packages with those elements but they are ignored. Any authoring tools or commands will error on those elements --> <!-- You can embed software files directly into packages, as long as you are not bound by distribution rights. --> <!-- * If you are an organization making private packages, you probably have no issues here --> <!-- * If you are releasing to the community feed, you need to consider distribution rights. --> <!-- Do not remove this test for UTF-8: if “Ω” doesn’t appear as greek uppercase omega letter enclosed in quotation marks, you should use an editor that supports UTF-8, not this one. --> <package xmlns="http://schemas.microsoft.com/packaging/2015/06/nuspec.xsd"> <metadata> <!-- == PACKAGE SPECIFIC SECTION == --> <!-- This section is about this package, although id and version have ties back to the software --> <!-- id is lowercase and if you want a good separator for words, use '-', not '.'. Dots are only acceptable as suffixes for certain types of packages, e.g. .install, .portable, .extension, .template --> <!-- If the software is cross-platform, attempt to use the same id as the debian/rpm package(s) if possible. --> <id>demopackage</id> <!-- version should MATCH as closely as possible with the underlying software --> <!-- Is the version a prerelease of a version? https://docs.nuget.org/create/versioning#creating-prerelease-packages --> <!-- Note that unstable versions like 0.0.1 can be considered a released version, but it's possible that one can release a 0.0.1-beta before you release a 0.0.1 version. If the version number is final, that is considered a released version and not a prerelease. --> <version>1.0.0</version> <!-- <packageSourceUrl>Where is this Chocolatey package located (think GitHub)? packageSourceUrl is highly recommended for the community feed</packageSourceUrl>--> <!-- owners is a poor name for maintainers of the package. It sticks around by this name for compatibility reasons. It basically means you. --> <!--<owners>__REPLACE_YOUR_NAME__</owners>--> <!-- ============================== --> <!-- == SOFTWARE SPECIFIC SECTION == --> <!-- This section is about the software itself --> <title>Craigs Choco Package</title> <authors>__REPLACE_AUTHORS_OF_SOFTWARE_COMMA_SEPARATED__</authors> <!-- projectUrl is required for the community feed --> <projectUrl>https://_Software_Location_REMOVE_OR_FILL_OUT_</projectUrl> <!--<iconUrl>http://cdn.rawgit.com/__REPLACE_YOUR_REPO__/master/icons/demopackage.png</iconUrl>--> <!-- <copyright>Year Software Vendor</copyright> --> <!-- If there is a license Url available, it is required for the community feed --> <!-- <licenseUrl>Software License Location __REMOVE_OR_FILL_OUT__</licenseUrl> <requireLicenseAcceptance>true</requireLicenseAcceptance>--> <!--<projectSourceUrl>Software Source Location - is the software FOSS somewhere? Link to it with this</projectSourceUrl>--> <!--<docsUrl>At what url are the software docs located?</docsUrl>--> <!--<mailingListUrl></mailingListUrl>--> <!--<bugTrackerUrl></bugTrackerUrl>--> <tags>demopackage SPACE_SEPARATED</tags> <summary>__REPLACE__</summary> <description>__REPLACE__MarkDown_Okay </description> <!-- <releaseNotes>__REPLACE_OR_REMOVE__MarkDown_Okay</releaseNotes> --> <!-- =============================== --> <!-- Specifying dependencies and version ranges? https://docs.nuget.org/create/versioning#specifying-version-ranges-in-.nuspec-files --> <dependencies> <dependency id="firefox" /> <dependency id="googlechrome" /> <dependency id="powershell" /> <dependency id="pester" /> <dependency id="visualstudiocode" /> <dependency id="sql-server-management-studio" /> <dependency id="git" /> <dependency id="visualstudio2017enterprise" /> <dependency id="ssdt17" /> <dependency id="logitech-presentation" /> </dependencies> <!-- chocolatey-core.extension - https://chocolatey.org/packages/chocolatey-core.extension - You want to use Get-UninstallRegistryKey on less than 0.9.10 (in chocolateyUninstall.ps1) - You want to use Get-PackageParameters and on less than 0.11.0 - You want to take advantage of other functions in the core community maintainer's team extension package --> <!--<provides>NOT YET IMPLEMENTED</provides>--> <!--<conflicts>NOT YET IMPLEMENTED</conflicts>--> <!--<replaces>NOT YET IMPLEMENTED</replaces>--> </metadata> <files> <!-- this section controls what actually gets packaged into the Chocolatey package --> <!-- <file src="tools\**" target="tools" /> --> <!--Building from Linux? You may need this instead: <file src="tools/**" target="tools" />--> </files> </package>
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:
Choco install <packagename> -s ".;chocolatey"
Or in our case this would be:
Choco install demopackage -s ".;chocolatey"
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.