A Simple Tutorial: Create and Publish Chocolatey Packages

Chocolatey is a Machine Package Manager, somewhat like Apt, RPM but built with Windows. When you want to install a Windows application, with Chocolatey, what you need to do is to simply run a one line command — Chocolatey will automatically download and install this application for you. While there is an official guide on how to create and publish a package, this tutorial is simpler.

Create the Package

First, install Chocolatey if you haven’t done yet. Then, add %CHOCOLATEY_INSTALLATION%bin to the PATH environmental variable, where %CHOCOLATEY_INSTALLATION% is the Chocolatey installation path. The default installation directory is C:Chocolatey, so the path you need to add to PATH variable is C:Chocolateybin by default. If you don’t know how to modify environmental variables, take a look at this page.

After the preparing work is done, let’s create a new directory myapp-package for our work. In this directory, create a file named myapp.nuspec, with the following lines:

<?xml version="1.0"?>
<package xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <summary>This is an example App</summary>
    <description>Your descriptions here</description>
    <tags>some tags here</tags>
    <requireLicenseAcceptance>false</requireLicenseAcceptance> <!-- or true if you require the user to accept the License before installing -->

Replace the content above with the information of your app (The myapp.nuspec can be more complex than this, see here for details). Save the file.

Then create a tools directory, and copy chocolateyInstall.ps1 into the tools directory. Follow the comments in the template to do some modifications to chocolateyInstall.ps1. For the simplist case, your chocolateyInstall.ps1 will contain only one line, something like:

Install-ChocolateyPackage 'MyApp' 'msi' '/quiet' 'http://myapp.tld/myapp-0.1.0-x86.msi' 'http://myapp.tld/myapp-0.1.0-x64.msi' # The 64 bit package URL can be omitted.

Or for installation from a zip archive:

Install-ChocolateyZipPackage 'MyApp' 'http://myapp.tld/myapp-0.1.0-x86.zip' "$(Split-Path -parent $MyInvocation.MyCommand.Definition)"

Save this file.

Switch the current working directory to the myapp-package directory. Run cpack or chocolatey pack on the command line. See this page for details about cpack (or chocolatey pack) command. Then there should be a myApp. in the current directory — this is the package.

Test the Package

Run the following command in myapp-package directory to test your package:

cinst myApp -source %cd%

Publish the Package

The easiest way (or, to be more accurate, the easiest way to learn) to publish the package is to use the web interface. First, go to http://chocolatey.org/ and create an account. Go to the Upload Package page, and upload your package there. Done! There is an alternative way to publish the package on command line, see cpush.

You should be able to create a simple Chocolatey package now. For more details, please take a look at the official guide. Also, you can find many real world examples here.

* FYI, this blog post was written after I made this package.

4 thoughts on “A Simple Tutorial: Create and Publish Chocolatey Packages

  1. ferventcoder

    Thanks for the post! I don’t believe that you need to add those items to the path. The bin directory gets added automatically to the PATH on choco install. You never should need to get into the chocolateyinstall into the path. Choco puts a redirect in the bin folder.


Leave a Reply

Your email address will not be published.