How to build and share your own Nuget packages

If you write code, you've probably had to share your work with others; and if you work with the .NET framework, and build or have built re-usable .NET packages, then you've had to share your libraries.

If you're a company building stuff using C#, VB.NET or some other .NET tool, you probably split your application into reusable projects/packages; Which means you need a way to distribute packages among developers.

Nuget Logo

Nuget is the package manager for the Microsoft development platform.

  • If you speak Node, then Nuget is the NPM for Microsoft technologies.
  • If you speak Python, then Nuget is the PIP ) for Microsoft technologies.
  • If you speak ... Yada yada!!!

Which means, if you want to get your packages out there in the wild, you should get familiar with this tool, and fast. This is what we'll be discussing.

Let's get started ...

Get Nuget Installed

Download the appropriate Nuget executable for your machine (Windows x86 preferably)

As at the time of writing, v3.5.0 is the recommended version of Nuget.

Setup Nuget on your Machine

The Nuget.exe file you have downloaded is a command-line interface (CLI), meaning you can use it from the command-line, and perhaps from the command-line alone.

Place Nuget in its folder

You need to place Nuget.exe in its own folder, which could be any folder. I have chosen to use C:\Users\IKECHI\Documents\Assemblies because I like it, but yours can be anywhere.

Execute Nuget.exe

Open your command-line window (command-prompt on Windows) and navigate to the folder containing Nuget.exe with the following line of code cd "{folder-path}" such as:

cd "C:\Users\IKECHI\Documents\Assemblies"  

Enter nuget and press ENTER

Your terminal should now look like:

terminal view nuget

Add Nuget to Path Environment Variables

Right now, to use Nuget, you'd have to be within the directory where Nuget.exe is ... We can fix that by adding the directory to the System Environment Variables.

Create a Nuget Package

So, now that you can access Nuget from any directory in the command-line, we're ready to start creating Nuget packages.

If you use Visual Studio, you have working with .csproj or .vbproj files, yes?

One beautiful thing about Nuget is that you can make packages out of a Visual Studio project folder easily.

Create a .Nuspec file

Simply navigate to the Visual Studio Project folder (The folder containing the csproj file) in your command line.

Type nuget spec and press ENTER

What that does is to create a .nuspec file which is then used to create a .nupkg file which is used by the Nuget server.

The .nuspec file created is an XML file which contains a default config with placeholders for describing your package.

<?xml version="1.0"?>  
<package >  
    <releaseNotes>Summary of changes made in this release of the package.</releaseNotes>
    <copyright>Copyright 2017</copyright>
    <tags>Tag1 Tag2</tags>

Note that values within the XML like $id$ and $title$ are placeholders and are filled by Nuget later. You may provide these values if you want to specify something different, such as the tags field which could be <tags>Entity-Framework, SQL, Database-Schema</tags>

So, let's move to the next step.

Create a .Nupkg file

When you're ready to package your application, simply type nuget pack within the same project folder.

In the true developer spirit, ignore the warnings 😋

Other alternative commands are:

> nuget pack <ProjectName>.csproj
> nuget pack <ProjectName>.nuspec

If you want to include dependencies in the package

> nuget pack <ProjectName>.csproj -IncludeReferencedProjects

You can specify the build configuration (Debug, Release, etc)

> nuget pack <ProjectName>.csproj -properties Configuration=Release

What this does is to create a .nupkg file which your Nuget server will make use of ...

Setup your Nuget server

Your Nuget server is where your packages are hosted.

If you're quite pedantic about exclusivity, it's possible to host it yourself.

In this article, we'll use MyGet which is quite capable of hosting Nuget, NPM, Bower, Maven and Vsix repositories.

Create an account

Fill this form ...

Create a Feed

When you ready to push your package, get the API Key on the Feed Details page and enter the following in your terminal within your project folder:

> nuget push <PackageName>.nupkg <your-api-key> -source


Did it work?


Yippeee!!! Congratulations! 😍😍😍

Now, you and your developer friends can share and use the feed<feed-name>/api/v3/index.json for your packages.

Be sure to recommend the article below and ask question in the comments section, if you have any.

PS: Check out my feed for the .NET Client for PayWithCapture

Have fun!

Show Comments