Using Chocolatey to Manage Software Package on Windows

Most windows users like to download an installer to install software. However, for developers, probably they would rather like to use a command line tool (APT, Homebrew, etc.), like they get used on Linux and macOS, to install a program. Chocolatey is a command line package manager for Windows.

Installing Chocolatey

Run cmd.exe as administrator and use the command to install Chocolatey:

@"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"

chocolatey install

Once the installation is done, we can use `choco install’ to install a program. For example:

choco install atom

chocolatey install atom

How to Change the Installation Directory of Chocolatey

If you have multiple disk partitions on Windows, you may run into the problem of insufficient disk space.  You can use the command to change the installation directory as follows:

choco install atom --dir "g:\"

Unfortunately, I got the following error:

It appears you are attempting to use options that may be only available in licensed versions of Chocolatey (‘–install-directory’). Please remove and consult the documentation.

chocolatey change install directory

The reason is that the feature is available only for licensed editions (Pro and Business).

Creating and Publishing a Package with Chocolatey

To facilitate barcode application development, I decided to pack Dynamsoft Barcode Reader SDK (DLL files) with Chocolatey.

Create packages

Download Dynamsoft Barcode Reader for Windows.

Create a new package dbr:

choco new dbr

choco new

  • Dbr.nuspec is the required configuration file for every package.
  • *.ps1 files are PowerShell script files. We can use them to do extra tasks for installing, upgrading and uninstalling. If there is nothing to do, just delete them.
  • LICENSE.txt is required if including binaries.
  • VERIFICATION.txt is required if including binaries.
  • Read _TODO.txt and ReadMe.md to figure out what you should do and how you can do. The two files can be deleted.

Try to build the package:

choco pack

You will see the prompt:

'__REPLACE__' is not a valid version string.

Parameter name: version

Open dbr.nuspec and change the version:

<version>5.2.0</version>

Rerun the above command to generate an initial dbr.5.2.0.nupkg.

Copy Dynamsoft.BarcodeReader.dll and Dynamsoft.BarcodeReader.XML from Dynamsoft\Barcode Reader 5.2\Components\DotNet to .\tools. Since I have added the binaries, I have to edit LICENSE.txt and VERIFICATION.txt.

Build the full dbr.5.2.0.nupkg and install it locally:

choco pack
choco install dbr.5.2.0.nupkg
choco list --local-only

Publish packages

Now it is time to publish the package to https://chocolatey.org.

Register a Chocolatey account.

chocolatey account

Get the API key and publish the package:

choco apikey --key <api-key> --source https://push.chocolatey.org/
choco push dbr.5.2.0.nupkg --source https://push.chocolatey.org/

Install the package via the command:

choco install dbr