Skip to content
Latchkey

dotnet add package: Add a NuGet Dependency

dotnet add package inserts or updates a PackageReference in a project file and restores it.

add package is the scripted way to manage dependencies. In CI it is most often used to pin or bump a version reproducibly.

What it does

dotnet add package adds a <PackageReference> to the project (or updates the version if it already exists) and runs a restore to resolve it. Without --version it picks the latest stable version from the configured sources.

Common usage

Terminal
dotnet add package Newtonsoft.Json --version 13.0.3
dotnet add ./src/Api/Api.csproj package Serilog
dotnet add package MyLib --source https://my.feed/index.json

Options

FlagWhat it does
--version <ver>Pin a specific package version
-s, --source <url>Resolve the package from a specific feed
--no-restoreAdd the reference without restoring
-f, --framework <tfm>Add the reference only for one target framework
--prereleaseAllow a prerelease version

In CI

Always pass --version in automation so a build is reproducible; an unpinned add picks "latest" and changes over time. If you are scripting many adds, use --no-restore on all but the last to avoid restoring repeatedly. With central package management (Directory.Packages.props) the version comes from there, not from --version.

Common errors in CI

NU1101 ("Unable to find package X") means the package or version is not on a configured source. NU1605 ("Detected package downgrade") means the version you added is lower than a transitive requirement; pin the higher version or add an explicit reference. NU1010 (central package management) means the version was given on the reference instead of in Directory.Packages.props.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →