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.

.gitignore

https://docs.nuget.org/consume/package-restore
# Ignore NuGet Packages
*.nupkg
# Ignore the packages folder
**/packages/*
# except build/, which is used as an MSBuild target.
!**/packages/build/
# Uncomment if necessary however generally it will be regenerated when needed
#!**/packages/repositories.config

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

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.

NuGet.targets

An example of content:
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">

	<PropertyGroup Condition="'$(OS)' == 'Windows_NT'">
		<NuGetPath>$(MSBuildThisFileDirectory).nuget</NuGetPath>
		<NuGetExe>$(NuGetPath)\NuGet.exe</NuGetExe>
		<CachedNuGet>$(LocalAppData)\NuGet\NuGet.exe</CachedNuGet>	
	</PropertyGroup>

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

	<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)' != ''" />
	</Target>

	<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>
	</PropertyGroup>

	<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))" />
	</Target>

	<UsingTask TaskName="DownloadNuGet" TaskFactory="CodeTaskFactory" AssemblyFile="$(CodeTaskAssembly)" Condition="'$(OS)' == 'Windows_NT'">
		<ParameterGroup>
			<TargetPath ParameterType="System.String" Required="true" />
		</ParameterGroup>
		<Task>
			<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">
				<![CDATA[
                try {
                    TargetPath = Path.GetFullPath(TargetPath);
                    if (!Directory.Exists(Path.GetDirectoryName(TargetPath)))
                        Directory.CreateDirectory(Path.GetDirectoryName(TargetPath));

                    Log.LogMessage("Downloading latest version of NuGet.exe...");
                    WebClient webClient = new WebClient();
                    webClient.DownloadFile("https://www.nuget.org/nuget.exe", TargetPath);

                    return true;
                }
                catch (Exception ex) {
                    Log.LogErrorFromException(ex);
                    return false;
                }
            ]]>
			</Code>
		</Task>
	</UsingTask>
</Project>
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:
http://ikeptwalking.com/this-project-references-nuget-packages-that-are-missing-on-this-computer/