Add a package with a local package file in 'dotnet'
Asked Answered
O

6

78

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.

Opponent answered 13/4, 2017 at 19:16 Comment(0)
S
160

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

Seigel answered 9/6, 2017 at 17:25 Comment(16)
This doesn't work for me. ..foo/bin/Debug does not contain a nuspec file, and the error 'is incompatible with 'all' frameworks in project' is all I get. I really don't think using a random folder as a package source works any more with nuget 4 (if it ever did).Penicillium
nupkg, not nuspec in that case. it still works and I have a repo that relies on it (though on windows and backslashes as path). Note that there is a package named foo on nuget so your app most likely will conflict with it. Use dotnet add package foo -v 1.0.0 to get the version.Seigel
There also is still an example at learn.microsoft.com/en-us/nuget/schema/…Seigel
using nuget add ... to add a package generates a folder structure with a nuspec sitting beside the nupkg file. Without the nuspec file, dotnet add package doesn't work for me.Penicillium
Is your nupkg named to convention? my.pkg.1.0.0.nupkg? this is also crucial for nuget to build metadata for the package feed.Seigel
The logic for that is in this file: github.com/NuGet/NuGet.Client/blob/dev/src/NuGet.Core/…Seigel
yes. If it works for you, all I can say is I literally have three machines here and I just tried it and it doesn't work for me on windows, mac or linux; doing exactly what you said... My nuget version is 4.0.0.2283 / 4.1; so ... ?Penicillium
I've created a small example project at github.com/dasMulli/LocalNupkgExample you can run it using ./run-example.shSeigel
I've had a chance to dig in to your example, and it does work, I'm mystified as to why my NuGet.Config doesn't, but at least it's a starting point to work backwards to try to find what I'm doing wrong.Penicillium
I noticed in your sample you have both a Nuget.config file and a RestoreSources block in the csproj file. In your discussion you mention that the RestoreSources is an alternative to the Nuget.config file. Is the RestoreSources block to be used in conjunction with the Nuget.config file?Brewhouse
@Brewhouse as I hoped to make clear with the text between those, they are meant as an alternative to choose from, but the csproj's RestoreSources feature will only work on the upcoming 2.0 release of the dotnet CLI and VS 2017 15.3 Update (both in prerelease at the moment)Seigel
@MartinUllrich, so, with both of them present in the sample on GitHub, which one 'takes precedence'? In my case I don't have the 2.0 prerelease installed so it has to work with the Nuget.config file. But with the prerelease version what happens if, for example, they contain conflicting (or just different) information?Brewhouse
VS2017 seem to doesn't mind your command, but executing from a cmd prompt, it works! Just added some extra "source folders" in my VS2017 configuration as a workaround when developing.Quantify
You win the internet. Our internal NuGet feed would fail on func extenstions install and dotnet add package -s being ignored.Mahau
I did this for VSCode running a C# App using .net Core 3.1.1. I added this NuGet.Config to my main project folder. I added the full path to where I put the nuget package, in the debug folder. I copied my package, mysql.data.8.0.19.nupkg into my bin\Debug\netcoreapp3.1. I ran "dotnet add package mysql.data" and it worked! Thanks Martin Ullrich! Finally a way to get mysql or other nuget packages installed into a dotnetcore app! Cool!Natachanatal
now there's a --add-source <dir of your .nupkg> option.Musketry
P
32

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'.
Penicillium answered 10/6, 2017 at 6:17 Comment(2)
I am not sure how to create a .nupkg file?Nickelsen
@KevinBurton Creating nuget packages is an entire topic all of its own. If you're using .Net core or a recent version of nuget, see learn.microsoft.com/en-us/dotnet/core/tools/…; you can basically just use dotnet pack or nuget pack Foo.csproj; if you're using the full .Net, it's considerably more of a pain to do, you'll have to hunt around for details.Penicillium
C
15

Specify the package's location folder using -s|--source option. For example:

dotnet add package Microsoft.AspNetCore.Cors -s "d:\Cache\localfeed" -f netcoreapp1.1

Enter image description here

UPDATE: Thanks to MartinUllrich's answer: you can't simply install a .nupkg file. It's necessary to specify a local feed and add a local .nupkg file to the feed before you could install the package. Check MartinUllrich's answer for details.

Unfortunately you could face this blocking issue:

Package 'NameOfPackage' is incompatible with 'all' frameworks in project

At this moment it is open and I was able to reproduce it on a stable package version.

Callas answered 13/4, 2017 at 19:36 Comment(6)
This doesn't answer the question: You're describing taking a nupkg already in a nuget repository and installing it; the question is, given only a foo.1.0.0.nupkg how do you install it.Penicillium
@Penicillium what do you think about -v|--version option to specify the exact package version? And yes, it's not necessary to have a local feed to install a package. I've provided more details in answer.Callas
I get the same error (incompatible with all frameworks), but that seems to be a generic error for 'cant find package'. For example, trying dotnet add package ADFADFDFVCXVZD -s .... still gives the same error. I don't think its a framework issue; I think it just can't find the nupkg file. ie. I don't think this works.Penicillium
@Penicillium try to install a package from the VS offline feed: C:\Program Files (x86)\Microsoft SDKs\NuGetPackages In my case it's working fine so I believe the bug is in dotnet pack command.Callas
btw in 90% of the times I've seen the incompatible with 'all' message, it was because the package name was incorrect or there already being a foo, lib etc. nuget package on nuget.org.Seigel
Thank you. It is advantageous. I use it without the -f option and it works for me. dotnet add package packagename -s "h:\nuget\"Herd
V
15

In .NET Core 3.1 (Arm64), I can add the local source and package by

dotnet nuget add source ~/my/nuget-packages/
mv mypackage.1.1.1.nuget ~/my/nuget-packages/
dotnet add package mypackage

and ~/.nuget/NuGet/NuGet.Config was changed.

Valentine answered 16/7, 2020 at 23:51 Comment(0)
H
2

It's not a direct answer to your question, but I am guessing the reason you want to use the package from the local feed is for development purposes and thus it might still be very relevant for you and others.

There is a project that tries to do npm link like experience that is called NuLink. It essentially creates symlinks from cached nuget package to your package bin/Debug folder. It will be a better experience that using local feed.

I am a bit cautious with installing 3d party .exe files, so I have used the simple version of the approach used in NuLink that does not require any 3d party tools:

  1. Install an old version of your package (e.g. 1.0.0). You can just reference it in your consuming project and run dotnet restore e.g.
  2. Rename/remove lib folder from the installed package. Windows example: rename C:\Users\<USERNAME>\.nuget\packages\MyPackage\1.0.0\lib lib_old
  3. Create a symlink/junction from it to your package debug folder. On windows it will be something like this: mklink /J C:\Users\<USERNAME>\.nuget\packages\MyPackage\1.0.0\lib C:\Source\MyPackage\bin\Debug
  4. "Downgrade" to version 1.0.0 of your package in you consuming project

Now your consuming project should be referencing the folder with your .dll/.pdb files from your package for as long as you use 1.0.0. You just build your package, then it should become available right away including navigating to files and debugging (since .pdb file is in the same folder).

P.S. There are probably a lot of tweaks depending on versions of dotnet/nuget you are using, but the gist should still be the same with some variations in folder structure.

Harned answered 20/8, 2021 at 9:15 Comment(0)
W
1

I thought I would throw this Dockerfile on here in case anyone wanted an additional example. My private/local nuget packages exist in a directory ./packages which contains all of the necessary *.nupkg files

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS base

RUN apt-get update && apt-get install -y gcc-multilib && apt-get install -y libcurl3-gnutls
WORKDIR /app
EXPOSE 2275
EXPOSE 44303

FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
WORKDIR /src

COPY . .
WORKDIR /src/SampleWebApp
RUN dotnet nuget add source /src/packages
RUN dotnet restore
RUN dotnet build -c Release -o /app

FROM build AS publish
RUN dotnet publish -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .

EXPOSE 80
ENTRYPOINT ["dotnet", "SampleWebApp.dll"]
Wisnicki answered 30/12, 2020 at 1:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.