Add a package with a local package file in 'dotnet'
C#Nuget.Net CoreC# Problem Overview
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.
C# Solutions
Solution 1 - C#
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
Solution 2 - C#
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'.
Solution 3 - C#
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
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.
Solution 4 - C#
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.
Solution 5 - C#
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:
- 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.
- Rename/remove
lib
folder from the installed package. Windows example:rename C:\Users\<USERNAME>\.nuget\packages\MyPackage\1.0.0\lib lib_old
- 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
- "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.
Solution 6 - C#
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"]