169

I checked in a project on one computer, checked out on another, and find that the binaries installed by NuGet are missing. I could check them in to source control as well, but it looks like there's a better solution:

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

I followed those instructions, now have a .nuget folder where one should be, have the following entries in my .csproj file:

<RestorePackages>true</RestorePackages>
<Import Project="$(SolutionDir)\.nuget\nuget.targets" />

and yet when I rebuild my solution, the missing packages are not restored.

What am I missing? How can I diagnose this problem?

Eric J.
  • 139,555
  • 58
  • 313
  • 529

20 Answers20

283

Note you can force package restore to execute by running the following commands in the nuget package manager console

Update-Package -Reinstall

Forces re-installation of everything in the solution.


Update-Package -Reinstall -ProjectName myProj

Forces re-installation of everything in the myProj project.

Note: This is the nuclear option. When using this command you may not get the same versions of the packages you have installed and that could be lead to issues. This is less likely to occur at a project level as opposed to the solution level.

You can use the -safe commandline parameter option to constrain upgrades to newer versions with the same Major and Minor version component. This option was added later and resolves some of the issues mentioned in the comments.

Update-Package -Reinstall -Safe

Abel
  • 52,738
  • 19
  • 137
  • 227
Chris Marisic
  • 30,638
  • 21
  • 158
  • 255
  • Just be aware that running a reinstall on a solution can take a long time, you'll need to answer questions on file replacements and it had a bug/conflict with Git source control for me. – Luke Puplett Jun 17 '13 at 13:41
  • Not a good solution - this broke my project so it wouldn't run anymore. – NightOwl888 Sep 12 '13 at 19:16
  • 7
    @NightOwl888 that sounds like something that needs reported to nuget as there should be no way it could do that, unless perhaps you actually always had DLL hell binding issues and by luck it was working, but reinstalling it ended your luck of it working. – Chris Marisic Sep 23 '13 at 20:18
  • This command broke my project as it corrupted the web.config with new unwanted connectionString, completely blew away my existing membership, role provider config, and added other extraneous stuff. I don't know what other horrors await me. @NightOwl888 – subsci Jan 16 '14 at 06:33
  • 4
    @nightowl if you're using source control it shouldn't be that hard to back the changes out. – ErikE Mar 22 '14 at 06:32
  • 4
    The main draw back here is that package versions aren't maintained, so the latest package version will be installed. This can be a problem if your project is not compatible with a new version.. – JDandChips Mar 25 '14 at 17:59
  • 4
    Yeah, the Update-Package -Reinstall worked for me. I have no idea why the IDE just doesn't do it. Everything is set correctly. Ugghh, I swear, NuGet is both good and annoying. – Jeremy Ray Brown Jun 30 '14 at 15:15
  • Has anyone discovered whether this is a bug in NuGet or within certain packages or something? I'm working on automating builds and I feel that I shouldn't have to add an "Update-Package -Reinstall" to my build script. I'd like to be able to complain somewhere and feel well informed :) – Uxonith Aug 19 '15 at 22:55
  • I am using vs2015 and I found the packages\repositories.config file had old references to project repos I no longer used and thus removed them. Then a VS restart and surprisingly the error is at bay, for now. – sobelito Nov 19 '15 at 14:09
  • It's worth mentioning that if you have opened up a project in VS 2012 and you do not have a solution file you will also get this error. In this case save the solution file and rebuild, the missing packages will then reinstall – System24 Tech Nov 27 '15 at 11:38
  • @Abel great update. Don't know when they added that, but very useful to be included. – Chris Marisic Jan 28 '16 at 16:53
  • The following procedure is without risk: Delete the whole content of the _packages_ folder, and either a) use the _Restore NuGet Packages_ menu on the solution item, or b) in Tools | Options | NuGet Package Manager: Activate _Allow NuGet to download missing packages_ and _Automatically check for missing packages during build in Visual Studio_, and build the solution. – Marc Sigrist Jun 16 '17 at 15:45
  • 2
    nice of you to add the "important! this could destroy your project" at the BOTTOM of your answer! – devman Aug 17 '17 at 12:34
  • It doesn't install only one package named "Sustainsys.Saml2.MVC". But I think this answer is still really useful especially when there are a lot of projects in the solution and each project has many packages. – Zeeshan Ahmad Khalil Aug 23 '20 at 15:23
  • I've got a problem now where nuget reinstall seems to not work, even after clearing the nuget cache. It just says No package updates are available from the current package source for project 'XXXXX.vbproj'. For my 2 projects :( Building causes the nuget cache to reload but something not right here, as several references to nuget packages are showing warning icons. Sigh – Zeek2 Apr 01 '21 at 12:01
  • After issuing Update-package -reinstall, all projects is my solution are updated. When I do a build, all projects fail with the error - This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is ..\NBWExcelAddIn\packages\Microsoft.CodeAnalysis.VersionCheckAnalyzer.2.9.6\build\Microsoft.CodeAnalysis.VersionCheckAnalyzer.props. – Doug Kimzey May 04 '21 at 12:27
  • @DougKimzey you could try installing or updating package for NBWExcelAddIn to an earlier or later version and then build – Chris Marisic May 12 '21 at 18:46
26

For others who stumble onto this post, read this.

NuGet 2.7+ introduced us to Automatic Package Restore. This is considered to be a much better approach for most applications as it does not tamper with the MSBuild process. Less headaches.

Some links to get you started:

Dave New
  • 34,265
  • 48
  • 183
  • 366
20

You have to choose one way of the following :

Re-installing a package by it's name in all solution's projects:

Update-Package –reinstall <packageName>

Re-installing a package by it's name and ignoring it's dependencies in all solution's projects:

Update-Package –reinstall <packageName> -ignoreDependencies

Re-installing a package by it's name in a project:

Update-Package –reinstall <packageName> <projectName>

Re-installing all packages in a specific project:

Update-Package -reinstall -ProjectName <projectName>

Re-installing all packages in a solution:

Update-Package -reinstall 
Mohammad Dayyan
  • 18,338
  • 35
  • 143
  • 207
20

Did you enable package restore mode in the project that has the missing packages/binaries ? There's a known issue that requires the packages to be correctly installed when enabling the restore mode :

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


Original link is dead; this might be a replacement: https://github.com/NuGet/Home/issues/1968

StayOnTarget
  • 7,829
  • 8
  • 42
  • 59
Alexandre Dion
  • 8,762
  • 2
  • 39
  • 29
  • 3
    Thanks for the link. Enable package restore mode in a project that has the missing packages/binaries will be a common case. If you don't have the packages, that's when you want to get them. Use-case fail. – Anthony Aug 22 '12 at 12:24
  • 2
    When you say, "enable package restore mode in the project that has the missing packages", what do you mean? Is there a console command that I need to run to do that? – CodeWarrior Nov 07 '12 at 16:51
  • 94
    NuGet fails me on a daily basis, I despise it utterly. – Jammer Jun 03 '13 at 13:17
16

VS 2017

Tools>NuGet Package Manager>Package Manager Settings>General Click on "Clear All NuGet Cache(s)"

Niaz Morshed
  • 161
  • 1
  • 3
  • When checking a solution out of source control (managed using the Visual Studio Team Explorer with DevOps/Git) builds were not possible due to missing references and restore packages did nothing. This solution was the correct one for this circumstance. – PJRobot Jun 09 '20 at 08:56
  • Thank you very much. It solved my problem with testing local NuGet package! – user-7391153 Sep 17 '20 at 18:37
11

I have run into this problem in two scenarios.

First, when I attempt to build my solution from the command line using msbuild.exe. Secondly, when I attempt to build the sln and the containing projects on my build server using TFS and CI.

I get errors claiming that references are missing. When inspecting both my local build directory and the TFS server's I see that the /packages folder is not created, and the nuget packages are not copied over. Following the instructions listed in Alexandre's answer http://nuget.codeplex.com/workitem/1879 also did not work for me.

I've enabled Restore Packages via VS2010 and I have seen builds only work from within VS2010. Again, using msbuild fails.My workaround is probably totally invalid, but for my environment this got everything working from a command line build locally, as well as from a CI build in TFS.

I went into .\nuget and changed this line in the .nuget\NuGet.targets file:

from:

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

to: (notice, without the quotes around the variables)

<RestoreCommand>$(NuGetCommand) install $(PackagesConfig) -source $(PackageSources) -o $(PackagesDir)</RestoreCommand>

I understand that if my directories have spaces in them, this will fail, but I don't have spaces in my directories and so this workaround got my builds to complete successfully...for the time being.

I will say that turning on diagnostic level logging in your build will help show what commands are being executed by msbuild. This is what led me to hacking the targets file temporarily.

PerryM
  • 163
  • 1
  • 6
  • I had the issue with the double quotes and had to make the same edit that you did. Very frustrating! – Greg Apr 16 '13 at 21:29
5

If anything else didn't work, try:

  1. Close Project.
  2. Delete packages folder in your solution folder.
  3. Open Project again and restore Nugget Packages again.

Worked for me and it's easy to try.

Loaderon
  • 2,338
  • 1
  • 24
  • 36
  • 2
    This worked for me. In step 3, I did not have to manually restore the packages--they restored automatically when I opened the project. – Tawab Wakil Mar 20 '18 at 16:10
5

If none of the other answers work for you then try the following which was the only thing that worked for me:

Find your .csproj file and edit it in a text editor.

Find the <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild"> tag in your .csproj file and delete the whole block.

Re-install all packages in the solution:

Update-Package -reinstall

After this your nuget packages should be restored, i think this might be a fringe case that only occurs when you move your project to a different location.

Jako Basson
  • 1,281
  • 11
  • 18
4

Just for others that might run into this problem, I was able to resolve the issue by closing Visual Studio and reopening the project. When the project was loaded the packages were restored during the initialization phase.

Mike Perrenoud
  • 63,395
  • 23
  • 143
  • 222
4

For me, I had an empty tag NuGetPackageImportStamp in .csproj

<NuGetPackageImportStamp>
    </NuGetPackageImportStamp>

It should ideally contain some valid GUID.

Removing this tag and then "Restore Nugets" worked for me.

Pang
  • 8,605
  • 144
  • 77
  • 113
anu
  • 275
  • 1
  • 3
  • 13
1

Sometimes something strange happens and using Visual Studio to automatically restore doesn't work. In that case you can use the NuGet Package Manager Console. That is opened within Visual Studio from Tools -> NuGet Package Manager -> Package Manager Console. The commands within the console are simple. And to get context help while typing a command just press the button and it will give you all options that start with the letters you're typing. So if a package isn't installed, for example log4net, type the following command:

Install-Package log4net

You can do a whole lot more, like specify the version to install, update a package, uninstall a package, etc.

I had to use the console to help me when Visual Studio was acting like a weirdo.

Jeremy Ray Brown
  • 1,183
  • 12
  • 20
1

Automatic Package Restore will fail for any of the following reasons:

  1. You did not remove the NuGet.exe and NuGet.targets files from the solution's .nuget folder (which can be found in your solution root folder)
  2. You did not enable automatic package restore from the Tools >> Options >> Nuget Package Manager >> General settings.
  3. You forgot to manually remove references in all your projects to the Nuget.targets file
  4. You need to restart Visual Studio (make sure the process is killed from your task manager before starting up again).

The following article outlines in more detail how to go about points 1-3: https://docs.nuget.org/consume/package-restore/migrating-to-automatic-package-restore

CShark
  • 1,885
  • 1
  • 19
  • 36
  • 1
    Regarding your #1 point, the link you provide contradicts your advice: "If you are using TFS 1. Remove the NuGet.exe and NuGet.targets files from the solution's .nuget folder 2. Retain the NuGet.Config file to continue to bypass adding packages to source control." – Andrew Dennison Feb 11 '16 at 23:24
1

I had NuGet packages breaking after I did a System Restore on my system, backing it up about two days. (The NuGet packages had been installed in the meantime.) To fix it, I had to go to the .nuget\packages folder in my user profile, find the packages, and delete them. Only then would Visual Studio pull the packages down fresh and properly add them as references.

Aaron
  • 288
  • 2
  • 6
1

The best workaround that I found creating a new Project from scratch, then import all the source files with the code. My project was not so complicated so I had no problem from there.

Koby Douek
  • 14,709
  • 15
  • 58
  • 84
1

None of the other solutions worked in my situation:

AspNetCore dependencies had been installed/uninstalled and were being cached. 'AspNetCore.All' would refuse to properly update/reinstall/remove. And regardless of what i did, it would use the cached dependencies (that it was not compatible with), because they were a higher version.

  1. Backup Everything. Note the list of Dependencies you'll need to reinstall, Exit VisualStudio
  2. Open up all .proj files in a text editor and remove all PackageReference
  3. In each project, delete the bin, obj folders
  4. Delete any "packages" folders you find in the solution.
  5. Open solution, go into Tools > Nuget Package Manager > Package Manager Settings and Clear all Nuget caches. Check the console because it may fail to remove some items - copy the folder path and exit visual studio.
  6. Delete anything from that folder Reopen solution and start installing nuget packages again from scratch.

If that still doesn't work, repeat but also search your drive in windows explorer for nuget and delete anything cachey looking.

jv_
  • 1,136
  • 12
  • 11
1

In VS2017, right-click on the solution => Open CommandLine => Developer Command Line.

Once thats open, type in (and press enter after)

dotnet restore

That will restore any/all packages, and you get a nice console output of whats been done...

James Joyce
  • 1,414
  • 16
  • 16
0

vs2015 no enable nuget restore problem. My solution:

  1. add folder .nuget, add file NuGet.Config and NuGet.targets in Directory .nuget

  2. each project file add: build

  <RestorePackages>true</RestorePackages>

  <Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
  <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
    <PropertyGroup>
      <ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them.  For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
    </PropertyGroup>
    <Error Condition="!Exists('$(SolutionDir)\.nuget\NuGet.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\.nuget\NuGet.targets'))" />
  </Target>
Floern
  • 31,495
  • 23
  • 98
  • 115
方权军
  • 9
  • 1
  • This is the old way of doing it. See @davenewza answer, and his link to http://blog.davidebbo.com/2014/01/the-right-way-to-restore-nuget-packages.html – timB33 May 23 '17 at 11:12
0

If the error you are facing is "unable to connect to remote server" as was mine, then it would benefit you to have this check as well in addition to the checks provided in the above comments.

I saw that there were 2 NUGET Package Sources from which the packages could be downloaded (within Tools->Nuget Package Manager->Packager Manager Settings). One of the Package Source's was not functioning and Nuget was trying to download from that source only.

Things fell into place once I changed the package source to download from: https://www.nuget.org/api/v2/ EXPLICTLY in the settings

vamsee
  • 31
  • 1
  • 9
0

In my case, an aborted Nuget restore-attempt had corrupted one of the packages.configfiles in the solution. I did not discover this before checking my git working tree. After reverting the changes in the file, Nuget restore was working again.

Frederik Struck-Schøning
  • 11,909
  • 7
  • 55
  • 63
0

There is a shortcut to make Nuget restore work.

  1. Make sure internet connection or Nuget urls are proper in VS Tools options menu

  2. Look at .nuget or nuget folder in the solution, else - copy from any to get nuget.exe

  3. DELETE packages folders, if exists

  4. Open the Package manager console execute this command

  • paste full path of nuget.exe RESTORE full path of .sln file!
  1. use Install-pacakge command, if build did not get through for any missing references.
Pang
  • 8,605
  • 144
  • 77
  • 113
HydTechie
  • 670
  • 8
  • 17