Keywords: NuGet | Package Management | Version Control
Abstract: This article provides a comprehensive guide on how to download specific versions of packages using NuGet, rather than only the latest ones. It covers the use of the Install-Package command in the Package Manager Console to install historical versions by specifying version numbers. Additionally, the Get-Package command is explained for listing all available versions, and the Tab key auto-completion feature is highlighted to streamline operations. These techniques are essential for dependency management, version rollbacks, and compatibility testing.
Basic Operations in the Package Manager Console
In Visual Studio, open the Package Manager Console via Tools > NuGet Package Manager > Package Manager Console. This is a PowerShell-based command-line interface dedicated to managing NuGet packages.
Command for Installing Specific Package Versions
When using the Install-Package command, you can specify the version to install with the -Version parameter. For example, to install version 1.2.0 of the Common.Logging package, run:
Install-Package Common.Logging -Version 1.2.0After executing this command, NuGet will download and install the specified version of the package and its dependencies from configured sources, such as nuget.org.
Listing Available Versions
Before installation, you can view all available versions of a package using the Get-Package command. The command format is as follows:
Get-Package -ListAvailable -Filter Common.Logging -AllVersionsHere, the -ListAvailable parameter lists packages from remote sources, -Filter specifies the package name filter, and -AllVersions ensures that all historical versions are displayed, not just the latest.
Using Tab Key Auto-Completion
In the Package Manager Console, after typing the Install-Package command with the -Version parameter, pressing the Tab key automatically lists the latest available versions of the package. This facilitates quick version selection without manually entering the full version number.
Application Scenarios and Best Practices
Common scenarios for downloading old package versions include dependency compatibility testing, rolling back to stable versions, and reproducing historical issues. It is recommended to always verify version numbers before installation to avoid dependency conflicts. Additionally, regularly updating the package source index (e.g., via Update-Package -Reinstall or similar commands) ensures access to the most current version information.