ChatGPT解决这个技术问题 Extra ChatGPT

How do I install a NuGet package .nupkg file locally?

I have some .nupkg files from a C# book. How can I install them?

https://i.stack.imgur.com/RMgIm.jpg


C
Cullub

Menu Tools → Options → Package Manager

https://i.stack.imgur.com/BgbB0.png

Give a name and folder location. Click OK. Drop your NuGet package files in that folder.

Go to your Project in Solution Explorer, right click and select "Manage NuGet Packages". Select your new package source.

https://i.stack.imgur.com/jOthh.png

Here is the documentation.


Actually, I have done step 1 a few times. But my package is not showing up in step 2, when I open up to view Installed packages, Updates or Recent packages.
On my screen, I only have "All" under "Installed packages" not the "NuGet offical package source", not my custom "newNuget", they are missing.
I am using vs2010. Trying to download and install latest nuget from their homepage just now. Download click next next...then error...M$ as expected, there is a link of known issues, checked that, says signature mismatch then I need to uninstall the existing nuget and it is easy fix. fine, do that. When I restart computer and do install again, same error comes back, signature mismatch... so now I am stuck with the old version gone, new version doesn't want to install.
Problem fixed. I uninstalled nuget from windows -> control panel. It didn't work that way. I needed to start up VS and go into Tools->Extension Manager ... then hit uninstall nuget from there. Restarted VS. Then went to install nuget again, it worked. Now, the local nupkg files are showing up, too (edit: they are in the Online tab, not the Installed tab, my mistake in the picture I post). Thanks for the help :)
Dont forget to change Package source from right side near the gear icon. you package will show with the name you sepecified in the previous window. thanks
E
Enrico Campidoglio

You can also use the Package Manager Console and invoke the Install-Package cmdlet by specifying the path to the directory that contains the package file in the -Source parameter:

Install-Package SomePackage -Source C:\PathToThePackageDir\

can i do something like that with the command line tool also?
Yes. The -Source option is available in nuget.exe as well. For example: nuget install SomePackage -Source C:\PathToThePackageDir
You might need to specify the -IncludePrerelease flag as well. Otherwise, if the package version has a dash-suffix (e.g "-beta1"), Install-Package won't find it.
This is a much more direct answer than the accepted one. Thanks for the info!
Doesn't work for VS 2017, see f.ex. answer by @Granger.
F
Felix Alcala

For .nupkg files I like to use:

Install-Package C:\Path\To\Some\File.nupkg

Agree. And worth mentioning that only absolute paths work
Doesn't work for VS 2017, see f.ex. answer by @Granger.
@RenniePet that worked for me in VS2017 (but I had already placed my .nupkg under the same directory every other packages were stored)
It is not working for me, I am using VS2013 Version 12.0.21..5 and Nuget package manager 2.12.0.817 :(
I do not get this to work with the nuget.exe cli tool. It tries to find the package using the feeds in the nuget config (GET nuget.COMPANY.nl/nuget/NuGet/… Files (x86)...)
P
Peter Mortensen

For Visual Studio 2017 and its new .csproj format

You can no longer just use Install-Package to point to a local file. (That's likely because the PackageReference element doesn't support file paths; it only allows you to specify the package's Id.)

You first have to tell Visual Studio about the location of your package, and then you can add it to a project. What most people do is go into the NuGet Package Manager and add the local folder as a source (menu Tools → Options → NuGet Package Manager → Package Sources). But that means your dependency's location isn't committed (to version-control) with the rest of your codebase.

Local NuGet packages using a relative path

This will add a package source that only applies to a specific solution, and you can use relative paths.

You need to create a nuget.config file in the same directory as your .sln file. Configure the file with the package source(s) you want. When you next open the solution in Visual Studio 2017, any .nupkg files from those source folders will be available. (You'll see the source(s) listed in the Package Manager, and you'll find the packages on the "Browse" tab when you're managing packages for a project.)

Here's an example nuget.config to get you started:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <packageSources>
        <add key="MyLocalSharedSource" value="..\..\..\some\folder" />
    </packageSources>
</configuration>

Backstory

My use case for this functionality is that I have multiple instances of a single code repository on my machine. There's a shared library within the codebase that's published/deployed as a .nupkg file. This approach allows the various dependent solutions throughout our codebase to use the package within the same repository instance. Also, someone with a fresh install of Visual Studio 2017 can just checkout the code wherever they want, and the dependent solutions will successfully restore and build.


I am using VS 2017 and haven't run into this problem. I just passed the -Source argument to Update-Package and it worked fine. Maybe something to do specifically with the Install-Package command?
Just wanting to point out that this works great on VS 2019, and equally well in DevOps pipelines. Had to do this to be able to use FastReports in our solution that's built & deployed with CI/CD. HOWEVER, the nuget.config had to also have the official repo URL, AND use the Nuget Restore task, NOT DotNetCLI restore, because that creates a copy of the nuget.config so your relative path no longer works.
J
Jeff Puckett

Add the files to a folder called LocalPackages next to you solution (it doesn't have to be called that, but adjust the xml in the following step accordingly) Create a file called NuGet.config next to your solution file with the following contents If the solution is open in Visual Studio, close it, then re-open it.

Now your packages should appear in the browser, or be installable using Install-Package


This may just be because I have a package with the same name as one on the Nuget repository, but I needed to use the Package Manager Console and select the package source as LocalPackages before running Install-Package.
M
Marlon Cristian

If you have a .nupkg file and just need the .dll file all you have to do is change the extension to .zip and find the lib directory.


if you treat it as a zip file, then files having space in the file name will be extracted with % (replacing the spaces). To avoid this install using NuGet.
Actually, you do not even need to rename the extension. Just right-click the file, and then choose Open with, and find an application that can open the .zip file, and the .nupkg file can be opened, and then you can extract the needed dll file to your designated folder.
Best solution I need
W
Willy David Jr

Just to give an update, there are minor changes for Visual Studio 2015 users.

To use or install package manually, go to Tools -> Options -> NuGet Package Manager -> Package Sources

Click the Add button, choose the Source, and don't forget to click "Update" as it will update the folder location for your packages, edit your desired Name of your package source if you want:

https://i.stack.imgur.com/d6Bfk.jpg

To choose your added package, right click your solution and select "Manage Nuget Packages"

The drop down list is on the right and choose Browse to browse your packages that you specified on your folder source. If there is no nuget package on that folder source, this will be empty:

https://i.stack.imgur.com/HCFDE.jpg


u
user7610

On Linux, with NuGet CLI, the commands are similar. To install my.nupkg, run

nuget add -Source some/directory my.nupkg

Then run dotnet restore from that directory

dotnet restore --source some/directory Project.sln

or add that directory as a NuGet source

nuget sources Add -Name MySource -Source some/directory

and then tell msbuild to use that directory with /p:RestoreAdditionalSources=MySource or /p:RestoreSources=MySource. The second switch will disable all other sources, which is good for offline scenarios, for example.


On mac (i'm assuming Linux as well), you can't rely on nuget sources if you are using dotnet restore (or VS Mac) because of this bug. I had to add the local source to the ~/.nuget/NuGet/NuGet.Config manually (see this GH issue).
D
Donghua Liu

Recently I want to install squirrel.windows, I tried Install-Package squirrel.windows -Version 2.0.1 from https://www.nuget.org/packages/squirrel.windows/, but it failed with some errors. So I downloaded squirrel.windows.2.0.1.nupkg and save it in D:\Downloads\, then I can install it success via Install-Package squirrel.windows -verbose -Source D:\Downloads\ -Scope CurrentUser -SkipDependencies in powershell.


D
Duck Ling

pack your library using one of the 3 options:

Visual Studio (csproj > Properties > Package > Tick "Generate NuGet Package on Build". Then Build the solution)

dotnet CLI (in command prompt in project folder: dotnet build, then dotnet pack commands)

NuGet CLI (in command prompt in project folder: dotnet build, then nuget pack command)

add generated package (in project folder bin > Debug (or Release) > *.nupkg file) to the offline feed (default location in VS2019 is C:\Program Files (x86)\Microsoft SDKs\NuGetPackages\) using nuget add command In VS, Manage NuGet Packages > Top-right: Change Package source to Microsoft Visual Studio Online Packages.

Detailed instruction can be found here or on yt