dotnet-add-package
Add a NuGet package to a project
TLDR
Add a package to the project in the current directory
Add a package to a specific project
Add a specific version of a package to the project
Add a package using a specific NuGet source
Add a package only when targeting a specific framework
Add and specify the directory where to restore packages (~/.nuget/packages by default)
SYNOPSIS
dotnet add [PROJECT] package <PACKAGE_NAME> [options]
PROJECT: The project file or directory to add the package to. If omitted, the command searches the current directory for a project file.
PACKAGE_NAME: The name of the NuGet package to add.
PARAMETERS
Specifies the project file or directory to operate on. If not specified, the command searches the current directory.
The name of the NuGet package to add.
--version
Specifies the exact version of the package to add. If not provided, the latest stable version is used.
--framework
Adds the package reference only when targeting a specific framework (e.g., 'net6.0', 'netcoreapp3.1').
--source
Specifies the NuGet package source URL to use during the restore operation. Can be a path or a URL.
--no-restore
Skips the implicit restore step after adding the package reference. Useful when managing multiple package additions before a single restore.
--interactive
Allows the command to stop and wait for user input or action (e.g., for authentication).
--prerelease
Allows the latest prerelease version of the package to be added if no specific version is provided via --version.
--configfile
The NuGet configuration file (NuGet.Config) to use. If specified, only settings from this file will be used.
DESCRIPTION
The dotnet add package command provides a convenient way to add a NuGet package reference to a .NET project file. When executed, it inserts a <PackageReference> item into the specified project file (e.g., .csproj, .fsproj). By default, the command automatically resolves and adds the latest stable version of the specified package, and then performs a package restore operation to download and install its dependencies. This command streamlines the process of integrating external libraries and frameworks into your .NET applications, eliminating the need for manual project file editing. You can specify a particular package version, target a specific framework, or define a custom NuGet package source using its various options.
CAVEATS
This command modifies the project file (e.g., .csproj) directly.
It requires a valid .NET project file to operate. If no project is found in the current directory or specified, the command will fail.
By default, a package restore operation is performed, which might take time depending on network conditions and the number of dependencies. Use --no-restore to skip this step.
The command only adds <PackageReference> items; it does not manage global tools (use dotnet tool install) or local tools.
Version resolution defaults to the latest stable version. To add a prerelease version without specifying its exact number, use --prerelease.
IMPLICIT RESTORE
After adding a package reference, the command automatically performs a 'dotnet restore' operation to download the package and its dependencies. This ensures your project can immediately build and run with the new package. If you intend to add multiple packages in sequence, consider using the --no-restore option and then manually running dotnet restore once all packages have been added.
PROJECT FILE MODIFICATION
This command modifies the specified project file (e.g., 'MyProject.csproj') by adding an <PackageReference Include="PackageName" Version="1.0.0" /> XML element. It handles the correct placement within the file, making manual XML editing unnecessary and less error-prone.
SPECIFYING FRAMEWORKS
When developing multi-targeted applications, you can use the --framework option to add a package reference that is conditional on a specific target framework. For example, dotnet add package MyLib --framework net6.0 will add the package only for the 'net6.0' target framework, allowing different versions or packages for other frameworks.
HISTORY
The dotnet add package command is an integral part of the .NET Core CLI, which was introduced to streamline development workflows for .NET Core applications. It emerged as the primary command-line interface for managing NuGet package dependencies, replacing older methods like direct XML editing of .csproj files or reliance on third-party tools like Paket for this specific task. Its development focused on providing a consistent and intuitive experience for developers working across different operating systems, including Linux. It became particularly crucial with the shift from project.json to .csproj files for package management, offering a simple verb to interact with the new <PackageReference> format. Since its inception, it has been continually refined as part of the broader .NET SDK releases, ensuring compatibility with new .NET versions and features.
SEE ALSO
dotnet restore(1), dotnet remove package(1), dotnet list package(1), dotnet build(1), dotnet publish(1), nuget(1)