NuGet package restore fail TFS Build Server

If you don’t want to check-in packages into your source control system, you can enable package restore. This will automate the download of packages during build, if packages aren’t on the machine yet.

After enabling this for a solution I’m working on, I had no problem running this locally. I removed my original packages folder from disc, and the build succeeded because nuget.exe (reference in the build) was able to download the packages. However… this didn’t work on our tfs build server.

NuGet package restore from build server – not downloading packages

The log file showed the command that was run:

"C:Builds\1\TeamProjectName\TeamProjectName\_Dev\_CI\Sources\.nuget\nuget.exe" install "C:Builds\1\TeamProjectName\TeamProjectName\_Dev\_CI\Sources\src\TeamProjectName.Services\packages.config" -source "https://nuget.org/api/v2/" -o "C:Builds\1\TeamProjectName\TeamProjectName\_Dev\_CI\Sources\packages"

After running this from the command prompt, the output folder (packages) was not only empty, it didn’t even exist. Worse, running the command didn’t even give a hint of a failure.

The solution for me: Starting with NuGet 2.0 there’s a new option to give NuGet consent to download packages: Allow NuGet to download missing files during build.
These are the steps to fix this problem:

  • Go to your build server (rdp or physical)
  • Start Visual Studio
  • Check the version of NuGet under Tools > Extension Manager - Update to v2.0 if needed and restart Visual Studio
  • You might need to uninstall, restart and install if simple update doesn’t work.
  • Open up the NuGet settings under Tools > Options > Package Manager
  • Make sure the Allow NuGet to download missing files during build checkbox is checked.

[NuGet Package Restore]

Possible solution if NuGet package restore fail on build server.
Make sure the consent checkbox is checked.

If your build runs under a different account

Maybe you can’t/won’t run Visual Studio under the account the build runs. Or maybe visual studio isn’t even installed.
NuGet will look for an environment variable as a workaround.
Create an environment variable named ‘EnableNuGetPackageRestore‘ with the value ‘true‘.
Right-click computer, properties, advanced system settings, environment variables, hit New under system variables, create the variable and reboot the machine.

Still not solved?

If this didn’t solve the problem, other things to try for your package restore to work on your build server:

  • Check the NuGet.targets file in the .nuget folder. And look for PackageSource. You can specify repositories there if you have a custom NuGet gallery or need to reference MyGet or similar.
  • Check the firewall of proxy in your network. NuGet might be blocked.

Was this post helpful to you? Please let me know in the comments.