How do you create and publish your own NuGet package from a class library project?
Short answer
Set true (default for class libraries) along with package metadata (PackageId, Version, Authors, Description) in the .csproj, run `dotnet pack` to produce a .nupkg file, then publish it with `dotnet nuget push` to NuGet.org or a private feed using an API key.
Advertisement
AModel answer
Set <IsPackable>true</IsPackable> (default for class libraries) along with package metadata (PackageId, Version, Authors, Description) in the .csproj, run `dotnet pack` to produce a .nupkg file, then publish it with `dotnet nuget push` to NuGet.org or a private feed using an API key.
<PropertyGroup>
<PackageId>MyCompany.Utilities</PackageId>
<Version>1.2.0</Version>
<Authors>MyCompany</Authors>
<Description>Shared utility functions</Description>
</PropertyGroup>
dotnet pack -c Release
dotnet nuget push bin/Release/MyCompany.Utilities.1.2.0.nupkg --api-key <key> --source https://api.nuget.org/v3/index.jsonReal-world example
A company publishes an internal SharedValidation library to a private Azure Artifacts NuGet feed, letting every internal team consume it via a normal PackageReference instead of copying source code between repositories.
Likely follow-ups
- How does dotnet pack decide what files to include in the .nupkg?
- What's the difference between publishing to NuGet.org versus a private feed?
Related topics