How to create a NuGet package

How to create a NuGet package

A few days ago I looked into setting up our private NuGet server as a way for us to share code, instead of using git sub modules for example.
Turns out setting up the server is as simple as starting an empty web project in visual studio and adding the NuGet server package:
On a side note, if you are already using team city, you can use this as a NuGet repository, just select create artifact with the package NuGet build step.

Getting started

So you have an awesome library you want to package and deploy so everyone can start using it. The first thing you want to do then is create a .nuspec file. Just add an empty text file or add a new XML file from visual studio. Use the same folder as your .csproj file and use the project name as a filename.

The file

An example file could be something like this:

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="">
  <!-- more info: -->
    <title>My awesome Library</title>
    <owners>University of Antwerp</owners>
    <repository type="git" url="" />
    <description>My awesome library, please use it everywhere.</description>
    <copyright>Copyright 2018 University of Antwerp</copyright>
      <group targetFramework=".NETFramework4.6">
        <dependency id="Newtonsoft.Json" version="11.0.2" />
        <dependency id="Microsoft.AspNet.WebApi.Client" version="5.2.6" />
      <frameworkAssembly assemblyName="System.Net.Http.Formatting" targetFramework=".NETFramework4.6" />
    <file src="bin\$configuration$\$id$.dll" target="lib\net46" />

I’ve used some variables ($variable$) as an example:

  • The $version$ for instance will insert the assembly version and use that as the package version.
  • $configuration$ will contain Release, Debug, …
  • $id$ is the project id/name.

There are 3 important groups to note:

  • The dependencies group is used to list NuGet dependencies if any.
  • The frameworkAssemblies are .NET assemblies required for this project.
  • The files lists the files to package. This is commented out because we will let NuGet together with the project information decide what to package.

Lets get packaging

Make sure you have installed NuGet:
Open a command window in the project/nuspec folder (right click with shift pressed, open command window here).

nuget pack -IncludeReferencedProjects -properties Configuration=Release

Ship it

You have your package, lets upload it to the server:

nuget push .\MyLibrary.1.0.0.nupkg apiKey -Source

That’s it.

Visual studio add feed

In visual studio you can add the NuGet server manually or you can include a config file in the root of your solution, to allow people who clone your project to have the new server(s) automatically configured.
An example nuget.config file:

<?xml version="1.0" encoding="utf-8"?>
    <add key="" value="" protocolVersion="3" />
    <add key="MyServer" value="" />
    <add key="enabled" value="True" />
    <add key="automatic" value="True" />
    <add key="skip" value="False" />
    <add key="format" value="0" />
    <add key="disabled" value="False" />

One thought on “How to create a NuGet package

  1. thank you writer. keep the spirit in writing I like this article is good to add knowledge that I did not know before. how else do you complete this process especially for beginners?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.