How to create nuget package – Step by Step Guide

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:

  1. Open Visual Studio 2017
  2. Go to File -> New -> Project
  3. Choose Class Library (.NET Framework) or Class Library (.NET Standard)
    • Tip: .NET Standard is recommended for better compatibility
  4. 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)

  1. Right-click your project -> Properties
  2. Go to Package tab
  3. Fill in the metadata (Version, Authors, Description)
  4. 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.

  1. Create a folder: C:\LocalNugetFeed
  2. Copy your .nupkg file into that folder
  3. 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

  1. Sign in at nuget.org
  2. Go to Upload Package
  3. Upload the .nupkg file
  4. Confirm the package details
  5. Publish

Your package will be live within a few minutes.