Working With Nuget Packages

How to work with Nuget packages in .NET

Posted by Marcelo on June 13, 2024

Using .NET CLI

List Installed Nuget Packages

You can list all the Nuget packages installed with the "dotnet list packages" command:

dotnet list package

List Outdated Nuget Packages

To see only the outdated packages, use the --outdated option:

dotnet list package --outdated

List Outdated Nuget Packages

Append the "--include-prerelease" option to consider prerelease versions:

dotnet list package --include-prerelease

Add Nuget Packages

To add a new package, use the "dotnet add package" command:

dotnet add package <package_name>

It automatically installs the latest version of the package, but if you need a specific version of a package, append the -v or --version switch to the command:

dotnet add package <package_name> -v <version>

Sometimes you need to specify the source of the Nuget package:

dotnet add package <package_name> --source https://api.nuget.org/v3/index.json

Update Nuget Packages

To update a Nuget package, use the "dotnet add package" as well.

dotnet add package <package_name>

Restore Packages

The "dotnet run" and "dotnet build" commands automatically restores packages, but if you want explicitly restore them use the "dotnet restore" command:

dotnet restore

Remove Packages

dotnet remove package <package_name>

Using VS Code Extensions

There are some extensions that allows you to manage the Nuget packages directly from Visual Studio Code. I use this one: NuGet Package Manager GUI

Among many features, it allows you to add, update and remove multiple packages at once.

You can install it from the VS Code Marketplace NuGet Package Manager GUI.

To use it, open the Command Palette (Ctrl+Shift+P on Windows or Command+Shift+P on Mac) and then select > NuGet Package Manager GUI

To update all packages:

  1. Click Load Package Versions
  2. Click Update All Packages

References

Add a NuGet source

Create Packages

Reinstall and update NuGet packages in Visual Studio

Nuget Package Manager GUI extension