Install and manage packages using the dotnet CLI
The CLI tool allows you to easily install, uninstall, and update NuGet packages in projects and solutions. It runs on Windows, Mac OS X, and Linux.
The dotnet CLI is for use in your .NET Core and .NET Standard project (SDK-style project types), and for any other SDK-style projects (for example, an SDK-style project that targets .NET Framework). For more information, see SDK attribute.
This article shows you basic usage for a few of the most common dotnet CLI commands. For most of these commands, the CLI tool looks for a project file in the current directory, unless a project file is specified in the command (the project file is an optional switch). For a complete list of commands and the arguments you may use, see the .NET Core command-line interface (CLI) tools.
Prerequisites
- The .NET Core SDK, which provides the
dotnetcommand-line tool. Starting in Visual Studio 2017, the dotnet CLI is automatically installed with any .NET Core related workloads.
Install a package
dotnet add package adds a package reference to the project file, then runs dotnet restore to install the package.
Open a command line and switch to the directory that contains your project file.
Use the following command to install a NuGet package:
dotnet add package <PACKAGE_NAME>For example, to install the
Newtonsoft.Jsonpackage, use the following commanddotnet add package Newtonsoft.JsonAfter the command completes, look at the project file to make sure the package was installed.
You can open the
.csprojfile to see the added reference:<ItemGroup> <PackageReference Include="Newtonsoft.Json" Version="12.0.1" /> </ItemGroup>
Install a specific version of a package
If the version is not specified, NuGet installs the latest version of the package. You can also use the dotnet add package command to install a specific version of a NuGet package:
dotnet add package <PACKAGE_NAME> --version <VERSION>
For example, to add version 12.0.1 of the Newtonsoft.Json package, use this command:
dotnet add package Newtonsoft.Json --version 12.0.1
List package references
You can list the package references for your project using the dotnet list package command.
dotnet list package
Remove a package
Use the dotnet remove package command to remove a package reference from the project file.
dotnet remove package <PACKAGE_NAME>
For example, to remove the Newtonsoft.Json package, use the following command
dotnet remove package Newtonsoft.Json
Update a package
NuGet installs the latest version of the package when you use the dotnet add package command unless you specify the package version (-v switch).
Restore packages
Use the dotnet restore command, which restores packages listed in the project file (see PackageReference). With .NET Core 2.0 and later, restore is done automatically with dotnet build and dotnet run. As of NuGet 4.0, this runs the same code as nuget restore.
As with the other dotnet CLI commands, first open a command line and switch to the directory that contains your project file.
To restore a package using dotnet restore:
dotnet restore