Force Nuget to Reinstall Packages without Updating

Occasionally I run into an issue where I’ll open a solution in Visual Studio, build it, and the build will fail because of dependent packages. I’ll try every way offered by Visual Studio to restore packages, but it will claim everything is up to date. Looking in Solution Explorer, you’ll see that some packages are clearly missing (icons on the packages showing they’re not there), but no amount of telling VS to restore packages (or building, which should do the restore as well) will get them.

The fix for this is to open Package Manager Console and run this command:

Note: If you just run Update-Package, it will try to update all packages to the latest version, which isn’t necessarily what you want (especially if you’ve simply pulled from source control and want the project to just build with the versions of packages it has in source control).

That’s it – this does the trick for me. If you want to narrow it down to a certain project, just make sure you’ve selected the correct active project, or use this:

I’ve seen this problem frequently on Visual Studio 2015 and a .NET 4.6 solution with a handful of projects (ASP.NET and class libraries). I’m not sure how widespread it is, or if it’s still present in VS2017. Hope this helps.

  • rz

    Another option is to simply remove specific package folder and run restore again, I prefer to do it from console, it seems to be faster.

  • Joel Hammond-Turner

    It’s often useful to reinstall just a single package – particularly if something’s borked your references.

    update-package mypackage –reinstall

  • The need to do this arises often when you change the directory structure relationship between a .SLN and its corresponding .CSPROJ files. Packages are installed at the .SLN level and package references in .CSPROJ files contain relative paths back to that directory. If that directory structure changes, you’ll have to do this -reinstall bit.

    • Gareth Erskine-Jones

      Yes, this is exactly right. You can fix up the hint paths in the project files with something like this:

      (I can’t take credit for this – I found it somewhere…. ).
      This leaves the project files in a more robust state than it was originally with relative hint paths.

      • While I haven’t tested this, it’s probably _better_ than the update-package -reinstall option in most cases. update-package -reinstall does more than just fix package paths. It also runs each package’s installation script, which could have negative effects on your environment. update-package -reinstall can also take a very long time depending on the number of packages you use.

  • bit bonk

    Is there a way to do the same thing outside the package manager console? For development and debugging I would like to provide a single script (.cmd or .ps1) that I can doubleclick that does this.