ProTip: NuGet Local Package Installation

ProTip: NuGet Local Package Installation

Today we are going to do installation of NuGet package from a local location using PowerShell. This is a commonly used scenario but many end up reinventing the wheel even when all the resources are available. Hence this blog is to help and stop the wheel reinvention.
You might end up with a better solution by doing wheel reinvention and if you really got to that great solution, please let me know as well. I’ll be really happy and glad to learn from you! Reach me out at:

What is NuGet?

I would give you the official answer to this question from, “NuGet is the package manager for .NET. The NuGet client tools provide the ability to produce and consume packages. The NuGet Gallery is the central package repository used by all package authors and consumers.”

You may read more about how it works at Microsoft’s super awesome documentation at:

How to do local NuGet package installation using PowerShell?

When dealing with NuGet Cli, one of the main files that play the most important role is nuget.config file. nuget.config is primarily responsible for controlling the behaviour of NuGet through various settings mentioned in it.

So, here in our case as well, nuget.config is going to play a major role in setting the things up for us. Below is the nuget.config file that we’ll be using for our purpose.

<?xml version="1.0" encoding="utf-8"?>
        <add key="LocalSource" value="D:\this-is-some-optional-directory\some-path" protocolVersion="3" />
        <add key="" value="true" />
        <add key="Microsoft Visual Studio Offline Packages" value="true" />

In the above config file, we have given our local source as some directory in D drive. It can be any path depending upon your requirement. Then we disabled the as package source. We needed to disable it as this source is the default source of nuget cli and we want nuget cli to pick our package from our specified location instead of going online and checking at Secondly, we disabled “Microsoft Visual Studio Offline Packages” source as this is the place where most of the packages would be if you work a lot on Visual Studio for code building. I would say that this is an optional source and you may decide to remove this line incase this is not valid in your case.

Next, we’ll execute our NuGet cli command to install the package from our local source.

.\nuget.exe install <some-package> -ConfigFile .\nuget.config -OutputDirectory <output-directory>

In the above step we would pass the package name, our custom nuget.config file and the output directory / target directory where we would want to install our package to.

An important thing to note for the above command is that this would always install the latest package version for your desired package if you have multiple packages at your local source. In order to install package with your required version of package, you would need to pass the Version parameter to the above command. The final command would be:

.\nuget.exe install <some-package> -ConfigFile .\nuget.config -OutputDirectory <output-directory> -version <package-version>

And your objective is achieved!! The package installation log should be similar to mine as below:

PS D:\dest> .\nuget.exe install mypackage.web -ConfigFile .\nuget.config -OutputDirectory packages -version 0.0.1436
Feeds used:

Attempting to gather dependency information for package 'mypackage.web.0.0.1436' with respect to project 'D:\dest\packages', targeting 'Any,Version=v0.0'
Gathering dependency information took 64 ms
Attempting to resolve dependencies for package 'mypackage.web.0.0.1436' with DependencyBehavior 'Lowest'
Resolving dependency information took 0 ms
Resolving actions to install package 'mypackage.web.0.0.1436'
Resolved actions to install package 'mypackage.web.0.0.1436'
Retrieving package 'mypackage.web 0.0.1436' from 'LocalSource'.
Adding package 'mypackage.web.0.0.1436' to folder 'D:\dest\packages'
Added package 'mypackage.web.0.0.1436' to folder 'D:\dest\packages'
Successfully installed 'mypackage.web 0.0.1436' to D:\dest\packages
Executing nuget actions took 2.44 sec

You may check the complete code at GitHub:

I hope this blog helps!

Also, don’t forget to checkout my other popular blogs:

Subscribe to Akhil Sharma - An Engineering and Writing Geek

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.