Redigera

Dela via


Manage NuGet packages with the NuGet CLI

You can use the nuget.exe CLI tool to manage NuGet packages in Visual Studio projects and solutions. This article describes the most common NuGet CLI commands for managing NuGet packages. All these commands work on Windows, and most work on Mac and on Linux with Mono.

The NuGet CLI runs on .NET Framework and non-SDK-style projects, for example non-SDK style projects that target .NET Standard libraries. The NuGet CLI commands can use a project packages.config file that lists package references. For non-SDK-style projects that use PackageReference instead of packages.config for package references, use the dotnet CLI instead.

Note

For most non-SDK-style projects that use packages.config, it's best to migrate packages.config to PackageReference, and then use the dotnet CLI instead of the NuGet CLI to manage packages. However, you can't migrate C++ or ASP.NET projects.

For most commands, the NuGet CLI tool uses the current directory, unless you specify a different location in the command. To run NuGet CLI commands, open a command line and switch to the directory that contains your project file.

For a complete list of commands and their arguments, see the NuGet CLI reference.

Prerequisites

Download the NuGet CLI from nuget.org. Save the nuget.exe file to a suitable directory, and make sure the directory is in your PATH environment variable.

Note

You can also use the winget tool for Windows or Homebrew for macOS.

To find out your NuGet CLI version, open a command line and run nuget help, or to avoid having to scroll up, use nuget help | more. The first line in the help output shows the version.

Install a package

The NuGet CLI install command downloads and installs specified NuGet packages.

Important

The install command doesn't modify the project file or packages.config file. The install and restore commands only add packages to disk, but don't add dependencies to projects. To add project dependencies, add packages through the Visual Studio Package Manager UI or Package Manager Console, then run install or restore.

Use the -OutputDirectory option to install packages to a specific directory. If you don't specify an output directory, install uses the current directory.

nuget install <packageID | configFilePath> -OutputDirectory <outputDirectory>

For example, to install the Newtonsoft.json package to the packages subdirectory, use the following command:

nuget install Newtonsoft.Json -OutputDirectory packages

Instead of specifying a package to install, you can specify an existing packages.config file in the current or another directory. The install command installs all the packages listed in the packages.config file.

nuget install packages.config

For example, the following command installs all the packages listed in packages.config in the config subdirectory to the packages subdirectory:

nuget install config\packages.config -OutputDirectory packages

Install a specific version of a package

The install command installs the latest version of a package unless you specify a different version. To install a specific version of a package, use the -Version option:

nuget install <packageID | configFilePath> -Version <version>

For example, to install version 12.0.1 of the Newtonsoft.json package, use:

nuget install Newtonsoft.Json -Version 12.0.1

List packages

Use the list command to display a list of packages installed in the packages folders. Use the -Source option to restrict the list.

nuget list -Source <source>

For example, to list packages in the packages subdirectory of MyProject, use:

nuget list -Source C:\Users\%USERNAME%\source\repos\MyProject\packages

You can also use a search term to search for package names, tags, or descriptions:

nuget list <"search term"> -Source <source>

Update all packages

Use the update command to update all packages in a project packages.config file to their latest available versions. It's best to run restore before you run update.

nuget update

Remove a package

To remove a package, delete that package from the project folder. To reinstall packages, use the restore or install commands.

Deleting packages from disk doesn't update the project, packages.config, or NuGet.Config files. The best way to remove packages is through the Visual Studio Package Manager UI or Package Manager Console.

Restore packages

The NuGet CLI restore command downloads and installs any missing packages. The command works on projects that use either PackageReference or packages.config for package references.

Like install, the restore command only adds packages to disk, but doesn't modify the project file or packages.config. To add project dependencies, use the Visual Studio Package Manager UI or Console.

To restore packages, run the following command:

nuget restore <projectPath>

The restore command uses a solution file or a package.config file in the specified project path.

For example, to restore all packages for MySolution.sln in the current directory, run:

nuget restore MySolution.sln

Note

For non-SDK-style projects that use PackageReference, use msbuild -t:restore to restore packages instead.

For more information, see Restore packages.

Next steps