· Diego Martin · tutorials · 2 min read
NuGet customization and some gotchas
Did you know you could customize the path where NuGet installs packages? And did you know you could apply some global NuGet configuration so that you could add custom authentication outside your solutions? Find out more on this post
NuGet is a package manager for .NET, enabling developers to share reusable code. It’s essential for managing dependencies in .NET projects, making it easier to add, update, and remove libraries.
Configuring NuGet Path
Sometimes, corporate security policies on company laptops require whitelisting certain folders. This can affect how and where NuGet installs packages. To address this, you can configure the NUGET_PACKAGES
environment variable, which specifies the directory where NuGet stores global packages.
By default, NuGet stores packages in %USERPROFILE%\.nuget\packages
. You can change this to a directory like C:\NuGet
by setting the NUGET_PACKAGES
environment variable.
Example, from a Windows terminal:
set NUGET_PACKAGES=C:\NuGet
This configuration is handy if your default packages folder needs to be whitelisted due to company security policies.
Gotcha with dotnet tool
When using the NUGET_PACKAGES
environment variable, you might encounter issues with the dotnet tool restore command. This is a known issue.
The best current workaround is to delete the %USERPROFILE%\.dotnet\toolResolverCache
folder before running the command again.
rmdir /s /q %USERPROFILE%\.dotnet\toolResolverCache
dotnet tool restore
This workaround ensures that the tool restoration process works correctly.
NuGet CLI Commands
NuGet provides several CLI commands to manage local caches and package installations:
To list all the local caches
dotnet nuget locals all --list
The NUGET_PACKAGES
environment variable specifically affects the global-packages
cache.
To remove all the local caches
dotnet nuget locals all --clear
Global configuration for NuGet
NuGet also supports global configuration settings stored in %APPDATA%\NuGet\NuGet.Config on Windows. This file, which can be customized, contains settings that apply to all NuGet operations across all your projects.
This comes handy if, for example, you have private NuGet repositories which require authentication, but you don’t want to add the authentication credentials in the NuGet.Config
at the solution level because you don’t want to expose the token. By creating a NuGet.Config
at the above global path, you’ll be able to restore or install packages in any solution without having to specify credentials there.
Example of NuGet.Config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
<add key="github" value="https://nuget.pkg.github.com/your_github_user_or_organization/index.json" />
</packageSources>
<packageSourceCredentials>
<github>
<add key="Username" value="your_pat" />
<add key="ClearTextPassword" value="your_pat_value" />
</github>
</packageSourceCredentials>
</configuration>