Publishing your first NuGet package in 5 easy steps
Posted on Friday, 15th April 2016
So you’ve just finished writing a small .NET library for a one-off project and you pause for a moment and think ”I should stick this on NuGet - others may find this useful!”. You know what NuGet is and how it works, but having never published a package before you’re unsure what to do and are short on time. If this is the case then hopefully this post will help you out and show you just how painless creating your first NuGet package is.
Step 1: Download the NuGet command line tool
First, you’ll need to download the NuGet command line tool. You can do this by going here and downloading the latest version (beneath the Windows x86 Commandlineheading).
Step 2: Add the NuGet executable to your PATH system variables
Now we’ve downloaded the NuGet executable we want to add it to our PATH system variables. At this point, you could simply reference the executable directly - but before long you’ll be wanting to contribute more of your libraries and adding it to your PATH system variables will save you more work in the long run.
If you already know how to add PATH variables then jump to Step 3, if not then read on.
Adding the nuget command to your PATH system variables
First, move the NuGet.exe you downloaded to a suitable location (I store mine in C:/NuGet/). Now, right-click My Computer (This PC if you’re on Windows 10). Click “Advanced System Settings” then click the “Environment Variables” button located within the Advanced tab. From here double-click the PATH variable in the top panel and create a new entry by adding the path to the directory that contains your NuGet.exe file (in this instance it’s C:/NuGet/).
Now, if all’s done right you should be able to open a Command Prompt window, type “nuget” and you’ll be greeted with a list of NuGet commands.
Step 3: Create a Package.nuspec configuration file
In short, a .nuspec file is an XML-based configuration file that describes our NuGet package and its contents. For further reading on the role of the .nuspec file see the nuspec reference on nuget.org.
To create a .nuspec package manifest file, let’s go to the root of our project we wish to publish (that’s where I prefer to keep my .nuspec file as it can be added to source control) and open a Command Prompt window (Tip: Typing “cmd” in the folder path of your explorer window will automatically open a Command Prompt window pointing to the directory).
Now type “_nuget_ spec” into your Command Prompt window. If all goes well you should be greeted with a message saying “Created ‘Package.nuspec’ successfully”. If so, you should now see a Package.nuspec file your project folder.
Let’s take a moment to look inside of our newly created Package.nuspec file. It should look a little like below:
<?xml version="1.0"?> <package> <metadata> <id>Package</id> <version>1.0.0</version> <authors>Joseph</authors> <owners>Joseph</owners> <licenseUrl>http://LICENSE_URL_HERE_OR_DELETE_THIS_LINE</licenseUrl> <projectUrl>http://PROJECT_URL_HERE_OR_DELETE_THIS_LINE</projectUrl> <iconUrl>http://ICON_URL_HERE_OR_DELETE_THIS_LINE</iconUrl> <requireLicenseAcceptance>false</requireLicenseAcceptance> <description>Package description</description> <releaseNotes>Summary of changes made in this release of the package.</releaseNotes> <copyright>Copyright 2016</copyright> <tags>Tag1 Tag2</tags> <dependencies> <dependency id="SampleDependency" version="1.0" /> </dependencies> </metadata> </package>
As you can see, all of the parameters are pretty self-explanatory (see the docs here if you have any uncertainties on what the configuration does). The only one you may have a question about is the dependencies node - this is simply a list of the dependencies and their version that your NuGet package may have (See here for more info).
Now we’ve generated our NuGet configuration file, let’s take a moment to fill it in.
Once you’re done, your manifest file should look a little like below. The next step is to reference the files to be packaged. In the following example, you’ll see that I’ve referenced the Release .dll file (take a look at the documentation here for more file options). If you haven’t noticed, I’ve also removed the
<?xml version="1.0"?> <package > <metadata> <id>Slugity</id> <version>1.0.2</version> <title>Slugity</title> <authors>Joseph Woodward</authors> <owners>Joseph Woodward</owners> <licenseUrl>https://raw.githubusercontent.com/JosephWoodward/SlugityDotNet/master/LICENSE</licenseUrl> <projectUrl>https://github.com/JosephWoodward/SlugityDotNet</projectUrl> <iconUrl>https://raw.githubusercontent.com/JosephWoodward/SlugityDotNet/release/assets/logo_128x128.png</iconUrl> <requireLicenseAcceptance>false</requireLicenseAcceptance> <description>Slugity is a simple, configuration based class library that's designed to create search engine friendly URL slugs</description> <language>en-US</language> <releaseNotes>Initial release of Slugity.</releaseNotes> <copyright>Copyright 2016</copyright> <tags>Slug, slug creator, slug generator, url, url creator</tags> </metadata> <files> <file src="bin\Release\Slugity.dll" target="lib\NET40" /> </files> </package>
Step 4: Creating your NuGet package
Once your Package.nuspec file has been filled in, let’s create our NuGet package! To do this simply run the following command, replacing the path to the .csproj file with your own.
nuget pack Slugity/Slugity.csproj -Prop Configuration=Release -Verbosity detailed
If you have the time, take a moment to look over the various pack command options in the NuGet documentation.
Once the above command has been run, if all has gone well then you should see a _YourPackageName._nupkg file appear in your project directory. This is our NuGet package that we can now submit to nuget.org!
Step 5: Submitting your NuGet package to nuget.org
We’re almost there! Now all we need to do is head over to nuget.org and submit our package. If you’re not already registered at nuget.org then you’ll need to do so (see the “Register / Sign in” link at the top right of the homepage). Next, go to your Account page and click “Upload a package”. Now all you need to do is upload your .nupkg package and verify the package details that nuget.org will use for your package listing!
Congratulations on submitting your first NuGet package!
I hope this guide has been helpful to you, and as always, if you have any questions then leave them in the comments!
Enjoy this post? Don't be a stranger!
Follow me on Twitter at @_josephwoodward and say Hi! I love to learn in the open, meet others in the community and talk Go, software engineering and distributed systems related topics.