Skip to content

NuGet

NuGet is the package manager for the Microsoft development platform, providing tools for creating, publishing, and consuming packages in .NET applications.

Installation

# Using Chocolatey (Windows)
choco install nuget.commandline

# Using apt (Ubuntu/Debian)
sudo apt-get install nuget

# Download directly
# https://www.nuget.org/nuget.exe

# Via .NET CLI
dotnet tool install -g nuget

Basic Commands

# Add package to project
dotnet add package Newtonsoft.Json

# Remove package
dotnet remove package Newtonsoft.Json

# Update package
dotnet package update Newtonsoft.Json

# List packages
dotnet list package

# List outdated packages
dotnet list package --outdated

# Clear NuGet cache
dotnet nuget locals all --clear

Project File (.csproj)

Basic Package Reference

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net7.0</TargetFramework>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
    <PackageReference Include="Serilog" Version="3.0.1" />
    <PackageReference Include="Entity Framework" Version="6.4.4" />
  </ItemGroup>

</Project>

Package Management

# Restore NuGet packages
dotnet restore

# Build project (includes restore)
dotnet build

# Install package
nuget install Newtonsoft.Json

# Update all packages
nuget update

# Update specific package
dotnet add package Newtonsoft.Json -v 13.0.3

# Install from specific source
dotnet add package Newtonsoft.Json --source https://api.nuget.org/v3/index.json

Version Constraints

|Pattern|Example|Meaning| |---------|-------------| |Exact|1.0.0|Exact version only| |Floating Min|[1.0]|1.0.0 and above| |Range|[1.0, 2.0)|Between 1.0 and 2.0 (exclusive)| |Prerelease|1.0.0-beta|Beta version|

NuGet Configuration

nuget.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
    <add key="my-feed" value="https://mycompany.pkgs.visualstudio.com/_packaging/MyFeed/nuget/v3/index.json" />
  </packageSources>

  <packageSourceCredentials>
    <my-feed>
      <add key="Username" value="myusername" />
      <add key="ClearTextPassword" value="mytoken" />
    </my-feed>
  </packageSourceCredentials>

  <config>
    <add key="repositoryPath" value="./packages" />
  </config>
</configuration>

Creating Packages

.nuspec File

<?xml version="1.0"?>
<package>
  <metadata>
    <id>MyPackage</id>
    <version>1.0.0</version>
    <title>My Package</title>
    <authors>Author Name</authors>
    <description>Package description</description>
    <projectUrl>https://github.com/user/repo</projectUrl>
    <license type="expression">MIT</license>
    <releaseNotes>Initial release</releaseNotes>
    <dependencies>
      <dependency id="Newtonsoft.Json" version="13.0.3" />
    </dependencies>
  </metadata>
  <files>
    <file src="bin/Release/net7.0/MyPackage.dll" target="lib/net7.0" />
  </files>
</package>

Create Package

# Pack from .csproj
dotnet pack

# Pack with options
dotnet pack -c Release -o ./nupkg

# Pack from .nuspec
nuget pack MyPackage.nuspec

# Specify version
nuget pack MyPackage.nuspec -Version 1.0.0

Publishing Packages

# Push to NuGet.org
dotnet nuget push ./bin/Release/MyPackage.1.0.0.nupkg --api-key YOUR_API_KEY

# Push to private feed
nuget push MyPackage.1.0.0.nupkg -Source https://mycompany.pkgs.visualstudio.com/_packaging/MyFeed/nuget/v3/index.json -ApiKey AzureArtifacts

# List package
nuget list

# Delete package
nuget delete MyPackage 1.0.0 -Source https://api.nuget.org/v3/index.json

Dependency Management

# Show dependency tree
dotnet list package --format json

# Check for vulnerabilities
dotnet list package --vulnerable

# Outdated packages
dotnet list package --outdated --include-prerelease

# Transitive dependencies
dotnet list package --include-transitive

Troubleshooting

# Clear NuGet cache
dotnet nuget locals all --clear

# Rebuild solution
dotnet clean
dotnet build

# Force update packages
dotnet package update --force

# Check package source
dotnet nuget list source

# Add package source
dotnet nuget add source https://api.nuget.org/v3/index.json -n nuget.org

# Remove package source
dotnet nuget remove source nuget.org

CI/CD Integration

GitHub Actions

name: Publish NuGet
on:
  push:
    tags:
      - 'v*'

jobs:
  publish:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-dotnet@v1
        with:
          dotnet-version: '7.0.x'
      - run: dotnet restore
      - run: dotnet build -c Release
      - run: dotnet pack -c Release -o ./nupkg
      - run: dotnet nuget push ./nupkg/*.nupkg -k ${{ secrets.NUGET_API_KEY }}

Best Practices

  • Pin exact versions for stability
  • Review package updates regularly
  • Check for security vulnerabilities
  • Use semantic versioning for packages
  • Document package dependencies
  • Test updates before deployment
  • Maintain private NuGet feeds for internal packages
  • Keep package metadata updated

Resources


Last updated: 2025-07-06|Edit on GitHub