If you have mature Software Development department in your organization or you are a software development organization, your team would have developed set of reusable libraries that are being used in several projects. A success of a mature software development team relies on being able to generate reusable code and practices.

Sharing these libraries across multiple teams and maintaining the versioning is a difficult task if you are not using the modern approaches like Nuget packages. Do you know you can create your own Nuget packages and publish your own Nuget Feed, so your teams can easily use these libraries?

Not only for your private purpose, but if you are a product company publishing versions of software libraries for your end users, Nuget is a great channel to publish your libraries.

How to create your own private Nuget Feed?

Creating a private NuGet feed gives you an easy way to standardize versioning and distribution of framework or library code in your organization. The simplest way to accomplish this is to host an ASP.NET website running NuGet.Server. You can host such website either internally in your company or on your private servers on the cloud.

Deploying your feed with NuGet.Server

The first step to hosting a NuGet feed is to simply create an ASP.NET web application to run on IIS on your server, or to publish to the cloud. Initialize the project with the “empty” template.

Open NuGet package manager for the solution. Search for an install the latest NuGet.Server release.

NuGet.Server will add a few files to your project, and just like that, you have a working NuGet feed out of the box. If running locally, configure the virtual directory to run the application under local IIS. If hosting in the cloud, publish your application accordingly.

Publishing packages to the feed

By default, NuGet.Server will look for .nupkg files in the Packages folder inside the application directory. Whatever files it finds in that flat directory, it will automatically unpack into the proper folder structure for the NuGet repo behind the scenes. You can configure the directory it looks in using a Web.config setting. The packagesPath key will be automatically added there when NuGet.Server is installed.

How to create publishing package for your libraries?

The simplest way to package your code for the NuGet feed is to use nuget.exe on the command line. Because nuget.exe does not come with any version of Visual Studio, you will need to visit the NuGet downloads page, install it, and add it to your PATH.

First, navigate to the project directory of the code you want to package. To create a NuGet package from the project, you will need to create a nuspec file, which is an XML definition of the package’s metadata: name, version, author, dependencies, and so on. Open the command prompt in the project directory and run the command “nuget spec” to automatically generate a nuspec based on the project file.

Open the file in a text editor. Note that entries with $ signs (such as $author$) will be automatically replaced with values from the assembly at packing time, but you may want to manually enter your own authorship information. You can also fill in the other fields such as icon URL, description, and tags.

With the nuspec created, you can now create a NuGet package. From the command prompt in the project directory, run the command “nuget pack” to create a  .nupkg file. The package will contain the proper dependency references once created.

Drag this .nupkg file into the NuGet feed application’s package directory on the server to add it to the feed.

Installing packages from the feed

Once you’ve added some packages, you need a way to install them through the familiar Package Manager interface in Visual Studio. The simplest way is to add your feed as a package source within Visual Studio’s options.

The source URL should be the local IIS URL or the cloud-hosted URL of your NuGet.Server application, and it should end with “/nuget”.

Alternatively, if you are working with a project under source control that references your NuGet package, you can include the feed information in the solution itself. This way, a new user who downloads the solution from source control will automatically have access to the feed in order to restore packages.

To do so, add a new text file directly the solution in the Solution Explorer. Name the file “nuget.config”, and include in it the following information:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <config>
    <clear />
    <add key="repositoryPath" value="packages" />
  </config>
  <packageSources>
    <clear />
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
    <add key="MyPackageFeed" value="http://10.1.10.100/MyPackageFeed/nuget" />
  </packageSources>
  <activePackageSource>
    <add key="All" value="(Aggregate source)" />
  </activePackageSource>
</configuration>

The “MyPackageFeed” information should be the same as previously described, referring to the URL of your private NuGet.Server application and ending in “/nuget”.

With either of the above steps completed, you will see your published packages available for install in the Package Manager.

Hope this helps you publishing your own Nuget Packages and be a rockstar among your technical team. :) Post your questions below if you run into problems or anything in this post should be corrected.