Getting to Know Chocolatey: the Windows Package Manager

Ronaldo B.August 1, 20224 min read

In this article

Do you have the habit of using your computer's Terminal? Do you enjoy doing this? If you have a Linux or Mac machine, you are probably used to using it. However, among Windows Operating System users, it's not very common to use the Command Prompt.

When we need to download a program, we are used to going to its website and following the installation step by step, the famous “Next Next Finish”. However, when we start programming, we learn to use the Terminal to download packages or libraries. And we realize that this can help us a lot, especially regarding practicality.

Now, imagine that we could go further and download programs or applications on our Windows System using the command line. That would be amazing, wouldn’t it? And it is possible! Through Chocolatey, the Windows Package Manager. In this article, we will learn how to install and configure it on our computer.

Installing Chocolatey

You can access the Chocolatey website by clicking here.

To install it, we will use Windows PowerShell. To find it, open the Start Menu and type “PowerShell”. After finding its icon, right-click and choose the “Run as administrator” option, as we can see below:

PowerShell in the Start Menu

For the installation to occur, we need to do a check: if our Terminal's Execution Policy allows us to run scripts or download programs and packages. To perform this check, execute the following command:

Get-ExecutionPolicy

If the return equals “Restricted,” we will need to change this setting. We will do this by executing the following command:

Set-ExecutionPolicy AllSigned

This should solve the issue. However, if it doesn’t work, execute the command below:

Set-ExecutionPolicy Bypass -Scope Process

After that, we will install Chocolatey using this command:

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'))

That's quite a long command, isn’t it?

After the download finishes (it may take a few seconds), we will check if Chocolatey was correctly installed using the choco -? command. It will return some options that Chocolatey offers us and will finally show the version that was installed, as we can see below:

Chocolatey version displayed in PowerShell

Using Chocolatey

To use Chocolatey, we will follow the same pattern as other Package or Dependency Managers, like Homebrew, npm, or the set of apt tools. We will use the choco install command followed by the name of the package we want to install. We can find a list of all the packages that Chocolatey has by clicking here.

Let’s do an example: we will install Git using Chocolatey

Git package displayed on the Chocolatey website

The page informs us that we should execute the command choco install git. Let’s do that in our PowerShell. It may ask us a few questions, just answer yes to them.

After the download, we will see the following result:

Result of Git download in PowerShell

And if we execute git --version, we will see the following:

Git version displayed in PowerShell

We see that Git was installed on our machine correctly! And the best part: it was not necessary to go to its website, download an executable, and install it through a graphical interface. We did all of this via the Terminal with a single command. Undoubtedly very practical, isn’t it?

Another advantage of using Chocolatey is the programs that rely on others as dependencies to work. Examples: programs like MySQL Workbench, XAMPP, or Visual Studio Code use the Visual C++ Redistributables as dependencies. They need these packages to function, and you may have encountered problems installing them. Chocolatey automatically downloads these required programs and configures everything for us. It sets it up in such a way that we only need to open the desired program, and it will already be ready to use, saving us a lot of headaches.

Following the pattern we saw above, if we want to search for a package, we can use the choco search command. It will return how many packages it found based on what we informed it to search for.

Another interesting feature is using the choco upgrade command. It updates one or more packages to their current version, performing a true “upgrade” on any program.

And finally, if we want to uninstall a program, just execute choco uninstall and provide its name. Just one detail: this command will only work with packages that we installed with Chocolatey. This means that it may not recognize other programs we installed before, without choco install.

Conclusion

When working with Web Development, we wish for repetitive processes to become increasingly agile, and this includes downloading and installing programs. If you want to optimize these processes, use Chocolatey; it really helps us save valuable time that we can use to continue developing web projects.

Until the next article :)

Keep learning with Hcode

Sign up for the course platform and follow the next blog guides.