How can I modify the location of the NuGet default global packages folder?

Due to some historical reasons, reinstalling the system has become an ancestral skill for Windows users to solve intractable diseases. Affected by this, partitioning the hard disk has almost become an obsession. There are at least two disks, C and D, and an exaggerated 5-6 disks.

PS: macOS and Linux have always discouraged disk partitioning. Although it is not prohibited, it is not advocated. With the improvement of cloud technology and broadband, more and more people prefer to store their important data in cloud disks instead of local disks. First, they can be accessed across devices, and second, they don’t have to worry about sudden computer damage. or lost.

When I was writing the code a few days ago, I suddenly received a prompt from the IDE that the disk C is not enough space! Quickly opened the file explorer and took a look, wow… There is more than 1MB of space left on the C drive, no blue screen is enough to give me face.

Next is the three tricks like flowing clouds and flowing water:

  • Exit the application (when the physical memory is insufficient, a part of the disk space will be set aside as virtual memory. The pagefile.sys in the root directory of the C drive is the file generated by the virtual memory. Can watch it eat up disk space)
  • Clean up junk files
  • Restart the computer

After cutting, the available space increased to 9GB. But it’s not enough, who knows when there will only be 1MB left? I used TreeSize to analyze the usage of the C drive, and found that the size of the .nuget folder exceeded 10GB. Although I made some configurations in the project, I stored the nuget packages used in the current project in the project directory instead of the default The global folder directory, but there are still many system programs and packages installed by LinqPad will be cached to the default directory.

The default nuget package cache path for Windows is C:\Users\username.nuget\packages.
masOS is /Users/user account/.nuget/packages

Cleaning up the historical version can free up some space, but it does not cure the root cause. If a program or project depends on the cleaned-up version, it may be restored or simply report an error and cannot be used. Can the default directory be assigned to another disk?

Sure!

Method 1: Modify the global configuration file

NuGet will generate a NuGet.Config file in C:\Users\User\AppData\Roaming\
uGet\
, which is the global configuration file of nuget. If the .net project is not configured separately, it will use the global Configuration recovery, caching and reading dependent packages.

Open this file with a text editor, the content is as follows:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
  </packageSources>
</configuration>

If you want to add other package sources, you can add related nodes in packageSources:

<!--Add a Nexus Repository service as a nuget source-->
<add key="MyNuget" value="https://nexus.mydomainname.com/repository/nuget-hosted/index.json" />
<!--Add a local disk folder as a nuget source-->
<add key="Test Source" value="D:\.nuget\packages" />

If you want to modify the global package directory, you need to set the globalPackagesFolder in the config node of NuGet.config (if there is no config node, you need to add one manually), the complete content after configuration is as follows:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
  </packageSources>
  <config>
<add key="globalPackagesFolder" value="D:\.nuget\packages" />
  </config>
</configuration>

The following are the configuration items supported by config:

  • dependencyVersion (packages.config only): The default DependencyVersion value for package installs, restores, and updates (without specifying -DependencyVersion switch). The NuGet Package Manager UI also uses this value. Values are Lowest, HighestPatch, HighestMinor, Highest.

  • globalPackagesFolder (only for projects using PackageReference): The location of the default global packages folder. The default is %userprofile%\.nuget\packages (Windows) or ~/.nuget/packages (Mac/Linux). Relative paths are available in project-specific nuget.config files. This setting is overridden by the NUGET_PACKAGES environment variable, which takes precedence.

  • repositoryPath (packages.config only): Where to install NuGet packages other than the default $(Solutiondir)/packages folder. Relative paths are available in project-specific nuget.config files.

  • defaultPushSource: Identifies the package source URL or path that should be used as the default if the operation does not find any other package source.

  • http_proxy http_proxy.user http_proxy.password no_proxy: The proxy setting to use when connecting to the package source; http_proxy should be http://:@ format. Passwords are encrypted and cannot be added manually. For no_proxy, the value is a comma-separated list of domains to bypass the proxy server. The http_proxy and no_proxy environment variables can be used interchangeably for these values.

  • maxHttpRequestsPerSource: Controls the maximum number of parallel requests sent from NuGet to each package source for package dependency resolution and download. The default value on dotnet.exe is HttpClientHandler.MaxConnectionsPerServer derived from the Int32.MaxValue property. This setting has no effect on dotnet.exe``Mac OS because the limit is set to 16 to avoid too many open files errors. NuGet client tools running on (eg) Visual Studio defaults to Mono on 1 code>Windows 64. Packages.config defaults to Environment.ProcessorCount for .NET Framework nuget.exe style projects. Configuring the property maxHttpRequestsPerSource to a value smaller than the default may affect NuGet performance.

  • signatureValidationMode: Specifies the verification mode for verifying package signatures for package installation and restore. accept is , require. The default is accept.

<config>
    <add key="dependencyVersion" value="Highest" />
    <add key="globalPackagesFolder" value="c:\packages" />
    <add key="repositoryPath" value="c:\installed_packages" />
    <add key="http_proxy" value="http://company-squid:[email protected]" />
    <add key="signatureValidationMode" value="require" />
    <add key="maxHttpRequestsPerSource" value="16" />
</config>

Method 2: Setting Environment Variables

If method one does not work, you need to specify the global package location by setting environment variables.

  1. Open the Start Menu–Settings–System–System Information–Advanced System Settings in turn, then switch to the “Advanced” tab, click the “Environment Variables” button to open the environment variable settings window.

  1. The environment variable setting window is divided into user environment variables and system environment variables. My purpose is to free up disk space, so here I choose to set the environment variables at the system level (in fact, they are almost the same).

  1. Click the New button below the system variable, fill in the variable name (NUGET_PACKAGES) in the pop-up dialog box, select by browsing the directory or directly enter the new global package directory in the variable value input box. Then click the OK button.

  1. After confirming that the creation is successful, click OK in the Environment Variables window to save the settings.
  2. After completing the above settings, the new settings will take effect immediately (you can restart the computer if you are worried), and then you can cut all the packages in the original directory to the new directory.