How to create your own NuGet Server

In this article we will see how to create our own NuGet Server, make a package, publish it and install it in any project.

The problem

What do we do when we develop a library on which different projects depend, and this library receives continuous code integrations several times a day, generating possible breaking changes between versions and when we realize, that a function was obsolete in the new version, or has a new implementation.

That´s the moment where we realize that we must have available all the versions of the library, for the projects and the development team.

The solution

Having our own NuGet Server can help us to generate a package with each version of the library and have it available for each project and member of the development team.

Creating our NuGet Server

Let’s make our NuGet Server work, first we need to create an empty Web Application and add the NuGet.Server package.

Creating an Empty Web Application for a NuGet Server
Creating an empty web application for a NuGet server.
Adding the package NuGet.Server
Adding the NuGet.Server package to our project.

With just doing this and running the project we will have our NuGet Server working.

NuGet Server running from Visual Studio

Although it is working, it does so through Visual Studio and IIS Express, which means that if we stop the project, our NuGet Server will stop working, to not depend on that, we will publish it on a local IIS server

Publishing the NuGet Server in IIS

The first thing we must do is create a Website, for that, after opening the IIS administration window, we right click on Sites and then Add Website… which will open a window to indicate the details of the new Website, as the name, the path where the files will be and the port. This is the basic configuration.

Creating the website on IIS that hosts the Nuget Server

Now we are going to publish the project where we have our NuGet Server in the website that we have just created: basically we right click on the name of the project and then on Publish… this will open a window where we must select the target, and indicate the destination site data, as seen in step 2 of the image, save the information and then click on Pusblish.

Publishing our NuGet server in IIS
Publishing our NuGet Server in IIS
NuGet Server running from IIS
NuGet Server running from IIS

It is also necessary to configure an apiKkey in the Application Settings (web.config), which should be indicated when publishing a package. In this case I put the value 809.

Nuget Server Api Key
Configuring the apiKey in NuGetServer

With this, our server is ready to receive and serve NuGet packages.

Creating and publishing a package

We are almost ready to create our first package, which we can version and have it available from our NuGet Server.

First we need to install the NuGet.CommandLine package in the project we want to create a package, in my case create a test project called MyLibrary. The NuGet.CommandLine will allow us to execute NuGet commands in our project.

NuGet Pack

NuGet Pack is the command that creates the package that we want to publish to the server, that is, a file with extension .nupkg, of the project that we indicate. This command can be executed in the Package Manager Console and takes as an argument the path of the project and the configuration in which we want it to be created, in this case: release, that is, ready to be used in production.

The description, the author, the company, version and other information of the package must be indicated in the properties of the project. Once this is done we can execute the command:

NuGet Pack .\MyLibrary\MyLibrary.csproj -Properties Configuration=release
Package information on the right, and NuGet Pack command on the bottom

after run the command, the package MyLibrary.1.0.0.nupkg will be created in the project’s solution folder.

NuGet Push

NuGet Push is the command that will allow us to publish our package to NuGet Server, we have to indicate the package path, the server url and the apiKey that we configured previously.

NuGet Push .\MyLibrary.1.0.0.nupkg -Source http://localhost:2019/nuget -ApiKey 809
NuGet Package Published
NuGet Package published

Installing the NuGet Package

Since the package is our Nuget Server we can install it, but first we must add it as a source of packages in the NuGet Package Manager, for that we right click on a project, then click Manage NuGet Package… and add the url of our NuGet Server as a source of packages.

NuGet Server as Package Source
Our NuGet Server as Package Source

With this we can install our package in any project.

MyLibrary package

If you like the post, have any questions or comments, mention me in Twitter @robertlluberes

Leave a Reply