21

I have set up NuGet Package Restore on my solution and it works nicely on my local machine. I followed the instructions supplied here:

http://docs.nuget.org/docs/workflows/using-nuget-without-committing-packages

The problem I have is on my build server where the following error occurs:

Package restore is disabled by default. To give consent, open the Visual Studio Options dialog, click on Package Manager node and check 'Allow NuGet to download missing packages during build.' You can also give consent by setting the environment variable 'EnableNuGetPackageRestore' to 'true'.

Unfortunately I dont have access to the build server as it is controlled off site so cant update the environment variable. Is there any other way around this? Anything I can add to the solution file or similar that would allow the package restore?

9 Answers 9

15

Try this package:

Install-Package NuGetEnablePackageRestore 
5
  • Thanks - does this package need to be installed on all projects in the solution or just the main project eg web app?
    – amateur
    Oct 8, 2012 at 22:10
  • 1
    You can install it once and then follow the instructions that will appear in the Library Package Manager Console. After completing the instructions, you can uninstall it again. Oct 9, 2012 at 17:53
  • 1
    Uses a nifty workaround to set the required 'EnableNuGetPackageRestore' environment variable as part of the build May 22, 2013 at 19:25
  • +1. Using the SET statement on the server, and applying the environment variable through control panel hadn't worked for me, but this did :) One Q @XavierDecoster - why would I want to uninstall it again? Wouldn't this re-break the build? Jul 11, 2013 at 9:29
  • because the package will inject the PowerShell scripts into your NuGet PowerShell profile :) it won't break the build as the build is not relying on this package which only adds a new Powershell command in the VS Package Manager Console. Jul 11, 2013 at 9:56
14

NuGet can use local settings for it's behavior which can be unpredictable if you're not 100% sure how the server is configured.

I prefer putting the NuGet settings inside the <sln root>/.nuget/NuGet.targets file which is version controlled and at a single location. I got this working with 3 quick edits to <sln root>/.nuget/NuGet.targets, they should look as below after editting:

Change 1:

<!-- Enable the restore command to run before builds -->
<RestorePackages Condition="  '$(RestorePackages)' == '' ">true</RestorePackages>

Change 2:

<!-- Determines if package restore consent is required to restore packages -->
<RequireRestoreConsent Condition=" '$(RequireRestoreConsent)' != 'false' ">false</RequireRestoreConsent>

My comment: Awkward logic but think of "requires consent not equal to false must be true" (original) as "requires consent equal to true must be true" (translated) and it makes sense to change the last part to "false" (the edit)

Change 3 : I also added/uncommented the <PackageSource ... > tag to to remove any dependencies on the

<ItemGroup Condition=" '$(PackageSources)' == '' ">
    <PackageSource Include="https://nuget.org/api/v2/" />        
</ItemGroup>
1
  • 2
    Thank you Sid for this - this solution worked for me whereas the fix by Xavier did not. Dec 6, 2013 at 15:31
5

I came across this issue when I tried building one of my projects with Jenkins, and managed to get it work by simply changing one value in the .nuget\NuGet.targets file from true to false.

I changed:

<!-- Determines if package restore consent is required to restore packages -->
<RequireRestoreConsent Condition=" '$(RequireRestoreConsent)' != 'false' ">true</RequireRestoreConsent>

to

<!-- Determines if package restore consent is required to restore packages -->
<RequireRestoreConsent Condition=" '$(RequireRestoreConsent)' != 'false' ">false</RequireRestoreConsent>

Notice the element value has changed. Hope this helps.

1
  • This worked for me as my msbuild server are not on-premises . I have a solution with 20+ projects and one of them were added recently with some custom packages (log4net, json.net) and was causing the problem.
    – jpgrassi
    Nov 6, 2015 at 13:17
1

In %appdata%\NuGet\NuGet.Config add the following section inside

    <packageRestore>
    <!-- Package Restore and MSBuild-Integrated Package Restore -->
    <add key="enabled" value="True" />

    <!-- Automatic Package Restore in Visual Studio -->
    <add key="automatic" value="True" />
  </packageRestore>

Full example

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <activePackageSource>
    <add key="nuget.org" value="https://www.nuget.org/api/v2/" />
  </activePackageSource>
  <packageRestore>
    <!-- Package Restore and MSBuild-Integrated Package Restore -->
    <add key="enabled" value="True" />

    <!-- Automatic Package Restore in Visual Studio -->
    <add key="automatic" value="True" />
  </packageRestore>
</configuration>
0

I probably could try to set RestorePackages property to true in .nuget\NuGet.targets file:

<RestorePackages Condition="  '$(RestorePackages)' == '' ">true</RestorePackages>
6
  • Just tried this and it didnt do the trick, thanks for your input though.
    – amateur
    Oct 8, 2012 at 20:06
  • Have you checked the csproj file property <RestorePackages>true</RestorePackages> ?
    – asa
    Oct 8, 2012 at 20:09
  • In all projects within the solution?
    – amateur
    Oct 8, 2012 at 20:12
  • 1
    I believe in all projects which uses NuGet. BTW, you could also try to restore packages localy: just remove all packages from FS and try to run msbuild yoursolution.sln command. It should highlight more details.
    – asa
    Oct 8, 2012 at 20:16
  • 1
    The project has over 30 projects so having to edit each of them is not ideal. Thanks for the suggestion though.
    – amateur
    Oct 8, 2012 at 20:19
0

For anyone stumbling upon this question, looking for a way to get packages restored on a build server, NuGet Package Restore gives a nice overview of current options.

I Chose to use the Command-Line Package Restore approach. It's as easy as issuing the following command line:

C:><path to nuget.exe> restore <path to solution.sln>

nuget.exe an be obtained from https://docs.nuget.org/consume/installing-nuget. I used version Command-Line Utility Latest 3.X.

0

Run this command to fix the NuGet Enable Package

Install-NuGetEnablePackageRestoreFix

Then after that run Enable command

Install-Package NuGetEnablePackageRestore

1
  • Worked for me! Thx! (Hint: NuGetEnablePackageRestore must be installed)
    – leon22
    Feb 18, 2019 at 15:07
0

Install-Package NuGetEnablePackageRestore

-2

-Go to Tools -> Library Package Manager -> "Package Restore" -> uncheck "Allow NuGet to download missing packages" and "Automatically check..."

  • Rebuild Solution

  • Clean Solution

  • Now check "Allow NuGet to download missing packages" and "Automatically check..."

  • Rebuild Solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.