How To Release a NuGet Package Locally?

When building a library, it’s useful to test it locally, before even pushing it to a test (and definitely prod) environment. The following tutorial shows all the steps necessary to perform those local tests and see exactly what an end-user will see when using your NuGet package. All this, without leaving your machine!

Table Of Contents

Video tutorial

Setup your environment

First, we have to download NuGet executable and add it to our PATH to use NuGet CLI comfortably. This will allow you to type nuget in your Command Prompt to run any NuGet script. You can skip to the next chapter if you already have it configured.

Before we attempt to set it up, check if you don’t already have it ready:

  1. Press Win + S
  2. Type cmd and press Enter
  3. Type nuget and press Enter

If you see the following message…

'nuget' is not recognized as an internal or external command,
operable program or batch file.
NuGet not found - The exe wasn't added to PATH
NuGet not found – The exe wasn’t added to PATH

…Perform the steps below. Otherwise, move on to the Pack it section:

  1. Download NuGet.exe from here (latest recommended version is okay)
  2. Place it in a folder it will be stored in. For example: C:\Users\user_name\NuGet, remember to replace user_name with your own. Copy that folder path (remember not to include nuget.exe in the path.
  3. Copy that path address
  4. Press Win + S
  5. Type Environment and press Enter on Edit the system environment variables
  6. In Advanced tab, click on Environment variables… button
  7. In User variables, click on Path variable and select Edit
  8. Click New
  9. Paste the path to your nuget.exe
  10. Press OK
  11. Repeat steps 7-10 for System variables

Alternatively, refer to this article.

Different ways of packing your library

Before we release the package, we have to prepare it first. To do that, we use a pack command. I’ll show you three ways to do that.

Pack using Visual Studio

  1. Open your project/solution in Visual Studio
  2. Right-click on the library and select Properties
  3. Navigate to the Package tab
  4. Tick Generate NuGet package on build – this will do exactly what is says on the tin. It will automatically pack your NuGet package on build.
  5. Insert package details in the fields below. Make sure to specify the package version. The convention I usually use is Major.Minor.Patch starting with 0.0.0. Add “-beta” suffix for the test release.
  6. Save the file
  7. Press Ctrl + Shift + B or select Build -> Build Solution
  8. Right-click on your library .csproj file in Solution Explorer
  9. Select Open folder in File Explorer
  10. You should now see a bin folder, which contains the binary output of your compilation. Enter that folder and go to Debug/Release, whichever configuration you selected when building the project
  11. the *.nupkg file should be there.
Project settings in Visual Studio allow generating a NuGet package on build
Project settings in Visual Studio allow generating a NuGet package on build

Pack using NuGet CLI

  1. Navigate to the folder containing your *.csproj file of the library you’d like to release
  2. Type cmd in the File Explorer address bar and hit Enter. This will open Command Prompt
  3. Type dotnet build and hit Enter. This will build your project. bin and obj folders should appear.
  4. Back in Command Prompt, type nuget pack and hit Enter. This will generate *.nupkg file in the current directory.
Using NuGet CLI to pack the DLL
Using NuGet CLI to pack the DLL

Pack using dotnet CLI

  1. Navigate to the folder containing your *.csproj file of the library you’d like to release
  2. Type cmd in the File Explorer address bar and hit Enter. This will open Command Prompt.
  3. Type dotnet build and hit Enter. This will build your project. bin and obj folders should appear.
  4. Back in Command Prompt, type dotnet pack and hit Enter. This will generate *.nupkg file in the bin/{environment}/ directory, where enviroment is either Debug or Release, depending what you chose for your build.
Using dotnet CLI to pack the DLL
Using dotnet CLI to pack the DLL

Release the package locally

We’re almost there, we already have a packed library, all we want is to make it available in our Visual Studio project locally, so we can imitate using a normal NuGet package downloaded from a remote repository.

To do that, follow the below steps:

  1. Open Command Prompt as an Administrator in the location of your *.nupkg file.
  2. Type nuget add package_filename.nupkg -Source "C:\\Program Files (x86)\\Microsoft SDKs\\NuGetPackages\\" (escape characters and quotation marks are important since the path contains spaces)
  3. Replace package_filename (your nupkg file name) with the correct value and hit Enter. The source specifies where the package will be unpacked to – the location I provided is the default location for offline NuGet packages in Visual Studio. This will matter in a minute.
Release locally to the folder available in Visual Studio Offline
Release locally to the folder available in Visual Studio Offline

Use your local NuGet package

  1. Open the project you’d like to install your library to in Visual Studio
  2. Right-click on the solution/project – wherever you’d like to install your new NuGet package and select Manage NuGet Packages…
  3. In the top-right, you should have a dropdown menu under Package Source with a default value of nuget.org. Visual Studio normally searches there for the packages by default, but since our package is deployed locally, change it to Microsoft Visual Studio Offline Packages. (You can configure where that location is by pressing a little cog icon next to the dropdown if you’d like to change it)
  4. Search your package name and hit Enter. It should appear on the list
  5. You can now enjoy your package locally!

Find your new package in Visual Studio Package Manager
Find your new package in Visual Studio Package Manager

Conclusion

As you can see, deploying your NuGet package locally is really easy if you follow those simple steps. In the end, it’s all about generating that *.nupkg file and adding it to the offline feed in the correct folder, so Visual Studio Offline Package Manager can find it.

Default image
Pawel Flajszer
Articles: 5

Leave a Reply