313

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?

sashoalm
  • 75,001
  • 122
  • 434
  • 781
Gyan Parkash
  • 3,233
  • 2
  • 13
  • 10
  • i would prefer console because the extension has no good reviews – Gyan Parkash Nov 19 '16 at 08:43
  • 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. – mslot Apr 16 '20 at 17:53

12 Answers12

327

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

John Smith
  • 7,243
  • 6
  • 49
  • 61
Jeff Albrecht
  • 3,723
  • 1
  • 17
  • 22
  • 2
    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. – svick Mar 11 '17 at 02:06
  • 3
    That unfortunately does not support search or auto-complete. That is, you have to know the exact package name spelling. – Andrew Savinykh Mar 19 '17 at 06:29
  • 8
    You can go to [nuget.org](https://www.nuget.org/) to search packages as you might otherwise do in Visual Studio, then use the command line to install the package you want. – MikeBeaton Apr 01 '17 at 11:10
  • 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.31` – srk May 03 '23 at 18:16
114

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

sashoalm
  • 75,001
  • 122
  • 434
  • 781
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.
shA.t
  • 16,580
  • 5
  • 54
  • 111
Roshan Perera
  • 780
  • 7
  • 12
36

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.

Moses Machua
  • 11,245
  • 3
  • 36
  • 50
  • Ahh, this is interesting. There is no `.csproj` file tick option in the current version. Maybe that's what my problem is. https://github.com/pcislo/vscode-nuget-gallery/issues/15 – woter324 Mar 03 '20 at 20:14
  • Hi @woter324, the problem of the checkbox not showing was identified as some packages in the `.csproj` not having version numbers. See [issue comment](https://github.com/pcislo/vscode-nuget-gallery/issues/15#issuecomment-596127291). I've updated my answer to include that. – Moses Machua Mar 07 '20 at 19:29
  • *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! – John Foll Apr 01 '20 at 00:26
  • I have tested the latest version and it no longer has an issue with missing version numbers – Moses Machua Apr 11 '20 at 16:52
  • 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? – Florent Jun 16 '22 at 13:16
  • @Florent yes there would be several csproj checkboxes if you have multiple projects. – Moses Machua Jun 17 '22 at 15:46
29

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

shA.t
  • 16,580
  • 5
  • 54
  • 111
Eugeniu Znagovan
  • 449
  • 6
  • 11
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.

Chris Cavell
  • 189
  • 2
  • 10
  • 12
    I'm sure this works but having to edit the XML manually seems a shame. – Davos Sep 08 '17 at 03:10
  • 1
    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. – Chris Cavell Sep 09 '17 at 11:27
  • 1
    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. – Davos Sep 10 '17 at 04:13
  • 1
    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. – Chris Cavell Sep 12 '17 at 01:18
  • This is a problem with MS using XML for csproj files instead of something user friendly like yaml. – A.R. Dec 16 '21 at 21:58
15

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

Ali.Asadi
  • 643
  • 10
  • 16
10

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

dotnet add package <package name>
Avi Siboni
  • 686
  • 7
  • 16
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
nullify0844
  • 4,641
  • 1
  • 22
  • 16
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

gool
  • 295
  • 5
  • 12
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"
   }
}
Mauricio Aviles
  • 1,074
  • 9
  • 24
3

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

dotnet add package <package name>
Felipe Augusto
  • 1,341
  • 1
  • 16
  • 18