← Back to main site

Creating and using a local NuGet package repository

March 27, 2018

NuGet package repository For .NET projects, there is NuGet, the package manager to fulfil the external dependencies in the project. It’s not just limited to the packages that are present on http://www.nuget.org, but there can be multiple package repositories. That’s all nice, but what if you have some home-grown library that you do not want to make publically available? A great use of NuGet packages can be done for class reusability in private projects as well. It is easy to create a NuGet repo on local machine or FTP shared location on your network. How? I’ll show you.

Create a .NET Standard class library for the classes to be shared as a package.

Create the library

Add your code and public API classes to this library.

namespace VHC.StdLib
{
    public class MathUtils
    {
        public static int Square(int number) => number * number;
    }
}

Go to project properties > Package. Fill assembly details. Check on Generate NuGet package on build. Save.

Edit Assembly Properties

After completing the project, pack it to create the nupkg file. Technically speaking, a NuGet package is just a ZIP file that’s been renamed with the .nupkg extension and whose contents match certain conventions. To pack a project, build it and then right click it in Solution Explorer and click Pack.

Pack

You’ll see the package is created in debug or release folder.

Output

Alternatively, we can use MSBuild to pack (for build scripts) as follows —

msbuild /t:pack /p:Configuration=Release

Make sure NuGet CLI is installed from www.nuget.org and is saved in PATH environment variable. Then create a folder accessible to you (local directory or network share) as package repo.

Publish the package to local repo using ‘nuget add’ —

nuget add VHC.StdLib.1.0.0.nupkg -source d:\source\nuget_repo

Now, let’s take a look at the repository nuget_repo. The package is listed as per version, so it is important to maintain assembly versions while publishing.

Nuget Repo

Similarly, ‘nuget delete’ is used to delete a package from repo.

Now, we are ready to consume our library from another project. Let’s create another client project using Visual Studio. Right click on the project, and go to Manage NuGet Packages for Solution…

Manage Nuget Packages

Click the Settings button (little gear icon) next to the Package Source.

Manage Nuget Packages

We find ourselves in Visual Studio Options for NuGet package sources. These are all the places NuGet looks for dependencies. Let’s add our nuget_repo folder here. Add a new package repo using (+) button and set the path to our source folder. Make sure this one is used before nuget.org. You can arrange the sources by arrow buttons on top right. Click OK when done.

Manage Nuget Packages

Now, our package can be found and installed via NuGet. Visual Studio Packages UI

OR it can be installed using Package Manager Console.

Package Manager Console

This can then be used like any other package. 

Use of Package

For more in-depth information on this, please refer to the MSDN sources which I learned this from —


{author}

Written by
Vibhav Churi