Fork me on GitHub

How to restore nuget packages for solution

Gentoo Mono Handbook
By default, the NuGet.Config file instructs NuGet to bypass adding package binaries to source control.

# Ignore NuGet Packages
# Ignore the packages folder
# except build/, which is used as an MSBuild target.
# Uncomment if necessary however generally it will be regenerated when needed

Three approaches

NuGet offers three approaches to using package restore.
Command-Line Package Restore is required when building a solution from the command-line; it was introduced in early versions of NuGet, but was improved in NuGet 2.7.
The MSBuild-integrated package restore approach is the original Package Restore implementation and though it continues to work in many scenarios, it does not cover the full set of scenarios addressed by the other two approaches.
Automatic Package Restore is the NuGet team's recommended approach to Package Restore within Visual Studio, and it was introduced in NuGet 2.7.

Command-Line Package Restore

nuget restore TheSolutionFilname.sln

MSBuild-Integrated approach

In the old way, you right click on your solution in VS and choose Enable package restore. This causes VS to modify your csproj files, and create .nuget folder containing nuget.exe and some other files.

NuGet modifies the project files in the solution to reference the NuGet.targets file so it can participate in the build process.
the presence of a NuGet.targets file determines whether NuGet will continue to use the MSBuild-Integrated approach.

Each project file contain the fragment like
<Import Project="$(SolutionDir)\.nuget\nuget.targets" />  
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">  
        <ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them.  For more information, see The missing file is {0}.</ErrorText>
    <Error Condition="!Exists('$(SolutionDir)\.nuget\NuGet.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\.nuget\NuGet.targets'))" />

Automatic Package Restore

Nuget Automatic Package Restore has changed in Nuget 2.7+. Do not mix 'old' and new methods for automatic package restoration.

Edit each project file (e.g., .csproj, .vbproj) in the solution and remove any references to the NuGet.targets file. Open the project file(s) in the editor of your choice and remove the settings

when building from the command line, you need to run ‘nuget restore’ yourself before msbuild.


An example of content:
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="" ToolsVersion="4.0">

	<PropertyGroup Condition="'$(OS)' == 'Windows_NT'">

	<PropertyGroup Condition="'$(OS)' != 'Windows_NT'">

	<Target Name="RestorePackages" BeforeTargets="Build" DependsOnTargets="GetNuGet">
		<Exec Command="&quot;$(NuGetExe)&quot; Restore &quot;$(SolutionPath)&quot;" Condition="'$(SolutionPath)' != ''" />
		<Exec Command="&quot;$(NuGetExe)&quot; Restore &quot;%(Solution.Identity)&quot;" Condition="'%(Solution.Identity)' != ''" />

	<PropertyGroup Condition="'$(OS)' == 'Windows_NT'">
		<CodeTaskAssembly Condition="'$(MSBuildAssemblyVersion)' == ''">$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll</CodeTaskAssembly>
		<!-- In VS2013, the assembly contains the VS version. -->
		<CodeTaskAssembly Condition="'$(MSBuildAssemblyVersion)' == '12.0'">$(MSBuildToolsPath)\Microsoft.Build.Tasks.v12.0.dll</CodeTaskAssembly>
		<!-- In VS2015+, the assembly was renamed, hopefully this will be the last condition! -->
		<CodeTaskAssembly Condition="'$(MSBuildAssemblyVersion)' != '' and '$(MSBuildAssemblyVersion)' &gt;= '14.0'">$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll</CodeTaskAssembly>

	<Target Name="GetNuGet" Condition="'$(OS)' == 'Windows_NT' And !Exists('$(NuGetExe)')">
		<MakeDir Directories="$(NuGetPath)" Condition="!Exists($(NuGetPath))" />
		<DownloadNuGet TargetPath="$(CachedNuGet)" Condition="!Exists($(NuGetExe)) And !Exists($(CachedNuGet))" />
		<Copy SourceFiles="$(CachedNuGet)" DestinationFolder="$(NuGetPath)" Condition="!Exists($(NuGetExe))" />

	<UsingTask TaskName="DownloadNuGet" TaskFactory="CodeTaskFactory" AssemblyFile="$(CodeTaskAssembly)" Condition="'$(OS)' == 'Windows_NT'">
			<TargetPath ParameterType="System.String" Required="true" />
			<Reference Include="System.Core" />
			<Using Namespace="System" />
			<Using Namespace="System.IO" />
			<Using Namespace="System.Net" />
			<Using Namespace="Microsoft.Build.Framework" />
			<Using Namespace="Microsoft.Build.Utilities" />
			<Code Type="Fragment" Language="cs">
                try {
                    TargetPath = Path.GetFullPath(TargetPath);
                    if (!Directory.Exists(Path.GetDirectoryName(TargetPath)))

                    Log.LogMessage("Downloading latest version of NuGet.exe...");
                    WebClient webClient = new WebClient();
                    webClient.DownloadFile("", TargetPath);

                    return true;
                catch (Exception ex) {
                    return false;
Here we see, that it just ensures, that nuget restore command was executed once at the start of build process
it can be absent, or just in wrong relative path: