It's a simple process to create a NuGet package from a .NET Class Library and publish it to nuget.org. The following steps walk you through the process using the NuGet command-line interface (CLI) and Visual Studio:
- Install pre-requisites
- Create a class library project
- Create the .nuspec package manifest file
- Create the package
- Publish the package
- Visual Studio 2015. Install the Community edition for free from visualstudio.com; you can use the Professional and Enterprise editions as well, of course.
- NuGet CLI. Download the latest version of nuget.exe from nuget.org/downloads, saving it to a location of your choice. Then add that location to your PATH environment variable if it isn't already.
nuget.exe is the CLI tool itself, not an installer, so be sure to save the downloaded file from your browser instead of running it.
Create a class library project
In Visual Studio, choose File > New > Project, expand the Visual C# > Windows node, select the "Class Library" template, name the project AppLogger, and click OK.
Right click on the resulting project file and select Build to make sure the project was created properly.
Within a real NuGet package, of course, you'll implement many useful features upon which others can build applications. For this walkthrough, however, you won't add any additional code because a class library from the template is sufficient to create a package.
Create the .nuspec package manifest file
Every NuGet package needs a manifest–a
.nuspec file–to describe its contents and its dependencies. The NuGet CLI will create this file for you, which you then customize.
- Open a command prompt and navigate to the folder containing the AppLogger project file (
Run the NuGet CLI
speccommand to generate
Open the file in your favorite text editor. It will look something like the code below, where tokens in the form $
<token>$ will be replaced during the packaging process with values from the project's Properties/AssemblyInfo.cs file. For more details on tokens, see Creating a .nuspec file.
<?xml version="1.0"?> <package> <metadata> <id>$id$</id> <version>$version$</version> <title>$title$</title> <authors>$author$</authors> <owners>$author$</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>$description$</description> <releaseNotes>Summary of changes made in this release of the package.</releaseNotes> <copyright>Copyright 2016</copyright> <tags>Tag1 Tag2</tags> </metadata> </package>
Select a package ID that is unique across nuget.org. We recommend using the naming conventions described in Creating a package. You must also update the author and description tags or you will get an error in the next step. Here's an updated
.nuspecfile as an example:
<?xml version="1.0"?> <package> <metadata> <id>MyCompanyName.MyProductName.MyPackageName</id> <version>$version$</version> <title>$title$</title> <authors>kraigb</authors> <owners>kraigb</owners> <requireLicenseAcceptance>false</requireLicenseAcceptance> <description>Awesome application logging utility</description> <releaseNotes>First release</releaseNotes> <copyright>Copyright 2016</copyright> <tags>application app logger logging logs</tags> </metadata> </package>
For packages built for public consumption, pay special attention to the
<tags> element, as these tags help others find your package and understand what it does.
Create the package
Creating a NuGet package from a project is simple: just run the
nuget pack AppLogger.csproj
This will create a NuGet package file like
AppLogger.22.214.171.124.nupkg using, of course, the package name and version number from the
Note that you'll get warnings if you haven't updated various fields in the
.nuspec file from their default values.
Publish the package
You're now ready to publish the package to nuget.org using the NuGet CLI. (Alternately, you can use the nuget.org publishing workflow.
The packages you publish to nuget.org will be publicly visible to other developers. To host packages privately, see Hosting packages.
- Create a free account on nuget.org, or log in if you already have one. When creating a new account, you'll receive a confirmation email. You must confirm the account before you can upload a package.
- Once logged in, click your user name (on the upper right) to navigate to your account settings.
Under API Key, click copy to clipboard to retrieve the access key you'll need in the CLI:
Always keep your API key a secret! If your key is accidentally revealed, you can always regenerate it at any time. You can also remove the API key if you no longer want to push packages via the CLI.
At a command prompt, run the following command, replacing the key with the value copied in step 3:
nuget push AppLogger.126.96.36.199.nupkg 47be3377-c434-4c29-8576-af7f6993a54b -Source https://www.nuget.org/api/v2/package
You should then see something like the following:
Pushing AppLogger.188.8.131.52.nupkg to 'https://www.nuget.org/api/v2/package'... PUT https://www.nuget.org/api/v2/package/ Created https://www.nuget.org/api/v2/package/ 6829ms Your package was pushed.
In your account on nuget.org, click Manage my packages to see the one that you just published; you'll also receive a confirmation email. Note that it might take a while for your package to be indexed and appear in search results where others can find it, during which time you'll see the following message on your package page:
Virus scanning: Before being made public, all packages uploaded to nuget.org are scanned for viruses and rejected if any viruses are found. All packages listed on nuget.org are also scanned periodically.
And that's it! You've just created and published your first NuGet package to nuget.org, that other developers can use in their own projects.