Get started with an open source Windows package manager: Chocolatey

Download, install, and update applications on your Windows computer with the open source Chocolatey package manager.
101 readers like this.
Why and how to handle exceptions in Python Flask

Image from Unsplash.com, Creative Commons Zero 

Back in the 1990s, when Linux was a young operating system, Ian Murdock invented the concept of an app store in the form of what is now the apt command. This introduced the idea that a computer's capacity was boundless, and literally any command should be available to you; all you had to do was copy it from a network repository to your local system. It seemed impossible at the time, and yet it's commonplace now, whether you're on a Linux machine with DNF or Apt, Mac OS with Homebrew, or Windows with Chocolatey.

Chocolatey is software management automation for Windows that wraps installers, executables, ZIP files, and scripts into compiled packages. It's modeled after Apt and yum and unlocks a new world of automatable and predictable package management to Microsoft's operating system. Chocolatey is open source and encourages participation from the community. The more people who learn and use Chocolatey, the more its offerings of packages can grow.

Chocolatey's advantages

If you've never used a package manager before, then the advantages of Chocolatey might not be immediately obvious. When you start using Chocolatey, you may be excited about all of the great open source software available with just one or two commands. You don't have to hunt through every corner of the internet for cool new applications; thousands of them are discoverable, aggregated in one list (but still independent of one another online, a vital characteristic of a healthy and diverse ecosystem). Once you acclimate to having so many new choices of software to run, you might enjoy the ease of updating all of the software installed with Chocolatey at once. And finally, if you're a developer, you'll love the ease with which you can install and track your development environment.

Chocolatey is a bold step into a brave new world, so prepare yourself!

Install Chocolatey

Chocolatey requires 7-Zip, PowerShell, and admin privileges. If you don't have 7-Zip installed yet, download and install it before installing Chocolatey. It's a powerful open source archiving utility, and you'll be glad you have it—with or without Chocolatey.

To run PowerShell in admin mode, click on the Windows menu in the lower-left corner of your desktop and type powershell. Right-click on the PowerShell entry in the application menu and select Run as administrator.

Run Powershell as admin

Create an exception to your execution policy

PowerShell has a security feature to help users set basic rules to control when PowerShell may run scripts or load important configuration files. By configuring the execution policy, users avoid running a malicious script unintentionally.

A policy setting isn't meant to be restrictive, and you can easily adjust it through direct commands in PowerShell. There are a number of policy definitions available, and you can check your current policy with this command:

PS> Get-ExecutionPolicy

If this returns Restricted, then set it to AllSigned to allow scripts that have valid signatures by trusted publishers (and it prompts you when you attempt to run a script signed by someone you have not yet classified as trusted). Do this with:

PS> Set-ExecutionPolicy AllSigned

Creating an exception to the execution policy

Install the choco command

Before installing Chocolatey and its choco command, read through the install script.

Because running PowerShell as administrator places you within the C:\Windows\stystem32 directory, change to a reasonable location on your hard drive, such as your home directory, with:

PS> pwd
C:\Windows\System32
PS> cd \Users\$env.UserName

At the time of this writing, there's a bug in PowerShell that causes it to use TLS 1.0 when negotiating SSL on the internet. You must tell it to use a recent version of TLS instead, and the easiest way to do that is to set up some choices for PowerShell to cycle through until it finds the correct connection method:

PS> [Net.ServicePointManager]::SecurityProtocol = "Tls12, Tls11, Tls"

Download the install script:

PS> Invoke-Webrequest -Uri https://chocolatey.org/install.ps1 -OutFile chocolatey-install.ps1

Open the downloaded file, named chocolatey-install.ps1 by Invoke-Webrequest, in your favorite text editor or just read it in PowerShell:

PS> cat chocolatey-install.ps1 | more

If you're not familiar with PowerShell, this is a great way to get comfortable with its syntax. More importantly, reading through a script you want to run is a vital step in good security.

Once you've read through the install script and are comfortable with what you're about to do, run it:

Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))

Run choco

The main interface to the Chocolatey system is the choco command, which you just installed on your computer. Using choco, you can search for packages to install, install them, and keep them updated.

Searching for packages is a challenge if you don't know what packages exist. You can list (in theory) all the packages in the Chocolatey repository using the list subcommand:

PS> choco list

As of this writing, this only returns about 1,000 packages out of over 8,000 available. To browse through all packages in the Chocolatey repository, open a web browser, and navigate to chocolatey.org/packages.

Search for a package with choco

If you know what package you want, you can search for it in the Chocolatey repository using the list option along with the package name. (The list option has two aliases, search and find, so use the subcommand that comes most naturally to you.) Because choco searches titles and descriptions, there's usually a lot of output, so you might want to filter the output through more:

PS> choco search git | more

Install a package with choco

Once you've confirmed a package's availability, use the install command to install it on your system:

PS> choco install git

Depending on what kind of application it is, it may be available from the Windows menu or only as a PowerShell command. For instance, git has no GUI by default, so it's exclusively a PowerShell command, while the frontend git-cola is a GUI application and appears in the Windows menu.

Application installed with Chocolatey in the Windows menu

Update a package with choco

There are two ways to update a package you've installed with choco. You can either upgrade a package individually or upgrade all packages at once.

To upgrade just one package, use the upgrade option followed by the name of the package you want to upgrade. For instance, to upgrade a package named foo:

PS> choco upgrade foo

To upgrade all packages, use the keyword all as a package name:

PS> choco upgrade all

Remove a package with choco

If you decide a package isn't useful, you can purge it from your system with the uninstall subcommand followed by the package name you want to remove. For instance, to remove a package named foo:

PS> choco uninstall foo

Explore open source

Chocolatey is open source, and it makes it very easy to install many useful open source packages. However, not everything offered through Chocolatey is open source, so check the licenses carefully.

If you're new to open source, this is a great way to discover new tools, libraries, and applications. If you're already familiar with open source, Chocolatey provides an easy way for you to install and maintain your favorite packages. And best of all, if you're looking to migrate to Linux, Chocolatey makes a great introduction to important Linux concepts. Don't deny yourself the pleasure of trying Chocolatey. It's healthier than it sounds.

What to read next
Tags
Seth Kenlon
Seth Kenlon is a UNIX geek, free culture advocate, independent multimedia artist, and D&D nerd. He has worked in the film and computing industry, often at the same time.

2 Comments

This is great! I had never heard of it. I'm not a Windows user but many of my friends are. Thanks for sharing this.

Great article! I just want to point out a couple of points:

1. You don't need to install 7Zip before installing Chocolatey. The installation script will do that for you;

2. I wouldn't agree that asking a user to change their Execution Policy is a great idea (you ask the user to set it from Restricted to AllSigned) - the Chocolatey installation command only sets it for the specific session you're installing it within;

3. The command used to set TLS will only use those versions. It's better to ADD TLS 1.2 to what is already specified;

4. You suggest doing all the above and then suggest running the Chocolatey installation command which does all the above anyway so it's kind of redundant work. I think an explanation of what all of it means is a great idea while highlighting the command does it all for the user;

5. Can I suggest that anybody who wants to install Chocolatey checkout https://chocolatey.org/install rather than run the code directly from this article as it may change in the future and any changes won't be reflected here;

I think it's a great idea to suggest that the user read any script they run on their system, including the Chocolatey one. It's a bit like reading something before you sign it :)

Please reach out if you want more info or anything clarified.

Thanks!

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.