NuGet: Fetch missing or out of date packages on build

By: on June 3, 2013

NuGet is a package manager for the .NET world. At its core is an API that will take a set of assemblies that you are interested in and download any that are new to a specified folder. There is then a Visual Studio plug-in that uses a convention-based approach to adding new packages to your project. It creates a folder called “packages” in your solution, and a packages.config file in each project that makes use of NuGet. When you use the GUI to add a new package to a project, it updates the config file, and then pulls down the binaries and adds them to the project.

And of course, if the binaries are missing at build time it looks at that config file and pulls them down again. Wait, it doesn’t do that? Not only does it not do that, it’s not at all clear (to me at least) how to even trigger a refresh of the binaries manually.

This is because using NuGet and then committing the binaries to your source control is the developers’ recommended way of using NuGet. Which is Not Good.

Anyway, it’s an easy fix. Add this line to the “Pre-build event command line” for each project that makes use of NuGet. (Oh, and make sure that NuGet is on your PATH)

nuget install $(ProjectDir)packages.config -OutputDirectory $(SolutionDir)packages

And you’re away!

The “Pre-build event command line” option can be found in project properties in the “Build Events” tab.



  1. Ringo De Smet says:

    There is no need to do that. Just make sure that you change the values of the following properties in NuGet.targets:

    • RestorePackages to true
    • RequireRestoreConsent to false

    I also changed the following to prevent committing NuGet itself:

    • DownloadNuGetExe to true


  2. Martin Eden says:

    I tried those settings but couldn’t get it to work. Maybe I am just missing something… but the approach I blogged certainly worked for me!

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>