50

How do I enable the nuget package restore feature in Visual Studio 2015?

The feature is just missing in my context menu (Yes - I've double checked that I try it on the solution instead of a project)

My only project is a .NET 4.5 console application - there is no .nuget folder yet. There is an actual nuget package installed.

boop
  • 6,033
  • 9
  • 37
  • 76
  • 3
    Possible duplicate of [How do I Enable NuGet Package Restore in Visual Studio 2015](http://stackoverflow.com/questions/27895504/how-do-i-enable-nuget-package-restore-in-visual-studio-2015) – Dan Beaulieu Oct 02 '15 at 12:49

7 Answers7

55

I had the same problem as you and the way I solved it was to delete the packages folder from my solution and also bin and obj folders from every project in the solution and give it a rebuild.

Martin Shishkov
  • 2,379
  • 4
  • 24
  • 37
  • 2
    Dude thank you! I've been crawling everywhere on the web and this was the real fix after hitting many brick walls – scniro Jul 18 '15 at 18:01
  • This is the most accurate answer and fixed the same issue for me. I tried everything , searched everywhere but nothing worked. I had to run "update-package -reinstall" command multiple times without having my packages.config to get edited and that was so much pain! – Steve Johnson Sep 17 '15 at 14:16
  • 3
    NOTE! Read the answer from BinaryTox below instead! – Jim Aho Apr 13 '16 at 08:44
  • I've had this happen when the `packages` folder got committed to source control. In my case, the `.nupkg` files were there -- so NuGet didn't think there was anything to restore -- **but** the actual DLLs had not been committed (being binary files). For details see my answer [here](http://stackoverflow.com/questions/6876732/how-do-i-get-nuget-to-install-update-all-the-packages-in-the-packages-config/23254566#23254566). – David May 20 '16 at 17:05
37

As it turns out, the real solution is to migrate to Automatic Package Restore. We experienced issues with NuGet after upgrading to VS 2015 after working in 2013 for a while. Turns out the old way of doing NuGet completely hoses the new way of doing it.

The solution is simple, though tedious. Apparently the NuGet.targets file signals VS to use the old NuGet way of doing things, and it's absence means that you are now using "Automatic Package Restore". You can migrate to Automatic Package Restore by following these steps:

  1. Delete .nuget/NuGet.exe
  2. Delete .nuget/NuGet.targets
  3. For each project:
    1. Unload the project
    2. Right click -> Edit the project
    3. Delete all references to the NuGet.targets file, i.e. the following:
<RestorePackages>true</RestorePackages>  
...
<Import Project="$(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>

There is also a powershell script that will perform the migration for you, if you are feeling bold. You can find it on github.

BinaryTox1n
  • 3,246
  • 33
  • 42
  • 2
    It is easier to edit the project files in an external editor, VS will pick up that the file is changed when you save it. – Frode Nilsen Oct 13 '15 at 11:55
  • You also need to make sure you haven't unintentionally committed any of the packages to source/version control. Source control systems will typically ignore the DLLs, but commit the `.nupkg` files to the source repository. At that point NuGet will think there's nothing to restore, **but** your builds may fail. See [my answer to a related question](http://stackoverflow.com/questions/6876732/how-do-i-get-nuget-to-install-update-all-the-packages-in-the-packages-config/23254566#23254566). – David May 20 '16 at 17:16
  • And (as I understand it) NuGet will itself flag the packages as needing to be added to source control. You'll probably want to disable that behavior as described in the [NuGet docs](http://docs.nuget.org/consume/package-restore). – David May 20 '16 at 17:19
  • The [powershell scripts](https://github.com/owen2/AutomaticPackageRestoreMigrationScript) worked for me. – jaycer May 31 '16 at 19:45
  • This work for me when it reports the error `NuGet.targets is missing`. – Hao Aug 10 '16 at 10:26
5

For others this option is available in the Nuget settings section in Visual Studio Options

enter image description here

Eric Herlitz
  • 22,490
  • 25
  • 106
  • 149
  • 1
    I recently had this issue in vs 2015 and looked at my options / packages sources and for some reason nuget was not checked as available package source i clicked on that and it all started working – Ismail Feb 01 '16 at 14:43
  • Could you please tell me how package cache button will come under nuget package manager in vs 2015 – Pankaj Gupta Feb 25 '16 at 06:01
2

I had problem with restoring package with error 401 unauthorised. I found the solution by unchecking available local package sources. Debug > Options > NuGet Package Manager > Package Sources

Only allow nuget.org packages

Megha
  • 449
  • 3
  • 14
2

My Visual Studio 2015 Professional was installed using the default settings. The options for "Allow NuGet to download missing packages" and "Automatically check for missing packages during build in Visual Studio" were already checked by default, but I still could not download the packages. I was getting an error that a package version could not be found.

I needed to click a checkbox under Tools > Options > NuGet Package Manager > Package Sources to enable NuGet Package Restore to work as expected.

enter image description here

laylarenee
  • 3,148
  • 7
  • 30
  • 40
0

I had a problem with accessing the packages folder after starting Visual Studio 2015 in Administrator mode, so I deleted it with the intention of performing a restore.

But no restore was performed when rebuilding the solution, and there was no Restore Packages to be found at the solution level or in the Nuget window.

However, if you right-click the References nodes on the projects in the solution, there is a Restore Packages option. Running this on all projects fixed it for me.

Daniel Persson
  • 2,091
  • 1
  • 16
  • 24
0

enter image description here

Well it is pretty simple with VS2015, all i had to do was to right click on my project solution and click on restore nuget.