Skip to content

/commands/nuget/dotnet-add-package-version

dotnet add package --version: install a specific NuGet package version

verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash)updated
tool
NuGet
versions
.NET 8, .NET 9, .NET 10
os
linux, macos, windows

dotnet add package Newtonsoft.Json --version 13.0.3 adds a PackageReference with exactly that version to the project file and restores it. Without --version, the command picks the latest stable version. Running it again with another version updates the existing reference, so the same command upgrades and downgrades. In the .NET 10 SDK the preferred spelling is dotnet package add; dotnet add package still works.

Add a pinned version

bash
dotnet new classlib -o Lib > /dev/null
cd Lib
dotnet add package Newtonsoft.Json --version 13.0.1 > /dev/null
grep PackageReference Lib.csproj
dotnet add package Newtonsoft.Json --version 13.0.3 > /dev/null
grep PackageReference Lib.csproj
dotnet list package | grep Newtonsoft

Output

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

The second command changed the version in place instead of adding a second reference. dotnet list package shows the requested version and the version that restore resolved; they match for a plain version number.

Version syntax

Version attributeMeaning in NuGet
13.0.3Minimum version 13.0.3; NuGet picks the lowest version that satisfies it, so in practice exactly 13.0.3 if it exists
[13.0.3]Exactly 13.0.3, nothing else
[13.0,14.0)At least 13.0, below 14.0
13.*Floating: the highest 13.x available at restore time
*-*Floating, including prereleases (use with care)
9.0.0-preview.1A specific prerelease

Unlike npm, a bare version in NuGet is a minimum, but because NuGet resolves the lowest applicable version it behaves like a pin as long as nothing else needs a higher one.

Options

CommandWhat it does
dotnet add package SerilogLatest stable version
dotnet add package Serilog --version 4.0.0A specific version
dotnet add package Serilog --prereleaseLatest version including prereleases
dotnet add src/App package SerilogTarget a project that is not in the current folder
dotnet add package Serilog -s https://my.feed/v3/index.jsonUse a specific package source
dotnet add package Serilog -nOnly edit the project file, skip the restore (--no-restore)
dotnet add package Serilog -f net8.0Only for one target framework
dotnet remove package SerilogRemove the reference

Pitfalls

  • Version does not exist. A typo in the version fails with NU1102 Unable to find package with version; a typo in the name with NU1101.
  • Central Package Management. With Directory.Packages.props and ManagePackageVersionsCentrally, versions live in that file. dotnet add package then writes the version there and adds a reference without Version to the project.
  • Downgrades. Lowering a version below what another dependency needs fails with NU1605 detected package downgrade.
  • Private feeds. The command uses the sources in nuget.config; see package sources in nuget.config when the package lives on an internal feed.

How to confirm it worked

grep PackageReference *.csproj (or opening the file) shows the version, and dotnet list package shows requested and resolved versions. dotnet list package --include-transitive adds the packages it pulled in.

FAQ

How do I install a prerelease version?

Name it with --version, for example --version 9.0.0-preview.1, or pass --prerelease to let dotnet add package pick the newest version including prereleases.

How do I downgrade a NuGet package?

Run dotnet add package with the older --version; it rewrites the Version attribute. If another package needs a higher version, the restore fails with NU1605 (detected package downgrade) and you must downgrade or upgrade them together.

What is the difference between dotnet add package and Install-Package?

Install-Package is the Package Manager Console command inside Visual Studio. dotnet add package is the cross-platform CLI equivalent; both end up as a PackageReference in the project file.

Sources