How to Create your First Chocolatey Package

Adam Bertram

Read more posts by this author.

As automation becomes more of necessity rather than a nicety, it’s important we IT folk look at better ways to perform menial tasks; one of those tasks is installing software. Rather than download an MSI, double-click on it and run through the wizard, it would behoove you to think of a better way. In this blog post, learn how to create a Chocolatey package (a popular Windows packaging manager).

Nearly all software has command-line switches to install silently, and if you perform enough research, you might figure out the right switches. Your install might then look like this:

> install.exe /q /n /e /noreboot

Are you going to remember all of those switches? Probably not. You could put these switches in a batch file and then forget where you put that script or you could do this:

Install-Package -Name AcmeSoftware

Now that’s something that can be remembered easily.

But how would you get to that point? The answer is a NuGet repository and a NuGet package. NuGet is a traditional software packing technology that allows you to essentially wrap up a piece of software which can then be stored in a repository, downloaded and installed at will. Chocolatey is a favorite open source tool that uses NuGet to allow IT administrators to download and silently install software.

In this article, I’m going to assume you’ve already got a NuGet repository setup or you know where you’re going to send your package.

Let’s take a piece of software and wrap it up in a NuGet package for use in one of these repositories.

Installing Chocolatey

Since Chocolatey is not installed by default on a Windows operating system, it must be downloaded and installed. It can be downloaded and installed by running this in a PowerShell console:

PS> iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))

Creating an NUSPEC file

To create a Chocolatey package, you first must create an NUSPEC file. An NUSPEC file is an XML package manifest that describes the package contents. This NUSPEC file must be in a particular format. Here’s a sample NUSPEC file using only the required elements:

<?xml version="1.0" encoding="utf-8"?>
    <package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
      <metadata>
        <id>MySpecialSoftware</id>
        <version>1.0</version>
        <description>Some software I'm packaging up.</description>
        <authors>Adam Bertram</authors>
      </metadata>
    </package>

For the complete rundown of all the tags that can be used refer to the NUSPEC reference.

How to Create a Chocolatey Package

Next, create the package. To do this, we use the Install-ChocolateyInstallPackage command. Let’s pretend that the software we’re packaging up is an EXE and the switches to install it silently are /s. We must send the file location along with the silent switches to use to the command.

PS> Install-ChocolateyPackage -PackageName 'AcmeSoftware' -FileType 'exe' -File 'C:\install.exe' -SilentArgs '/s'

This command will create the package. Next, we’ll need to pack the package using the choco exe.

PS> choco pack

Once the software has been packaged up, it’s now in a format to where you can publish to a Chocolatey repository of your choice.

Summary

This process can be repeated for each piece of software you need to deploy via Chocolatey. As you can see, there are only a few additional steps that must take place to get the software “Chocolatey-ready.” By doing so now allows you to get back to doing tasks that matter rather than figuring out how to deploy software to a bunch of machines at once!

If you’d like a deep dive into creating Chocolatey packages, be sure to check out the Chocolatey docs.

Looks like you're offline!