# Introduction to Chocolatey, a package manager for Windows
by Seth Kenlon

Back in the 1990s, when Linux was a young operating system, my friend [Ian Murdock](http://ianmurdock.debian.net/) invented the concept of an app store, in the form of what is now the ``apt`` command.
This introduced the world to the idea that a computer's capacity was boundless, and that literally any command ought to 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](LINK TO MY ARTICLE ABOUT 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 automatible and predictable package management to Microsoft's operating system.
Chocolatey is open source, and encourages participation from its community.
The more people who learn and use Chocolatey, the more its package offering can grow.

## Advantages

If you've never used a package manager before, then the advantages of Chocolatey might not be immediately obvious to you.
When you first start using Chocolatey, you may be excited about all of the great open source software available to you 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 what software to run, you might later enjoy the ease with which you can update all of the software installed with Chocolatey all at once.
And finally, if you're a developer, then 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](https://www.7-zip.org/download.html), Powershell, and admin privilege.
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](windows-ps-admin.jpg)

## 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 ``ExecutionPolicy``, 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](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_execution_policies?view=powershell-7) available, and you can check your current policy with this command:

```
PS> Get-ExecutionPolicy
```

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

```
PS> Set-ExecutionPolicy AllSigned
```

### Install the choco command

Before installing Chocolatey and its command ``choco``, you should read through the install script.
Because running Powershell as administrator places you within the ``C:\Windows\stystem32`` directory, first change to reasonable location on your harddrive, such as your home directory.

```
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 favourite 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're going to run is the most vital step in good security.

Once you've read through the install script and are comfortable with what you're about ot 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'))
```

## Running choco

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

Searching for packages is difficult when you don't know yet what packages exist.
You can list (in theory) all packages in the Chocolatey repository using the ``list`` subcommand:

```
PS> choco list
```

As of this writing, however, this only returns about 1000 packages out of over 8000 available.
To browse through all packages in the Chocolatey repository, open a web browser and navigate to [chocolatey.org/packages](https://chocolatey.org/packages).

### Searching for a package with choco

If you know what package you want, you can search for it in the Chocolatey repository with 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
```

### Installing a package with choco

Once you've confirmed package 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 front-end ``git-cola`` is a GUI application and appears in the Windows menu.

![An application installed with Chocolatey in the Windows menu](windows-git-cola.jpg)

### Updating 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 you can 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
```

### Removing 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 (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, then Chocolatey provides an easy way for you to install and maintain your favourite packages.
And best of all, if you're looking to migrate to Linux, then Chocolatey makes a great introduction to important Linux concepts.
Don't deny yourself the pleasure of trying Chocolatey.
It's healthier than it sounds.