21

I use two computers, one with internet connection and the other one without.

I want to install a Nuget package (Nuget.server) with all its dependencies on the offline computer. Unfortunately it is not possible just to download the package itself and I have to download all dependencies manually and there are dozens of them.

How can I create a package on the computer with internet connection that contains all dependencies?

Thanks.

Joël
  • 2,723
  • 18
  • 36
Colleen MacRay
  • 213
  • 1
  • 2
  • 4

6 Answers6

37

I just went through the pain of this and wanted to find a solution using only the NuGet CLI. Turns out it's pretty simple really:

> nuget.exe install <PACKAGENAME> -OutputDirectory <OUTPUTDIR>

The key is the -OutputDirectory switch which makes the CLI install the specified package into a directory that doesn't have a project file in it. Running this command will download the package and all of its dependencies into the output directory, with each package put into a separate sub-folder. You can then grab all of the .nupkg from the output directory and do whatever you need to do with them.

Update: As Igand points out in the comments, the -OutputDirectory switch is not actually required. If omitted nuget.exe will just download into the current folder. Still best to not download it into a folder with a project file in it (unless that is what you're after).

Adrian Sanguineti
  • 2,455
  • 1
  • 27
  • 29
  • 2
    Thanks! You mean run from simple consele, right? Then it works even without -OutputDirectory option, directory with package just gets downloaded into directory with nuget.exe. I use it like this "nuget install NullGuard.Fody -Version 1.8.0", where the name and version is simply copy pasted from info on nuget.org for given package. – Igand May 18 '18 at 09:09
  • @Igand, you're absolutely correct that the `-OutputDirectory` is not needed. I think I must of confused myself when I was searching for a solution, and must have mixed up with another issue. In anycase, since I needed to upload the downloaded packages to a private feed, the `-OutputDirectory` switch helped me organise all the downloaded packages and dependencies into their own folder, making it clear which each package required on its own. – Adrian Sanguineti May 19 '18 at 10:02
  • 1
    If dependencies are not downloading, make sure you specify the -Framework flag (ex: -Framework netstandard2.0) – Zar Shardan Feb 26 '21 at 16:26
12

I had a similar need, so I created NuSave.

Cache a single NuGet package with dependencies

nusave cache package "Newtonsoft.Json@12.0.3" --targetFrameworks ".NETStandard@1.0,.NETStandard@1.3" --cacheDir "C:\path\to\my-local-feed"

Cache multiple NuGet packages from a .csproj file

nusave cache csproj "C:\path\to\project.csproj" --cacheDir "C:\path\to\my-local-feed"

Cache multiple NuGet packages from an .sln file

nusave cache sln "C:\path\to\solution.sln" --cacheDir "C:\path\to\my-local-feed"

Restore & build a solution offline using my local cache

cd C:\path\to\my-solution
dotnet restore --packages C:\path\to\my-local-feed
dotnet build --no-restore
Elias
  • 449
  • 4
  • 9
7

On the computer with internet access the NuGet packages (.nupkg) should be in the local machine cache. On Windows this is in the directory similar to:

C:\Users\YourUsername\.nuget\packages

So you should be able to copy the .nupkg files from there to the computer without internet access. I would create a directory on that computer and setup a new package source pointing to that directory. Alternatively you could copy the .nupkg files to the local machine cache, just be aware there is a limit of 200 NuGet packages in the cache. Then you can create a package source pointing to the cache.

Matt Ward
  • 47,057
  • 5
  • 93
  • 94
  • I didn't find `Cache`, but do see a directory with "`v3-cache` at the end of the path replacing it. It's full of .DAT files, not .nupkg. Not sure if the answer needs an update or I'm in the wrong place. **EDIT:** [Looks like](https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-restore#global-packages-folder) they're in `C:\Users\yourName\.nuget\packages` now? – ruffin Feb 28 '22 at 18:16
  • 1
    Yeah, this answer is quite old. The NuGet packages moved to another directory. https://learn.microsoft.com/en-us/nuget/Consume-Packages/managing-the-global-packages-and-cache-folders – Matt Ward Mar 02 '22 at 15:46
  • Absolutely the right idea, though, imo. I copied the folders for the nugets I needed from `...\.nuget\packages`, copied to a new dir on the "offline" workstation, set up a "local" nuget source in VS by pointing to the directory where I copied them, and `*poof*`, success. +1! – ruffin Mar 02 '22 at 19:22
5

Use the dotnet restore command with the --packages flag, which will download the packages to a specified directory.

dotnet restore --packages <TargetDirectory> <ProjectPath>

Ref: dotnet restore

Specifies the directory for restored packages.

MichaelM
  • 964
  • 7
  • 20
SFinnae
  • 51
  • 1
  • 3
  • 2
    While this code snippet may be the solution, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-‌​code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – colidyre Feb 27 '20 at 00:35
  • This does not include implicit packages in the download if they already exist in a fallback location. You should add `/p:DisableImplicitNuGetFallbackFolder=true` to resolve that. [Source](https://github.com/NuGet/Home/issues/6309#issuecomment-351830787) – MichaelM Feb 08 '23 at 19:41
3

A little late to the discussion here but I just ran into the same issue. I work with a medium size software development shop that works offline. Many new NuGet packages have very large dependency trees. Trying to walk the tree manually and download all required packages was very cumbersome. In the end, I wrote the NuGet Dependency Downloader. With it you give a package ID, optionally a version, and choose if you want to allow pre-release packages. After you click "Start" it will pull down the listed package and any dependencies it needs to run. As an example, when I put in "Microsoft.AspNet.Mvc" and selected "pre-release", this tool brought down 158 packages for MVC 6.0 RC1. Hopefully this will help those working in an offline environment.

https://github.com/StuffOfInterest/NuGetDependencyDownloader

StuffOfInterest
  • 518
  • 3
  • 11
0

In your csproj file:

<Project Sdk="Microsoft.NET.Sdk">


  <PropertyGroup>
    <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
  <PropertyGroup>

Or dotnet publish with all assemblies references.

Dzmitry Lahoda
  • 939
  • 1
  • 13
  • 34