If you’re building reusable components in .NET, one of the best ways to share them, across multiple projects or with the community, is by creating a NuGet package.
Whether you want to publish your package to nuget.org or use it privately within your team, this guide will walk you through the complete process of creating a NuGet package from scratch.
Step 1: Create a Class Library Project
Start by creating a new .NET project:
- Open Visual Studio 2017
- Go to File -> New -> Project
- Choose Class Library (.NET Framework) or Class Library (.NET Standard)
- Tip: .NET Standard is recommended for better compatibility
- Give your project a meaningful name (e.g.,
MyCoolLibrary)
Add the classes or functionality you want your package to provide.
namespace MyCoolLibrary
{
public class Greeter
{
public string SayHello(string name)
{
return $"Hello, {name}!";
}
}
}
Step 2: Add a .nuspec File (Optional with .NET Framework)
If you’re using a .NET Standard library or the SDK-style project, you don’t need a .nuspec file. your .csproj will contain the metadata.
But if you prefer a .nuspec file, here’s what it looks like:
<?xml version="1.0"?>
<package >
<metadata>
<id>MyCoolLibrary</id>
<version>1.0.0</version>
<title>My Cool Library</title>
<authors>Your Name</authors>
<owners>Your Name</owners>
<licenseUrl>http://opensource.org/licenses/MIT</licenseUrl>
<projectUrl>http://example.com</projectUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>A simple library that says hello.</description>
<tags>hello sample library</tags>
</metadata>
</package>
Step 3: Add NuGet Metadata to Your Project (.NET Standard Way)
For SDK-style projects, just edit your .csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<PackageId>MyCoolLibrary</PackageId>
<Version>1.0.0</Version>
<Authors>Your Name</Authors>
<Description>A simple library that says hello.</Description>
<PackageTags>hello sample library</PackageTags>
</PropertyGroup>
</Project>
Step 4: Build the NuGet Package
You can create the package in two ways:
A. Using Visual Studio (Easy Method)
- Right-click your project -> Properties
- Go to Package tab
- Fill in the metadata (Version, Authors, Description)
- Check “Generate NuGet package on build”
Then build the project.
Your .nupkg file will be created in:
bin/Debug/
bin/Release/
B. Using .NET CLI (Recommended)
Run:
dotnet pack --configuration Release
You’ll find the .nupkg file under:
bin/Release/
Step 5: Test Your Package Locally (Important!)
Before publishing, test your package by creating a new sample project.
- Create a folder:
C:\LocalNugetFeed - Copy your
.nupkgfile into that folder - In Visual Studio:
- Go to Tools -> Options -> NuGet Package Manager -> Package Sources
- Add a new source pointing to the folder
Now install your package just like any other:
Install-Package MyCoolLibrary
If it installs and works, you’re good to go.
Step 6: Publish Your Package
- Sign in at nuget.org
- Go to Upload Package
- Upload the
.nupkgfile - Confirm the package details
- Publish
Your package will be live within a few minutes.