To publish a .NET class library to NuGet.org, follow this streamlined workflow using the nuget.exe CLI tool. While modern .NET SDKs support dotnet pack and dotnet nuget push, this guide focuses on the classic nuget.exe approach for clarity and compatibility with older projects.
Create and Prepare the Library
Start by creating a standard .NET Class Library project (e.g., MyUtilityLibrary.csproj). Ensure versioning is correctly set in the project file or via <Version> and <AssemblyVersion> properties — avoid relying solely on AssemblyInfo.cs in SDK-style projects.
Register and Acquire nuget.exe
1. Sign up at NuGet.org and verify your email.
2. Download the latest nuget.exe from https://www.nuget.org/downloads.
3. Place nuget.exe in the same directory as your .csproj file.
Generate and Customize the .nuspec Manifest
From the project root, run:
nuget spec MyUtilityLibrary.csproj
This generates MyUtilityLibrary.nuspec. Edit it to define metadata — for example:
<?xml version="1.0" encoding="utf-8"?>
<package>
<metadata>
<id>MyUtilityLibrary</id>
<version>1.2.0</version>
<title>My Utility Library</title>
<authors>YourName</authors>
<owners>YourName</owners>
<projectUrl>https://github.com/yourname/myutilitylibrary</projectUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>A lightweight helper library for common .NET tasks.</description>
<releaseNotes>Added async validation methods.</releaseNotes>
<copyright>Copyright © 2024</copyright>
<tags>utility helpers extensions</tags>
</metadata>
</package>
Build the Package
Run:
nuget pack MyUtilityLibrary.csproj -Properties Configuration=Release
This produces a .nupkg file (e.g., MyUtilityLibrary.1.2.0.nupkg) using the project’s build output and the .nuspec metadata.
Authenticate and Publish
1. Retrieve your API key from https://www.nuget.org/account/apikeys.
2. Store it locally (optional but recommended):
nuget setApiKey abc123def456... -Source https://api.nuget.org/v3/index.json
3. Push the package:
nuget push MyUtilityLibrary.1.2.0.nupkg -Source https://api.nuget.org/v3/index.json -ApiKey abc123def456...
Note: Use https://api.nuget.org/v3/index.json (not the legacy /packages endpoint) for v3 API compliance.
Post-Publish Notes
After upload, the package appears under your profile immediately but may take 1–5 minutes to become searchable in Visual Studio’s NuGet Package Manager. If the CLI push fails due to network or auth issues, manually upload the .nupkg via the "Upload Package" button on your NuGet.org dashboard.
Self-Hosted Alternative
For private package hosting, consider setting up a lightweight NuGet server using NuGet.Server (ASP.NET Framework) or NuGet Gallery for full-featured scenarios.