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
  • https://about.me/ferventcoder ferventcoder

    The package itself will always install to the Chocolatey lib directory (it needs to for tracking installs and upgrades). The resources from the package (the software) is what you would be putting to a custom directory. If the path you passed did not work, it’s possible you didn’t pass the parameters in the proper way `–package-parameters “‘/InstallationPath:f:choco-dirsysinternals'”` – https://chocolatey.org/docs/commands-reference#how-to-pass-options-switches

  • http://www.dynamsoft.com/ Xiao Ling

    Thanks again for your information, which is pretty useful. I’ve tested it via the command “choco install sysinternals -d –package-parameters ‘/InstallationPath:””f:choco-dir””‘”. The conclusion is that the command does not change the package installation directory. It inspires me that I just need to pack PowerShell script and download other resources to any directories by running the script :)

  • https://about.me/ferventcoder ferventcoder

    https://chocolatey.org/packages/sysinternals is an example of a package with package parameters – a walkthrough is at https://chocolatey.org/docs/how-to-parse-package-parameters-argument.

    For installers, one needs to know the native install arguments and then you just pass them through. The feature page you linked has a link to how to handle it in FOSS – https://chocolatey.org/docs/getting-started#overriding-default-install-directory

  • https://about.me/ferventcoder ferventcoder

    You might also mention that you are creating and pushing a package to the community repository. You can stand up your own or use a service like MyGet to push packages to if they don’t make sense for the community repository.

    With the community repository, there is also a concept of moderation. A package will need to get through automated quality checks, automated testing of install/uninstall, and virus checking, and human verification prior to being able to issue the choco install command to install the software.

    Again, great article!

  • http://www.dynamsoft.com/ Xiao Ling

    Thanks. Could you show me an example with detailed information? I’ve written a PowerShell script to move the package. But apparently it is not a perfect solution.

  • https://about.me/ferventcoder ferventcoder

    Howdy, great article. Addressing the install directory feature – the packaging may support package parameters that would allow you to change the install directory without a need for a licensed version. If it is a package that manages an installer and you know the silent argument for changing the install directory, you can pass it through `–install-arguments`, which will also allow you to change the install directory.

    The ubiquitous install directory switch just allows an ease of use switch provided as a convenience so folks don’t need to do the research above.