Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a package with a local package file in 'dotnet'

Using the dotnet command line tool, how can I add a reference to an existing local package that is not downloaded with NuGet?

I have tried adding a local package to a project bar with dotnet:

dotnet add package /Users/sakra/foo/bin/Debug/foo.1.0.0.nupkg 

The package foo.1.0.0.nupkg has been created with dotnet pack in a different project. The command dotnet add package however tries to download the file foo.1.0.0.nupkg from https://api.nuget.org/ which of course fails.

like image 883
sakra Avatar asked Apr 13 '17 19:04

sakra


People also ask

How do I add a local NuGet package to Visual Studio?

From Visual Studio's menu, select Tools > Options, and then enter NuGet in the search input. Select the suitable Package Sources, as shown in Figure 1, followed by clicking the green plus button to add your path from set up local feed folder. Provide a name, and then click the OK button.


2 Answers

There isn't a way to directly install a single .nupkg package. NuGet can only install and restore from feeds, so you'll need to add the directory where the package is in as a feed.

To do this, add a NuGet.Config file that adds the location of the directory as a feed, so you don't have to add the source parameter to each NuGet-related command (especially dotnet restore):

<?xml version="1.0" encoding="utf-8"?> <configuration>   <packageSources>     <add key="local-packages" value="../foo/bin/Debug" />   </packageSources> </configuration> 

Alternatively in .NET Core 2.0 tools / NuGet 4.3.0, you could also add the source directly to the .csproj file that is supposed to consume the NuGet feed:

<PropertyGroup>   <RestoreSources>$(RestoreSources);../foo/bin/Debug;https://api.nuget.org/v3/index.json</RestoreSources> </PropertyGroup> 

This will make all commands be able to use the package:

  • dotnet add package foo (optionally add -v 1.0.0)
  • dotnet restore
  • dotnet run

dotnet add package foo will add a package reference (assumption here, version 1.0.0) to *.csproj:

<ItemGroup> +    <PackageReference Include="foo" Version="1.0.0" /> </ItemGroup> 

Note that during development, if you change the NuGet package, but don't increment its version in both the project that produces the .nupkg file and in the project that consumes it, you'll need to clear your local packages cache before restoring again:

dotnet nuget locals all --clear dotnet restore 

I have created a small example project at https://github.com/dasMulli/LocalNupkgExample

like image 131
Martin Ullrich Avatar answered Oct 09 '22 13:10

Martin Ullrich


I've struggled with this a lot, and this the only way that I can make it work:

  • Create a new 'package source' to use

  • Install the .nupkg file into the package source, using nuget add ...

  • Use dotnet add package Foo -s ... to install the package.

Specifically, the commands you need to use are:

nuget add ../whatever/lib/MyPackage.1.0.0.nupkg -Source ./packages

And:

dotnet add package MyPackage -s ./packages

Notice specifically, a couple of points here:

  • First, you cannot simply copy the .nupkg file into the packages folder. I've tried lots of variations of this, and all I can say is that it does not work for me, on Windows, Mac, or Linux.

  • You must use a version of NuGet which is at least 3, or this doesn't work. The default version for .NET Core is 2.xx at the time of writing. You will need to manually upgrade NuGet.

  • Your reference in the .csproj file will look like this:

... PackageReference Include="SolidMud" Version="1.0.0" ...

I.e., specifically note that it does not reference your package source in the dependency information; just the package name and version.

Basically, this means that if you run dotnet restore, it won't work unless the package is cached in your global NuGet cache on that machine; you need to run dotnet restore -s ./packages the first time you do a restore.

As mentioned in another answer, packages are globally cached; if you want to roll to a new version with the same version id, you need to use dotnet nuget locals all --clear or manually delete the cached package version.

Here is a specific, complete example of restoring a .nupkg called 'SolidMud' into a brand new .NET Core console application:

$ dotnet new console -n TestPkgs The template "Console Application" was created successfully.  Processing post-creation actions... Running 'dotnet restore' on TestPkgs/TestPkgs.csproj... Restore succeeded.  $ cd TestPkgs/ $ mkdir packages $ nuget add ../core-solidmud/lib/SolidMud.1.0.0.nupkg -Source ./packages Installing SolidMud 1.0.0. Successfully added package '../core-solidmud/lib/SolidMud.1.0.0.nupkg' to feed './packages'.  $ dotnet add package SolidMud -s ./packages Microsoft (R) Build Engine version 15.3.117.23532 Copyright (C) Microsoft Corporation. All rights reserved.  Writing /var/folders/29/0695l4fj26j64kp4p8vwqq5h0000gn/T/tmpkRBaST.tmp info : Adding PackageReference for package 'SolidMud' into project '/Users/doug/dev/dotnet-packages/TestPkgs/TestPkgs.csproj'. log  : Restoring packages for /Users/doug/dev/dotnet-packages/TestPkgs/TestPkgs.csproj... info : Package 'SolidMud' is compatible with all the specified frameworks in project '/Users/doug/dev/dotnet-packages/TestPkgs/TestPkgs.csproj'. info : PackageReference for package 'SolidMud' version '1.0.0' added to file '/Users/doug/dev/dotnet-packages/TestPkgs/TestPkgs.csproj'. 
like image 41
Doug Avatar answered Oct 09 '22 14:10

Doug