LinuxCommandLibrary

dotnet-add-package

Add a NuGet package to a project

TLDR

Add a package to the project in the current directory

$ dotnet add package [package]
copy

Add a package to a specific project
$ dotnet add [path/to/file.csproj] package [package]
copy

Add a specific version of a package to the project
$ dotnet add package [package] [[-v|--version]] [1.0.0]
copy

Add a package using a specific NuGet source
$ dotnet add package [package] [[-s|--source]] [https://api.nuget.org/v3/index.json]
copy

Add a package only when targeting a specific framework
$ dotnet add package [package] [[-f|--framework]] [net7.0]
copy

Add and specify the directory where to restore packages (~/.nuget/packages by default)
$ dotnet add package [package] --package-directory [path/to/directory]
copy

SYNOPSIS

dotnet add [PROJECT] package [PACKAGE_NAME] [options]

PARAMETERS

PACKAGE_NAME
    Name of the NuGet package to add (positional argument).

--version <VERSION> (-v)
    Version of the package (e.g., 1.2.3). Defaults to latest stable.

--prerelease
    Include prerelease packages during resolution.

--source <SOURCE>
    NuGet source/feed URL for the package.

--no-restore
    Skip automatic dotnet restore after adding.

--project <PROJECT>
    Path to project file. Defaults to current directory.

--interactive (-i)
    Pause for user input (e.g., credentials).

--verbosity <LEVEL>
    Output verbosity: quiet|minimal|normal|detailed|diagnostic.

DESCRIPTION

The dotnet add package command is part of the .NET CLI, used to add a NuGet package reference directly to a .NET project file such as *.csproj or *.fsproj. It modifies the project file by inserting a element, specifying the package ID and version.

Run it from a project directory: dotnet add package PackageName fetches the latest stable version from nuget.org by default. Options allow customization, like pinning a version with --version, enabling prereleases with --prerelease, or using a custom feed via --source. By default, it triggers dotnet restore to download dependencies, but --no-restore skips this.

This streamlines dependency management in SDK-style projects, avoiding manual XML edits. It supports multi-targeting frameworks and integrates with Visual Studio, VS Code, and CI systems. Works cross-platform on Linux, macOS, and Windows with .NET SDK installed.

Ideal for developers managing libraries like Entity Framework or logging packages in console apps, libraries, or web projects.

CAVEATS

Requires .NET SDK. Fails if no project file found. Does not handle transitive dependencies automatically beyond restore.

EXAMPLES

dotnet add package Newtonsoft.Json
dotnet add package Microsoft.Extensions.Logging --version 7.0.0 --prerelease
dotnet add MyApp.csproj package Serilog --source https://api.myfeed.com/nuget

GLOBAL OPTIONS

Inherits dotnet options like --framework <TFM> (e.g., net8.0), --dry-run.

HISTORY

Introduced in .NET Core SDK 1.0 (2016) for project.json, evolved with SDK 2.0 (2017) for MSBuild integration. Now core to .NET 8+ dependency workflows.

SEE ALSO

dotnet remove package, dotnet list package, dotnet restore

Copied to clipboard