How to create a NuGet package with .NET Core command-line interface (CLI) tools
The following shows command-line samples using Unix. The
dotnet pack command as shown here works the same way on Windows.
.NET Standard and .NET Core libraries are expected to be distributed as NuGet packages. This is in fact how all of the .NET Standard libraries are distributed and consumed. This is most easily done with the
dotnet pack command.
Imagine that you just wrote an awesome new library that you would like to distribute over NuGet. You can create a NuGet package with cross-platform tools to do exactly that! The following example assumes a library called SuperAwesomeLibrary that targets
If you have transitive dependencies, that is, a project that depends on another package, make sure to restore packages for the entire solution with the
dotnet restore command before you create a NuGet package. Failing to do so will result in the
dotnet pack command not working properly.
Starting with .NET Core 2.0 SDK, you don't have to run
dotnet restore because it's run implicitly by all commands that require a restore to occur, such as
dotnet build and
It's still a valid command in certain scenarios where doing an explicit restore makes sense, such as continuous integration builds in Azure DevOps Services or in build systems that need to explicitly control the time at which the restore occurs.
After ensuring packages are restored, you can navigate to the directory where a library lives:
Then it's just a single command from the command line:
Your /bin/Debug folder will now look like this:
$ ls bin/Debug netstandard1.0/ SuperAwesomeLibrary.1.0.0.nupkg SuperAwesomeLibrary.1.0.0.symbols.nupkg
This produces a package that is capable of being debugged. If you want to build a NuGet package with release binaries, all you need to do is add the
-c) switch and use
release as the argument.
dotnet pack --configuration release
Your /bin folder will now have a release folder containing your NuGet package with release binaries:
$ ls bin/release netstandard1.0/ SuperAwesomeLibrary.1.0.0.nupkg SuperAwesomeLibrary.1.0.0.symbols.nupkg
And now you have the necessary files to publish a NuGet package!
dotnet pack with
It is important to note that at no point is the
dotnet publish command involved. The
dotnet publish command is for deploying applications with all of their dependencies in the same bundle -- not for generating a NuGet package to be distributed and consumed via NuGet.