Build and Test a NuGet Package Locally
Use dotnet pack to build a NuGet package on your machine, serve it from a local feed, and test it in another project before publishing.
Before you push a package to NuGet.org, you should build it, install it, and use it somewhere real. Publishing is
permanent, and a broken package ID, a missing dependency, or a messed-up readme that renders incorrectly is miserable to
discover after the fact. You can do the entire loop on your own machine: dotnet pack builds a .nupkg, a folder acts
as a local feed, and another project restores from that feed exactly like it would from nuget.org.
Let’s walk that loop end to end, so you can iterate on a package before anyone else ever sees it.
What “building a NuGet package locally” means
A NuGet package has the extension .nupkg, which is a zip archive that holds your compiled assemblies (the .dll
files), a manifest of metadata, and anything else you choose to ship, like a readme or static assets. So a package is not a DLL;
it’s a container that usually wraps one or more DLLs plus the information NuGet needs to install them correctly.
Building a package “locally” means two things working together:
- Produce the
.nupkgfrom your project withdotnet pack. - Serve it from a local feed, which is any folder on disk that you register as a package source, so another project can restore it.
Rest assured, nothing here touches nuget.org until you decide. You’re rehearsing the real thing.
Build the package with dotnet pack
Start with the project you want to ship. A class library is the common case. The package’s identity and metadata live in
the .csproj, so set them there rather than passing a wall of command-line flags:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <TargetFramework>net8.0</TargetFramework> <PackageId>Contoso.Widgets</PackageId> <Version>1.0.0</Version> <Authors>Your Name</Authors> <Description>A small library of widget helpers.</Description> </PropertyGroup>
</Project>Give the library a type worth calling, so the consumer project has something to exercise. Add a Widget.cs alongside the .csproj:
namespace Contoso.Widgets;
public class Widget(string name){ public string Describe() => $"Widget: {name}";}Now build the package:
dotnet pack --configuration ReleaseBy default, this drops Contoso.Widgets.1.0.0.nupkg into bin/Release. In the next section we’ll send it to a dedicated feed folder instead, so another project can restore it.
A few things worth knowing up front:
PackageIdmust be unique on any feed you publish to, so check the name isn’t already taken before you settle on it. For local testing you can name it whatever you like.- The version is part of the file name and the restore cache key. This matters more than it looks, as you’ll see in a moment.
dotnet packbuilds first by default. If you’ve already built,--no-buildskips the recompile.
Set up a local NuGet feed
A local feed is just a folder. The cleanest way to wire one up is a nuget.config next to your solution file. NuGet resolves a relative value against the config file’s location, not your current directory, so the feed keeps working no matter which project folder you run commands from, and it’s safe to commit:
<?xml version="1.0" encoding="utf-8"?><configuration> <packageSources> <add key="local-feed" value="./artifacts"/> </packageSources></configuration>Pack the library into that folder. Name the .csproj so dotnet pack targets the project directly instead of the solution, since solution-level packing doesn’t support --output:
dotnet pack Contoso.Widgets/Contoso.Widgets.csproj --configuration Release --output ./artifactsPrefer a machine-wide feed that every project on the box can see? Register it once with an absolute path, because NuGet resolves a relative one against your current directory:
dotnet nuget add source /path/to/artifacts --name local-feedList your sources any time to confirm the feed is registered:
dotnet nuget list sourceConsume the local package in another project
With the nuget.config in place, a normal add package works from the consumer project, because NuGet already knows about the local-feed source:
dotnet add package Contoso.Widgets --version 1.0.0Skipped the config and just want the feed for this one command? Point at the folder with an absolute path:
dotnet add package Contoso.Widgets --version 1.0.0 --source /path/to/artifactsRestore, build, and call into the library to prove it actually works:
using Contoso.Widgets;
var widget = new Widget("gadget");Console.WriteLine(widget.Describe());One gotcha trips up almost everyone. NuGet caches restored packages in a global folder, keyed by package ID and version. If you repack without bumping the version, the consumer keeps using the cached copy and never sees your change. You have two ways out:
- Bump the version every time (
1.0.1,1.0.2, and so on). It’s clean, and it mirrors real publishing. - Clear the cached package when you must reuse a version:
dotnet nuget locals global-packages --clearBumping the version is the habit worth building; clearing the cache is what you reach for when you forget.
Iterate quickly with symbols
While you’re testing locally, you want to step into the library from the consumer and read the original source. A .snupkg symbol package is built for symbol servers like nuget.org’s, not a local folder feed, so it won’t help you here. For local iteration, embed the symbols and sources directly in the assembly instead. Set this in the library’s .csproj:
<PropertyGroup> <DebugType>embedded</DebugType> <EmbedAllSources>true</EmbedAllSources></PropertyGroup>Repack, bump the version, and restore in the consumer. Because the PDB and the source now travel inside the DLL, your debugger can step straight into the library with no symbol server involved. It’s the fastest way to find out why a method behaves differently once it’s packaged than it did in the same solution.
When you’re ready to publish
Once the package installs cleanly and the consumer project builds and runs, you’ve validated the part that local testing can prove. The remaining steps (wiring up versioning, SourceLink, a CI pipeline, and the actual push to nuget.org) are a separate job. My full guide to creating a NuGet package in .NET covers that path from an empty folder to a published package.
Two things are worth doing before you publish, both of which you can check locally first:
- Add a readme so your landing page explains the library. See adding a readme to NuGet package landing pages.
- Know what kind of package you’re shipping. A plain library behaves differently from a tool or an analyzer. Here are five common kinds of NuGet packages.
FAQ
Is a NuGet package a DLL?
No. A NuGet package is a .nupkg file, a zip archive that usually contains one or more DLLs plus a metadata manifest.
The DLL is the compiled code; the package is the container that installs it.
How do you make a NuGet package?
Set the package metadata (PackageId, Version, Description) in your .csproj, then run
dotnet pack --configuration Release. That produces a .nupkg you can install from a local feed or publish to
nuget.org.
How do I install a .nupkg file locally?
Put the .nupkg in a folder, register that folder as a source with an absolute path using
dotnet nuget add source /path/to/folder --name local-feed, then run dotnet add package <PackageId> --version <version> from
the consumer project.
Why doesn’t my consumer project see my code changes?
NuGet caches packages by ID and version, so repacking the same version reuses the cached copy. Bump the version, or
clear the cache with dotnet nuget locals global-packages --clear.
Around the web
Comments
Loading comments from GitHub Discussions…