Install a Nuget package in Visual Studio Code
Asked Answered
A

13

324

How can I install a Nuget Package in Visual Studio Code? I know in Visual Studio, we can do this through the Nuget Package Manager console, but how do I do it in VS Code?

Aboutship answered 18/11, 2016 at 10:48 Comment(2)
i would prefer console because the extension has no good reviewsAboutship
The way i do it, is to use nuget.org, search, and just use the PackageReference (located with the other installment options). Works pretty good, and you get a good UI for the nuget it self.Reck
C
338

From the command line or the terminal windows in VS Code editor:

dotnet add <PROJECT> package <PACKAGE_NAME> [options]

Ex.:

dotnet add MyApp package MySql.Data -Version 8.0.31

See this article by Scott Hanselman

Clambake answered 14/2, 2017 at 5:45 Comment(4)
Note that this will work only on the new csproj-based .Net Core SDK 1.0, but not on the old project.json-based preview versions.Seidler
That unfortunately does not support search or auto-complete. That is, you have to know the exact package name spelling.Description
You can go to nuget.org to search packages as you might otherwise do in Visual Studio, then use the command line to install the package you want.Tipton
Notes: (1) I had to use --version or -v ( not -Version); (2) I had to include the .csproj file extension. Full example that worked for me: dotnet add MyApp.csproj package MySql.Data --version 8.0.31Jamille
I
116

Edit: From the comments below:

22 June 2019: "This extension is now unpublished from Marketplace. You can choose to uninstall it." 2¢. – ruffin Jun 22 '19 at 13:23

The provided link above points to ".Net Core Project Manager (Nuget)" - try: marketplace.visualstudio.com/… – samis Oct 3 '19 at 16:14


You can use the NuGet Package Manager extension.

After you've installed it, to add a package, press Ctrl+Shift+P, and type >nuget and press Enter:

enter image description here

Type a part of your package's name as search string:

enter image description here

Choose the package:

enter image description here

And finally the package version (you probably want the newest one):

enter image description here

Iva answered 17/12, 2017 at 15:30 Comment(4)
This extension appears to not support the new workspaces featureOn
This extension seems back on the marketplace, just installed it and works fine for me.Crispy
22 June 2019: "This extension is now unpublished from Marketplace. You can choose to uninstall it." 2¢.Stateless
The provided link above points to ".Net Core Project Manager (Nuget)" - try: marketplace.visualstudio.com/…Sturmabteilung
H
42

You can do it easily using "vscode-nuget-package-manager".

Go to the marketplace and install this. After That:

  1. Press Ctrl+P or Ctrl+Shift+P (and skip 2)
  2. Type ">"
  3. Then select "Nuget Package Manager:Add Package"
  4. Enter package name Ex: Dapper
  5. select package name and version
  6. Done.
Heeheebiejeebies answered 14/9, 2017 at 4:53 Comment(0)
B
39

Nuget Gallery provides a GUI similar to the full Visual Studio. See below.

enter image description here

How To Use:

  1. Install Nuget Gallery from extension marketplace.
  2. Launch from the menu bar View > Command Palette or ⇧⌘P (Ctrl+Shift+P on Windows and Linux). Type Nuget: Open Gallery.
  3. The GUI above is displayed. You can filter just like in regular Visual Studio.
  4. Make sure the .csproj file checkbox is selected, select version from dropdown, and click install button.

UPDATE

Earlier versions, as noted in the comments, had an issue where the .csproj checkbox was not visible when a package in the csproj file was missing a version number like below.

<PackageReference Include="Microsoft.AspNetCore.App" />

This has been fixed in newer versions of the extension so if you have an older version with this issue, please update it to the latest version.

Brecciate answered 23/1, 2020 at 16:52 Comment(6)
Ahh, this is interesting. There is no .csproj file tick option in the current version. Maybe that's what my problem is. github.com/pcislo/vscode-nuget-gallery/issues/15Gagliano
Hi @woter324, the problem of the checkbox not showing was identified as some packages in the .csproj not having version numbers. See issue comment. I've updated my answer to include that.Brecciate
Thanks! That was just what I needed to be able to install the Microsoft.Windows.Compatibility package! This Nuget Gallery actually works with .NET Core 3.1.2! So now I can use OleDbConnection to open a connection to a MS Access Database, while using .NET Core for the other things in Visual Studio Code! Cool!Delighted
I have tested the latest version and it no longer has an issue with missing version numbersBrecciate
What if you wanted to install the nuget package for a specific project if you have multiple projects in one solution? Would there be multiple csproj checkboxes?Ballistic
@Ballistic yes there would be several csproj checkboxes if you have multiple projects.Brecciate
I
30

Open extensions menu (Ctrl+Shift+X), and search ".NuGet Package Manager".

Infarct answered 22/12, 2016 at 11:6 Comment(3)
Strange this manager cant find Microsoft.AspNetCore.Server.Kestrel. Below solution works.Agnostic
Doesn't work any more since the migration to .csproj file format.Vo
I can't see that extension when searching for it.Bump
D
16

Example for .csproj file

  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="1.1.2" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.1.2" />
    <PackageReference Include="MySql.Data.EntityFrameworkCore" Version="7.0.7-m61" />
  </ItemGroup>

Just get package name and version number from NuGet and add to .csproj then save. You will be prompted to run restore that will import new packages.

Doralynne answered 23/5, 2017 at 9:47 Comment(5)
I'm sure this works but having to edit the XML manually seems a shame.Bump
True, but VS Code is free (and really meant as an editor) so I'm not complaining. It would be different if I had to do this in VS.Doralynne
Fair point. So are you editing C# projects entirely in VSCode without any version of Visual Studio installed? Or creating the project in Visual Studio (with sln + csproj files) and then just using VSCode as an editor? I'm guessing if you just use dotnet and VSCode then why would you even need those project files.Bump
No, I actually utilize Visual Studio 2010 & 2015. Prefer 2010 though. All my real coding is also stored in Git repositories. VS Code is more to play around with on my Ubuntu system.Doralynne
This is a problem with MS using XML for csproj files instead of something user friendly like yaml.Twentieth
F
16

nuget package manager gui extension is a GUI tool that lets you easily update/remove/install packages from Nuget server for .NET Core/.Net 5 projects

> To install new package:

  1. Open your project workspace in VSCode
  2. Open the Command Palette (Ctrl+Shift+P)
  3. Select > Nuget Package Manager GUI
  4. Click Install New Package

enter image description here

For update/remove the packages click Update/Remove Packages

enter image description here

Fallon answered 14/12, 2020 at 13:47 Comment(1)
That's plain beautifulBigamous
C
10

If you're working with .net core, you can use the dotnet CLI, for instance

dotnet add package <package name>
Cladoceran answered 6/9, 2020 at 9:30 Comment(0)
H
8
  1. Install NuGet Package Manager
  2. Ctrl+Shift+P on Windows or Command+Shift+P on Mac
  3. Search for NuGet Package Manager: Add Package
  4. Enter package name i.e. AutoMapper
  5. Select package & version
  6. Restore if needed
Helainehelali answered 13/5, 2019 at 6:33 Comment(0)
M
5

The answers above are good, but insufficient if you have more than 1 project (.csproj) in the same folder.

First, you easily add the "PackageReference" tag to the .csproj file (either manually, by using the nuget package manager or by using the dotnet add package command).

But then, you need to run the "restore" command manually so you can tell it which project you are trying to restore (if I just clicked the restore button that popped up, nothing happened). You can do that by running:

dotnet restore Project-File-Name.csproj

And that installs the package

Mellisa answered 22/11, 2018 at 11:39 Comment(0)
B
3

Modify your project.json or *.csproj file. Add a dependency entry with the name of the package and the version desired.

JSON example:

{
   "dependencies" : {

     "AutoMapper": "5.2.0"
   }
}
Bake answered 13/1, 2017 at 7:35 Comment(0)
F
3

Go to folder that have a sln file. Open a terminal (like cmd)

dotnet add package <package name>
Ferity answered 7/4, 2021 at 22:17 Comment(0)
R
0

You can use .NET Add Command

Usage: dotnet add [PROJECT] [command] [options]

Arguments: PROJECT: The project file to operate on. If a file is not specified, the command will search the current directory for one.

Options: -?, -h, --help Show command line help.

Commands:

package <PACKAGE_NAME> Add a NuGet package reference to the project.

reference <PROJECT_PATH> Add a project-to-project reference to the project.

Ribbing answered 26/3 at 11:38 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Amaliaamalie

© 2022 - 2024 — McMap. All rights reserved.