How Chocolatey Works
- Chocolatey uses NuGet (NuGet.Core.dll) to retrieve the package from the source. This is typically a nupkg that is stored in a folder, share, or an OData location (HTTP/HTTPS). For more information on sources, please see Sources and Source Repositories.
The package is installed into
$env:ChocolateyInstall\lib\<pkgId>. The package install location is not configurable: the package must install to this location for tracking, upgrade, and uninstall purposes. The software that may be installed later during this process is configurable. See Terminology to understand the difference between "package" and "software" when related to Chocolatey.
- Chocolatey determines if the package is self-contained or if it has automation scripts. Currently these automation scripts are limited to PowerShell scripts (*.ps1 files) but other formats may be possible in future releases.
- Chocolatey takes a registry snapshot for later comparison.
- If there are automation scripts, Chocolatey runs them. You have the full capability of PowerShell, but you should try to ensure they are compatible with PowerShell v2 and beyond.
- Chocolatey compares the snapshot, determines uninstaller information, and saves that to a .registry file.
- Chocolatey snapshots the folder based on all files that currently reside in the package directory.
- Chocolatey looks for executable files in the package folder and generates shims in the
$env:ChocolateyInstall\binfolder to ensure availability on the path. The executable files may have been embedded into the package or downloaded from a remote source and placed there. If there is a shim ignore file (
<exeName>.exe.ignore) then Chocolatey will not generate a shim in the bin folder.
- Chocolatey will look for and run a chocolateyBeforeModify.ps1 file in the existing package prior to upgrading or uninstalling a package. This is your opportunity to shut down services and/or processes. The chocolateyBeforeModify.ps1 is run from the existing package, not the new version of the package. If it fails, it passes a warning and continues to the next step.
- Chocolatey makes a backup of the package folder (and only the package folder) prior to attempting an upgrade.
- The snapshot is used to determine which files can be removed from the package folder. If those files have not changed, they will be removed.
- If the upgrade fails, Chocolatey will automatically rollback the package folder to the previous version while passing an error message. This does not fix any folders used outside of the package directory, such as where the native installer installed a program; those fixes must be completed by the user. Chocolatey also does not rerun installation scripts on rollback.
- Chocolatey makes the determination that the package is actually installed.
- Chocolatey will look for and run a chocolateyBeforeModify.ps1 file in the existing package prior to upgrading or uninstalling a package. This is your opportunity to shut down services and/or processes. If it fails, it passes a warning and continues to the next step.
- Chocolatey makes a backup of the package folder.
- If an automation script is present, Chocolatey runs it. This is used to clean up anything placed by the installation script.
- If using the auto-uninstaller, Chocolatey will attempt to automatically uninstall the package if a silent uninstall can be determined. Otherwise, it will prompt the user (unless passed with -y) to continue with uninstallation. The auto-uninstaller can detect approximately 80% of native installers and determine the silent uninstall arguments.
- If successful, the snapshot is used to determine what files can be removed from the package folder. If those files have not changed, they will be removed.
- If everything has been deleted from the package folder, the folder is also removed.
When a package has an exe file, Chocolatey will create a link "shortcut" to the file (called a shim) so that you can run that tool anywhere on the machine. See shimming for more information.
When a package has a chocolateyInstall.ps1, it will run the script. The instructions in the file can be anything. This is limited only by the .NET framework and PowerShell.
Most of the Chocolatey packages that take advantage of the PowerShell download an application installer and execute it silently.