How to restore NuGet packages from the console when Visual Studio refuses

How to restore NuGet packages from the console when Visual Studio refuses

On occasion, and especially if I switch branches in git, my Visual Studio projects will load without any of the dependencies they rely on (generally in the packages folder*). Telling Visual Studio to restore all packages in various ways (including a full re-build) always seems to fail... Visual Studio's package manager thinks everything is already up to date... which it clearly is not.

So, command line to the rescue. You have two options:

  • Use nuget.exe
  • Use the Package Manager Console in Visual Studio (recommended)

Using NuGet

From the command line, you can run the following nuget.exe command for each project:

nuget install packages.config

Or with NuGet 2.7 you can restore all packages in the solution using the command:

nuget restore YourSolution.sln

Both of these will pull down the packages.

Note: Your project files will not be modified when running this command, so your project should already have references to any applicable 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 packages in your solution, first restore them, and then you can either use NuGet.exe to update the packages, or you can update the packages from the Package Manager Console window within Visual Studio (see below), 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 https://nuget.org

nuget update YourSolution.sln

Note: this will not run any PowerShell scripts in any NuGet packages.

Using Package Manager Console

First, make sure you have the Package Manager Console open (Tools > NuGet Package Manager > Package Manager Console) and enter the following command:

Update-Package -reinstall
Attempting to gather dependency information for multiple packages with respect to project 'openid', targeting '.NETFramework,Version=v4.7.2'
Gathering dependency information took 1.57 min
Attempting to resolve dependencies for multiple packages.
Resolving dependency information took 0 ms
Resolving actions install multiple packages
etc...

If you need to only update packages for a specific project (rather than the whole solution) just use the following:

Update-Package -reinstall -project [ProjectName]

If you're using the newer Package Reference method of including packages in your project (as opposed to the packages.config file method) you probably won't suffer this problem as the solution keeps references to the packages internally.

See also:

NuGet Package Restore
See an overview of how NuGet restores packages a project depends on, including how to disable restore and constrain versions.
Reinstalling and Updating NuGet Packages
Details on when it’s necessary to reinstall and update packages, as with broken package references in Visual Studio.
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 pa...