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: https://akhilsharma.work/contact-me/

What is NuGet?

I would give you the official answer to this question from nuget.org, “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: https://docs.microsoft.com/en-us/nuget/what-is-nuget

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"?>
<configuration>
    <packageSources>
        <add key="LocalSource" value="D:\this-is-some-optional-directory\some-path" protocolVersion="3" />
    </packageSources>
    <disabledPackageSources>
        <add key="nuget.org" value="true" />
        <add key="Microsoft Visual Studio Offline Packages" value="true" />
    </disabledPackageSources>
</configuration>

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 nuget.org 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 nuget.org. 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:
  D:\this-is-some-optional-directory\some-path

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: https://github.com/akhilvatts/NuGet-Cli-Utility-Scripts/tree/main/Install-NuGet-Package-From-LocalSource

I hope this blog helps!

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