NuGet Package Restore Not Working - nuget

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?

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

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:
The right way to restore NuGet packages
Migrate away from MSBuild-based NuGet package restore
Migrating MSBuild-Integrated solutions to use Automatic Package Restore

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

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

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

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.

If anything else didn't work, try:
Close Project.
Delete packages folder in your solution folder.
Open Project again and restore Nugget Packages again.
Worked for me and it's easy to try.

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.

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.

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.

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...

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.
Backup Everything. Note the list of Dependencies you'll need to reinstall, Exit VisualStudio
Open up all .proj files in a text editor and remove all PackageReference
In each project, delete the bin, obj folders
Delete any "packages" folders you find in the solution.
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.
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.

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.

Automatic Package Restore will fail for any of the following reasons:
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)
You did not enable automatic package restore from the Tools >> Options >> Nuget Package Manager >> General settings.
You forgot to manually remove references in all your projects to the Nuget.targets file
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

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.

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.

I experienced this problem recently as well. The usual 'dotnet restore' and 'Update-Package -reinstall' didn't help.
I looked at the .csproj file and found that the values for these packages were inconsistent. Some were pointing to <solutiondir>\packages and some were <projectdir>\packages.
I made them all point to the same place (I believe that was to <projectdir>\packages) and then I was able to restore the NuGet packages.
I'm not sure how the .csproj file ended up pointing to two different places. Maybe from starting out in Visual Studio 2017 (or earlier) and then moving to Visual Studio 2019? Or different settings used by different programmers? I really don't know.

vs2015 no enable nuget restore problem.
My solution:
add folder .nuget, add file NuGet.Config and NuGet.targets in Directory .nuget
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>

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

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.

There is a shortcut to make Nuget restore work.
Make sure internet connection or Nuget urls are proper in VS Tools options menu
Look at .nuget or nuget folder in the solution, else - copy from any to get nuget.exe
DELETE packages folders, if exists
Open the Package manager console execute this command
paste full path of nuget.exe RESTORE full path of .sln file!
use Install-pacakge command, if build did not get through for any missing references.

The same problem I encountered but what solved my problem was to go to Nuget Package Manager (rightclick to the prj > select Manage Nugget Packages) and uninstall and install again the packages that are having issues.
BUT there are some packages having dependencies to other packages, so what I did was to uninstall the dependencies first and then followed by the nuget package that I wanted to uninstall and re-install.
You can find out if the nuget has dependencies when you tried to uninstall it and then visit the Output window, there will be a line saying that the nuget failed to uninstall because of its dependencies, and those dependencies are also listed there.

I know this is an old thread but it was the first search result today (in 2022) for "nuget package restore canceled". This is the message I got in VS when trying to rebuild a solution at my new job.
I've been in the new job for a few weeks and the build worked up to now.
The package source used by my solution is xxxxPackages, where xxxx is the name of my employer. So the package source is not nuget.org, which I normally use.
Occasionally, a message was appearing in VS saying:
"TF30063: You are not authorized to access xxxxdev.visualstudio.com."
Despite this, I had xxxxdev.visualstudio.com open in my browser and was able to work in it.
I tried many of the suggestions here. Then, chatting to a colleague, it turned out that my VS login had gone stale. In Visual Studio, I clicked on my icon in the top right and selected 'account settings'. There was an exclamation mark indicating that I had to refresh the login to my work account on visualstudio.com, using my xxxx email address.
Once I had done this the problem went away.
I hope this saves others the hassle I had this morning.

Related

restore nuget package conflict

In VS2019, in the Package Manager Console, I start to update all packages of my solution with update-package, but it stops at;
update-package : Unable to find package
'System.Management.Automation_PowerShell_3.0'. Existing packages must
be restored before performing an install or update. At line:1 char:1
update-package
CategoryInfo : NotSpecified: (:) [Update-Package], Exception
FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PackageManagement.PowerShellCmdlets.UpdatePackageCommand
So I right click on my solution and ask to restore nuget packages but then, I have a contradictory piece of information.
All packages are already installed and there is nothing to restore.
Time Elapsed: 00:00:00.0759442
========== Finished ==========
When I manage solution nuget packages (right click on solution, manage nuget packages for solution), in the updates tabitem, I select all packages and ask for update, I have a:
Severity Code Description Project File Line Suppression State
Error Object reference not set to an instance of an object.
Which is not related to my code (build succeeds).
How to proceed ?
Quite strange about this behavior, actually, when you click restore nuget packages by the button and works well which proves that all nuget packages of your solution exists), there is no reason why update-package command does not works.
So please try the following suggestions to troubleshoot the issue:
Suggestions
1) clean all nuget caches
2) check your nuget package sources. check those sources which your nuget packages are installed from and uncheck those sources which you are not used.
Remember to check the private nuget source of System.Management.Automation_PowerShell_3.0.
3) close VS, delete .vs hidden folder under the solution folder, bin and obj folder of your project.
delete all vs component caches under C:\xxx(current user)\perryq\AppData\Local\Microsoft\VisualStudio\16.0_xxx\ComponentModelCache
4) try to use devenv /safemode(a default vs environment without any your third party vs installed extensions or packages) to start your Vs and then test your project.
====================================================================
5) Try to create a new project and then install System.Management.Automation_PowerShell_3.0 and several nuget packages and then test it.
If it does not exist on the new project, I think there may be some conflicting versions of nuget packages in your original project. You should check your installed packages carefully or you could share the packages.config file with us to troubleshoot the issue.
6) first make a backup of your nuget package source. Then, close VS, delete the global nuget.config file under C:\Users\xxx(current user)\AppData\Roaming\NuGet.
Then restart your VS, and then readd the required nuget package sources into VS to test your project.
7) Repair your VS or if your VS2019 is not the latest, please update it which might have a fix about it.

How to enable nugets package restore in Visual Studio 2015?

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.
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.
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:
Delete .nuget/NuGet.exe
Delete .nuget/NuGet.targets
For each project:
Unload the project
Right click -> Edit the project
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.
For others this option is available in the Nuget settings section in Visual Studio Options
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
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.
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.
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.

How do I delete NuGet packages that are not referenced by any project in my solution?

Somehow during the upgrade to VS2012 and .NET 4.5, I've managed to get NuGet confused. There are packages that appear in the package manager (and the packages folder) that I cannot delete (I believe they are legacy ASP.NET NuGet packages that have been replaced with new package names with the new version. They each have only a Manage button -- but no uninstall button. And when you look to see where they are used, you can see that they are referenced by none of the solution's projects? They are NOT displaying an Uninstall?
I've found a workaround for this.
Enable package restore and automatic checking (Options / Package Manager / General)
Delete entire contents of the packages folder (to Recycle Bin if you're nervous!)
Manage Nuget Packages For Solution
Click the restore button.
NuGet will restore only the packages used in your solution. You end up with a nice, streamlined set of packages.
You can use Package Manager Console with command: Uninstall-Package PackageId to remove it, or just delete package folder from 'packages' folder under solution folder.
More information about Package Manager Console you can find here: http://docs.nuget.org/docs/reference/package-manager-console-powershell-reference
First open the Package Manager Console. Then select your project from the dropdown list. And run the following commands for uninstalling nuget packages.
Get-Package
for getting all the package you have installed.
and then
Uninstall-Package PagedList.Mvc
--- to uninstall a package named PagedList.MVC
Message
PM> Uninstall-Package PagedList.Mvc
Successfully removed 'PagedList.Mvc 4.5.0.0' from MCEMRBPP.PIR.
If you want to delete/uninstall Nuget package which is applied to multiple projects in your solutions then go to:
Tools-> Nuget Package Manager -> Manage Nuget Packages for Solution
In the left column where is 'Installed packages' select 'All', so you'll
see a list of installed packages and Manage button across them.
Select Manage button and you'll get a pop out, deselect the checkbox across project name and Ok it
The rest of the work Package Manager will do it for you.
VS2019 > Tools > Options > Nuget Package Manager > General > Click on "Clear All Nuger Cache(s)"
If you have removed package using Uninstall-Package utility and deleted the desired package from package directory under solution (and you are still getting error), just open up the *.csproj file in code editor and remove the tag manually. Like for instance, I wanted to get rid of Nuget package Xamarin.Forms.Alias and I removed these lines from *.csproj file.
And finally, don't forget to reload your project once prompted in Visual Studio (after changing project file). I tried it on Visual Studio 2015, but it should work on Visual Studio 2010 and onward too.
Hope this helps.
An alternative, is install the unused package you want to delete in any project of your solution, after that, uninstall it and Nuget will remove it too.
A proper uninstaller is needed here.
Solution 1
Use the powershell pipeline to get packages and remove in single statement like this
Get-Package | Uninstall-Package
Solution 2
if you want to uninstall selected packages follow these steps
Use GetPackages to get the list of packages
Download Nimble text software
Copy the output of GetPackages in NimbleText(For each row in the list window)
Set Column Seperator to ( if required
Type Uninstall-Package $0 (Substitute using pattern window)
Copy the results and paste them in Package Manage Console
That be all folks.
One NuGet package can reference another NuGet package. So, please be very careful about inter-package dependencies. I just uninstalled a Google map package and it subsequently uninstalled underlying packages like Newtonsoft, Entity Framework, etc.
So, manually deleting particular package from packages folder would be safer.
From the Package Manager console window, often whatever command you used to install a package can be used to uninstall that package. Simply replace the INSTALL command with UNINSTALL.
For example, to install PowerTCPTelnet, the command is:
Install-Package PowerTCPTelnet -Version 4.4.9
To uninstall same, the command is:
Uninstall-Package PowerTCPTelnet -Version 4.4.9
If you want to use Visual Studio option, please see How to remove Nuget Packages from Existing Visual Studio solution:
Step 1:
In Visual Studio, Go to Tools/NuGet Package Manager/Manage NuGet Packages for Solution…
Step 2:
UnCheck your project(s) from Current solution
Step 3:
Unselect project(s) and press OK

How do I get NuGet to install/update all the packages in the packages.config?

I have a solution with multiple projects in it. Most of the third party references are missing, yet there are packages.config file for each project. How do I get NuGet to install/update all the packages needed? Does this need to be done via command line for each project?
You can use nuget.exe to restore your packages or with NuGet 2.7, or above, installed you can simply compile your solution in Visual Studio, which will also restore the missing packages.
For NuGet.exe you can run the following command for each project.
nuget install packages.config
Or with NuGet 2.7 you can restore all packages in the solution using the command line.
nuget restore YourSolution.sln
Both of these will pull down the packages. Your project files will not be modified however when running this command so the project should already have a reference to the NuGet packages. If this is not the case then you can use Visual Studio to install the packages.
With NuGet 2.7, and above, Visual Studio will automatically restore missing NuGet packages when you build your solution so there is no need to use NuGet.exe.
To update all the packages in your solution, first restore them, and then you can either use NuGet.exe to update the packages or from within Visual Studio you can update the packages from the Package Manager Console window, or finally you can use the Manage Packages dialog.
From the command line you can update packages in the solution to the latest version available from nuget.org.
nuget update YourSolution.sln
Note that this will not run any PowerShell scripts in any NuGet packages.
From within Visual Studio you can use the Package Manager Console to also update the packages. This has the benefit that any PowerShell scripts will be run as part of the update where as using NuGet.exe will not run them. The following command will update all packages in every project to the latest version available from nuget.org.
Update-Package
You can also restrict this down to one project.
Update-Package -Project YourProjectName
If you want to reinstall the packages to the same versions as were previously installed then you can use the -reinstall argument with Update-Package command.
Update-Package -reinstall
You can also restrict this down to one project.
Update-Package -reinstall -Project YourProjectName
The -reinstall option will first uninstall and then install the package back again into a project.
Or, you can update the packages using the Manage Packages dialog.
Updates:
2013/07/10 - Updated with information about nuget restore in NuGet 2.7
2014/07/06 - Updated with information about automatic package restore in Visual Studio and brought the answer up to date with other changes to NuGet.
2014/11/21 - Updated with information about -reinstall
Open Package Manager Console
View -> Other Windows -> Package Manager Console
Reinstall all packages in ALL PROJECTS of the current solution:
Update-Package -Reinstall
Reinstall all packages in SPECIFIC PROJECT of the current solution (Thanks to unarity and ashes999):
Update-Package -ProjectName 'YourProjectNameGoesHere' -Reinstall
There is another, newer and quicker way to do this from within Visual Studio. Check out this post by David Ebbo, and reference the comments section if you run into trouble. Basically, you do the following in Package Manager prompt:
PM> Install-Package NuGetPowerTools
PM> Enable-PackageRestore
Afterwards, when you build your solution the packages will be automatically installed if they're missing.
Update:
This functionality is built into Nuget 1.6 with visual studio integration so you don't even need to install NuGetPowerTools or type commands. All you have to do is
Right click on the Solution node in Solution Explorer and select
Enable NuGet Package Restore.
Read this article for more details.
Here's another solution if you are using website projects, or don't want to enable NuGet Package restore.
You can use the package manager console to enumerate all the packages in the package.config file and re-install them.
# read the packages.config file into an XML object
[xml]$packages = gc packages.config
# install each package
$packages.packages.package | % { Install-Package -id $($_.id) -Version $($_.version) }
Update-Package -ProjectName 'YourProjectNameGoesHere' -Reinstall
This is best and easiest example I found. It will reinstall all nugets that are listed in packages.config and it will preserve current versions. Replace YourProjectNameGoesHere with the project name.
With the latest NuGet 2.5 release there is now an "Update All" button in the packages manager: http://docs.nuget.org/docs/release-notes/nuget-2.5#Update_All_button_to_allow_updating_all_packages_at_once
I'm using visual studio 2015 and the solutions given above didn't work for me, so i did the following:
Delete the packages folder from my solution and also bin and obj folders from every project in the solution and give it a rebuild.
Maybe you will have the next error:
unable to locate nuget.exe
To solve this: Change this line in your NuGet.targets file and setting it to true:
<DownloadNuGetExe Condition=" '$(DownloadNuGetExe)' == '' ">true</DownloadNuGetExe>
Reference:https://stackoverflow.com/a/30918648 and https://stackoverflow.com/a/20502049
After 3 hours of searching and investigation.
I had problems with it because we have two members in team (using GitHub source control), because we didn't restrict files for packages for sending to remote repository, one of team members was send packages to server and i have pull that changes to my local.
After that i had same problem as PO, also i wasn't be able to publish my API project to server.
At the and I have just used
Update-Package -Reinstall - run this command on Package Manager Console
This command will reinstall all your packages that you have used in your solution. (For every project)
Reinstall all packages in ALL PROJECTS of the current solution:
Update-Package -ProjectName 'NameOfProject' -Reinstall - run this command on Package Manager Console
This command will reinstall all your packages that are in relation with project that you specified after "-ProjectName". And i think that this is better because i had wait for half a hour to reinstall all packages in solution.
For this many thanks to Rodolpho Brock.
Also, I would recommend you that when you pull changes from remote server, to press "Restore packages" button that will be shown by Visual studio.
I believe the first thing you need to do is enable the package restore feature. See also here. This is done at the solution (not project) level.
But that won't get you all the way -- I ran into a similar issue after having enabled the restore feature. (VS2013, NuGet 2.8.)
It turned out I had (unintentionally) committed the packages to source control when I committed the project -- but Visual Studio (and the source control plugin) had helpfully ignored the binaries when performing the check-in.
The problem arose when I created a release branch. My local copy of the dev/main/trunk branch had the binaries, because that's where I had originally installed/downloaded the packages.
However, in the new release branch,
the package folders and .nupkg files were all there -- so NuGet didn't think there was anything to restore;
but at the same time, none of the DLLs were present -- i.e. the third-party references were missing -- so I couldn't build.
I deleted all the package folders in $(SolutionDir)/packages (under the release branch) and then ran a full rebuild, and this time the build succeeded.
... and then of course I went back and removed the package folders from source control (in the trunk and release branch). I'm not clear (yet) on whether the repositories.config file should be removed as well.
Many of the components installed for you by the project templates -- at least for web projects -- are NuGet packages. That is, this issue is not limited to packages you've added.
So enable package restore immediately after creating the project/solution, and before you perform an initial check-in, clear the packages folder (and make sure you commit the .nuget folder to source control).
Disclaimer: I saw another answer here on SO which indicated that clearing the packages folder was part of the resolution. That put me on the right track, so I'd like to give the author credit, but I can no longer locate that question/answer. I'll post an edit if I stumble across it.
I'd also note that Update-Package -reinstall will modify the .sln and .csproj/.vbproj files. At least that's what it did in my case. Which IMHO makes this option much less attractive.
For those arriving here due to the build server falling foul of this, you can create an MSBuild target running the exec command to run the nuget restore command, as below (in this case nuget.exe is in the .nuget folder, rather than on the path), which can then be run in a TeamCity build step immediately prior to building the solution
<Target Name="BeforeBuild">
<Exec Command="..\.nuget\nuget restore ..\MySolution.sln"/>
</Target>
If you Nuget 2.8 install, check the checkbox
Tools >> Nuget Manager >> Package Manager Settings >> Automatically check for missing packages during build
in Visual Studio. If it is checked, then simply rebuild the project will restore all your reference libraries.
I tried Update-Package -reinstall but it fails on a package and stopped processing all remaining packages of projects in my solution.
I ended up with my script that enumerates all package.config files and run Update-Package -Reinstall -ProjectName prj -Id pkg for each project/package.
Hope it can be useful for someone:
$files = Get-ChildItem -Recurse -Include packages.config;
[array]$projectPackages = #();
$files | foreach { [xml]$packageFile = gc $_; $projectName = $_.Directory.Name; $packageFile.packages.package.id | foreach { $projectPackages += #( ,#( $projectName, $_ ) ) } }
$projectPackages | foreach { Update-Package -Reinstall -ProjectName $_[0] -Id $_[1] }
Edit:
This is an error that I had:
Update-Package : Unable to find package 'EntityFramework.BulkInsert-ef6'. Existing packages must be restored before performing an install or update.
Manual run of
Update-Package -Reinstall -ProjectName my_prj -Id EntityFramework.BulkInsert-ef6
worked very well.
now Nuget Package Manager Console in Visual Studio 2012 gives you a "Restore" button automatically as soon it find any package not installed but in there in package.config.
Awesome Feature!
At VS2012 V11, if I use "-Reinstall" at the end of the line it doesn't work.
So I simply used:
Update-Package -ProjectName 'NAME_OF_THE_PROJECT'
I know this is an old post, but thought this could be useful. If you have a need to ignore specific packages during the update process (like any packages that update JavaScript references), use the following PowerShell script (make sure your package source is set to "All" in Package Manager Console):
EDIT 2014-09-25 10:55 AM EST - Fixed a bug in the script
$packagePath = "packages.config"
$projectName = "MyProjectName"
$packagesToIgnore = #(
"bootstrap",
"jQuery",
"jquery-globalize",
"jquery.mobile",
"jQuery.jqGrid",
"jQuery.UI.Combined",
"jQuery.Validation",
"Microsoft.jQuery.Unobtrusive.Validation",
"Modernizr",
"Moment.js"
)
[xml]$packageFile = gc $packagePath
$packagesToProcess = $packageFile.packages.package | Where-Object {$packagesToIgnore -notcontains $_.id}
$packagesToProcess | % { Update-Package -reinstall -projectname $projectName -id $($_.id) }
Don't know since when, but in VS2019 you can do it in an easier way:
right click solution in Solution Explorer
select Manage Nuget Packages for Solution
there are 4 tabs, Browse, Installed, Updates, Consolidate
the Consolidate shows if there is any projects using different version of packages (and in most cases, that's why we want to update all the packages)
the Updates shows if there is any update available in ANY projects. Select all and click update, the job will be done.
In Visual Studio 2017 - When you compile using IDE - It will download all the missing nuget packages and save in the folder "packages".
But on the build machine compilation was done using msbuild.exe. In that case, I downloaded nuget.exe.
During each build process before executing msbuild.exe. It will execute -> nuget.exe restore NAME_OF_SLN_File (if there is only one .SLN file then you can ignore that parameter).

Problem with uninstalling NuGet Packages

I'm trying to uninstall some packages installed by NuGet. According to the doc described here, I should see an Uninstall button (first screenshot below) when I select the installed package. However, I only see a Manage button (second screenshot below). Can anyone please tell me how to uninstall the packages?
If you scroll down to the Managing Installed Packages section of the NuGet documentation page you linked to you will see the Manage button and an explanation.
In NuGet 1.4 a new feature was introduced allowing you to select the solution in the Solution Explorer and add/remove packages from multiple projects in one step. Previously you had to select each project in the Solution Explorer and add/remove the package from each project one at a time.
If you click the Manage button a Select Projects dialog will be displayed where you can uncheck the projects that you want the NuGet package to be uninstalled from.
If you select the project instead of the solution in the Solution Explorer you can add/remove packages from that selected project. In this case you will only see the Install and Uninstall buttons.
Finally, I found I had to manually edit the .sln solution file and remove these lines:
GlobalSection(ExtensibilityGlobals) = postSolution
EnterpriseLibraryConfigurationToolBinariesPath = packages\Unity.2.1.505.0\lib\NET35;packages\Unity.Interception.2.1.505.0\lib\NET35;packages\EnterpriseLibrary.Common.5.0.505.0\lib\NET35;packages\EnterpriseLibrary.Data.5.0.505.0\lib\NET35
EndGlobalSection
After removing the above lines in the solution file, those packages disappeared in the NuGet Packages Window "Installed package" page. There is only one package left and I can now see the "Uninstall" button.
EDIT:
I also had to delete the packages folder in my solution directory.
Sometimes there are dependencies that disallow uninstalling a package, but you need to do so anyway (i.e. if you inadvertently install two different versions of a package with dependencies).
When this happens, the only way to uninstall the packages is to force the uninstallation, and this can only be done through the Package Manager Console.
Before going to the Console, you should open the Manage NuGet Packages window, and note down the Id and Version of your package, which appears on the right pane when you click on the package.
Then you can close the Manage window, and open the console (Package Manager Console), and run this command:
uninstall-package -Id YourPackageId -Force -Version YourPackageVersion
Note the -Force parameter. For example
uninstall-package -Id Unity -Force -Version 3.0.1304.0
This command allows to uninstall the version 3 when it is installed in parallel with version 2, which can't be done throgh the Manage window.
I had similar issues recently. My project worked fine but I couldn't add or remove any packages via the NuGet package Manager anymore because EnterpriseLibrary 5.0.505 was shown as missing from the source. But trying to either restore or delete it failed with an error "unable to find version '5.0.505. of package 'EnterpriseLibrary'".
I traced this down to having the following reference in the packages.config file:
<package id="EnterpriseLibrary" version="5.0.505" targetFramework="net4" />
Comparing this with other solutions to which I added the package more recently I changed it to, the following, which fixed the issue:
<package id="EnterpriseLibrary.Common" version="5.0.505.0" targetFramework="net4" />
I never edited any package.config files manually, so I don't know how the mismatch happened. Maybe the reference changed since originally including the package without updating the config file...?
I had a problem to remove Code first lib CTP5 that had dependences, uninstall did not return any message and did not remove anything. Delete references from packages.config that you need to remove. After that it will not show in nuget anymore and you can reinstall or continue without.
After using the VS IDE Manage NuGet Packages dialog or Package Manager Console you may have to manually remove solution files or other files on the file system that are part of the package not in the packages folder.
I was evaluating the Microsoft.AspNet.FriendlyUrls package which added a master page, a user control, a class code file and a packages.config file to my ASP.NET web application project / solution. I made the mistake to manually undo / remove these files from the solution before using the NuGet Packages dialog to remove the package. Everything got confused at that point.
As others have said, deleting the packages folder was the solution. But then I had to manually figure out the other files outside that folder that also should be deleted.