.net - Is it possible to change the location of packages for NuGet?

ID : 10024

viewed : 34

Tags : .netvisual-studionugetnuget-package.net

Top 5 Answer for .net - Is it possible to change the location of packages for NuGet?

vote vote

97

It's now possible to control which folder the packages are installed into.

http://nuget.codeplex.com/workitem/215

Edit: See Phil Haack's comment on Dec 10 2010 at 11:45 PM (in the work item/the link above). The support is partially implemented in 1.0, but is not documented.

According to @dfowler: Add a nuget.config file next to the solution with this:

<settings> <repositoryPath>{some path here}</repositoryPath> </settings> 

There is a nuget package for creating the package folder override.

Update for version 2.1

As Azat commented, there is now official documentation on how to control the package locations. The release notes for 2.1 specifies the following configuration in a nuget.config file (see the release notes for a description of valid places to put the config files and how the hierarchical configuration model works):

<configuration>   <config>     <add key="repositoryPath" value="C:\thePathToMyPackagesFolder" />   </config>   ...  </configuration> 

This would change the packages folder for the configuration level you put the file in (solution if you put it in the solution directory, project in project directory and so on). Note that the release notes state:

[...] if you have an existing packages folder underneath your solution root, you will need to delete it before NuGet will place packages in the new location.

vote vote

81

  1. Created a file called "nuget.config".
  2. Added that file to my solutions folder

this did NOT work for me:

<configuration>   <config>     <add key="repositoryPath" value="..\ExtLibs\Packages" />   </config>   ...  </configuration> 

this did WORK for me:

<?xml version="1.0" encoding="utf-8"?> <settings>   <repositoryPath>..\ExtLibs\Packages</repositoryPath> </settings> 
vote vote

75

Okay for the sake of anyone else reading this post - here is what I understand of the myriad of answers above:

  1. The nuget.config file in the .nuget folder is relative to that folder. This is important because if your new folder is something like '../Packages' that will put it where it always goes out of the box. As @bruce14 states you must do '../../Packages' instead

  2. I could not get the latest nuget (2.8.5) to find a packages folder outside of the standard location without enabling package restore. So once you enable package restore then the following should be added to the nuget.config file inside of the .nuget folder to change the location:

    <?xml version="1.0" encoding="utf-8"?> <configuration>   ...   <config>     <add key="repositoryPath" value="..\..\Packages" />   </config>   ... </configuration> 
  3. (This is important) If you make ANY changes to the package folder location inside of the nuget.config files you must restart visual studio or close/reload the solution for the changes to take effect

vote vote

63

A solution for Nuget 3.2 on Visual Studio 2015 is:

<?xml version="1.0" encoding="utf-8"?> <configuration>     <config>         <add key="repositoryPath" value="../lib" />     </config> </configuration> 

Using forward slash for parent folder. Save above file (nuget.config) in solution folder.

Reference is available here

vote vote

51

The solution proposed in release notes for 2.1 doesn't work out-of-the-box. They forgot to mention that there is code:

internal string ResolveInstallPath() {     if (!string.IsNullOrEmpty(this.OutputDirectory))     {         return this.OutputDirectory;     }     ISettings settings = this._configSettings;      ... } 

which prevents it from working. To fix this you need to modify your NuGet.targets file and remove 'OutputDirectory' parameter:

    <RestoreCommand>$(NuGetCommand) install "$(PackagesConfig)" -source "$(PackageSources)"  $(RequireConsentSwitch)</RestoreCommand> 

So now, if you add 'repositoryPath' config somewhere in NuGet.config (see the release notes for a description of valid places to put the config files), it will restore all packages into single location, but... Your .csproj still contains hints to assemblies written as relative paths...

I still don't understand why they went hard way instead of changing PackageManager so it would add hint paths relative to PackagesDir. That's the way I do manually to have different package locations locally (on my desktop) and on build agent.

<Reference Include="Autofac.Configuration, Version=2.6.3.862, Culture=neutral, PublicKeyToken=17863af14b0044da, processorArchitecture=MSIL">   <Private>True</Private>   <HintPath>$(PackagesDir)\Autofac.2.6.3.862\lib\NET40\Autofac.Configuration.dll</HintPath> </Reference> 

Top 3 video Explaining .net - Is it possible to change the location of packages for NuGet?

Related QUESTION?