Επεξεργασία

Κοινή χρήση μέσω


Install and manage NuGet packages with the dotnet CLI

You can use the dotnet CLI tool on Windows, macOS, or Linux to easily install, uninstall, and update NuGet packages in .NET projects and solutions. This article describes the most common dotnet CLI commands for managing NuGet packages.

The dotnet CLI runs on .NET, .NET Core, .NET Standard SDK-style projects, and any other SDK-style projects, for example those that target .NET Framework. For more information, see .NET project SDKs.

For most commands, the CLI tool looks for a project file in the current directory, unless a different project file is specified as an optional switch in the command. For a complete list of commands and their arguments, see dotnet CLI commands.

Prerequisites

  • The .NET Core SDK, which provides the dotnet command-line tool. Starting in Visual Studio 2017, the dotnet CLI automatically installs with all .NET and .NET Core related workloads.

Install or update a package

The dotnet add package command adds a package reference to the project file, and then runs dotnet restore to install the package.

  1. Open a command line and switch to the directory that contains your project file.

  2. Use the following command to install a NuGet package:

    dotnet add package <PACKAGE_NAME>
    

    For example, to install the Newtonsoft.Json package, use the following command

    dotnet add package Newtonsoft.Json
    
  3. After the command completes, you can open the project file to see the package reference.

    For example, open the .csproj file to see the added Newtonsoft.Json package reference:

    <ItemGroup>
      <PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
    </ItemGroup>
    

Install a specific version of a package

The dotnet add package command installs the latest version of the package unless you specify a different version.

To install a specific version of a NuGet package, use the optional -v or --version switch:

dotnet add package <PACKAGE_NAME> -v <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

List the package references and versions for your project by 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

Restore packages

The dotnet restore command restores packages that the project file lists with <PackageReference>. For more information, see PackageReference in project files.

.NET Core 2.0 and later dotnet build and dotnet run commands restore packages automatically. As of NuGet 4.0, dotnet restore runs the same code as nuget restore.

To restore a package with dotnet restore:

  1. Open a command line and switch to the directory that contains your project file.
  2. Run dotnet restore.

Next steps