How to add a Nuget Package Using dotnet add

How to add a Nuget Package Using dotnet add

A feature I’ve long wished for in .NET Core and its command line interface (CLI) is the ability to manage nuget references from the command line. This becomes increasingly important as the final version of the tooling for .NET Core (and Visual Studio 2017) moves from a JSON-based project file to an XML-based one (for compatibility with the hundreds of other projects Visual Studio supports in that format). Adding references to project.json wasn’t too bad, but it’s ever-so-slightly more painful to do in XML, and I’ve grown increasingly fond of using the dotnet CLI, especially when I’m working with Visual Studio Code (and sometimes on macOS).

To check if you have the dotnet CLI installed correctly (get it here), run these commands in a new folder:

Sample output:

dotnet-cli

As of the latest release candidate of Visual Studio 2017 (or the latest SDK install of .NET Core), you can now use dotnet add to add packages and/or references to a project. You can view the built-in help by running these commands:

Sample output:

dotnet-add-cli

For example, if you want to add Entity Framework 6.1 to your .NET Core app, you would run this command (yes, you can use EF 6 from .NET Core!):

Alas, there’s no statement completion for the package name when you type it into the CLI. Sample output:

dotnet-add-ef

Having access to the CLI makes it much easier to write setup scripts, or to quickly add common packages to projects where you know the package name and can type reasonably quickly. If the command line isn’t your thing, don’t worry, you still have a bunch of other ways to work with packages, including editing the project file directly and using the GUI tools in VS2017 (or the built-in Package Manager Console there, and its PowerShell cmdlets).