155

Suppose I have a solution with 3 projects:

  • Core
  • UI
  • Tests

Some of the NuGet packages I use will apply to all 3 projects. Some will just apply to UI and Tests, and some will just apply to Tests (like NUnit).

What is the right way to set this up using NuGet?

  1. Should I use "Add Library Package Reference" on all three projects any time I need a reference?
  2. Should I use "Add Library Package Reference" the first time I need a package, and then use Add Reference->Browse for subsequent usages?

In either case, how many packages.config files should I have?

Paul Stovell
  • 31,336
  • 15
  • 76
  • 107

6 Answers6

241

For anybody stumbling across this, now there is the following option :

Right-click your solution > Manage NuGet Packages for Solution...

... Or:

Tools > Library Package Manager > Manage NuGet Packages for Solution...

And if you go to the Installed packages area you can 'Manage' a single package across every project in the solution.

Jason
  • 2,557
  • 1
  • 14
  • 5
  • 5
    What if I want to install package for Solution with specific version. Wizard does not give me that option. In console I can run it like that `Install-Package RazorEngine -Version 3.3.0` but it will be applied only for project which is selected in console dropdown. – MaciejLisCK Feb 07 '14 at 16:13
  • Is this supported in Visual Studio 2010. I just asked a question here -http://stackoverflow.com/questions/33163987/manage-nuget-package-for-solution-vs-2010-does-not-show-manage-button – user2645830 Oct 16 '15 at 06:50
  • If you have a seriously large project, would you really trust a package manager to update dependencies? – Mick Nov 12 '15 at 23:15
  • Do you have any idea when there is no "Manage" buttons for some specific package? Like System.ServiceModel, I cannot manage this library from solution view. – Hoàng Long Dec 30 '15 at 06:57
  • @Mick, check out the Consolidate tab on the mentioned dialog... I would! –  Mar 09 '16 at 14:12
76

Use the console to target multiple projects

Tools > Library Package Manager > Package Manager Console

then use this command

Get-Project PROJECT-NAMES-WITH-COMMAS | Install-Package PACKAGENAME

for example

Get-Project Core,UI | Install-Package FluentDateTime
Simon
  • 30,844
  • 15
  • 120
  • 187
  • 4
    +1: At least with the current NuGet version, I still prefer this method over the "Manage NuGet Packages for Solution" dialog, because our solution is really large, and I can't paste a list of project names into that dialog. – Yodan Tauber Sep 18 '13 at 06:40
21

This sweet deal works for me:

PM> Get-Project -all | where {$_.Name -match "Songhay.Silverlight" -and
    $_.Name -notmatch "ApplicationLoader" -and $_.Name -notmatch ".Xml"}
    | ForEach-Object {Install-Package MvvmLight -project $_.Name}
rasx
  • 4,947
  • 2
  • 40
  • 58
  • 2
    This is how it is done! For example if you want to list all of the versions of json.net for all of your projects in a solution try this: `Get-Project -all | ForEach-Object {Get-Package -ProjectName $_.Name -Filter Newtonsoft.Json}` – jonypony3 Sep 18 '14 at 01:44
  • 1
    @EdwardMMeshuris very good command but it will not get exact match for the package name. Here is a slightly updated for exact match `Get-Project -all | ForEach-Object {Get-Package -ProjectName $_.Name -filter PACKAGE_NAME} | where-object { $_.id -eq 'PACKAGE_NAME' }` – Dark_Knight Oct 02 '17 at 10:17
12

If you want to install a package across multiple solutions I wrote a handy Powershell script for doing it, see here.

You can even filter the Get-Project -All command and target a sub-set of the project list.

Aaron Powell
  • 24,268
  • 14
  • 93
  • 147
8

You should use the "Add Library Package Reference" for all your external library on every project in your solution. You'll end up with a packages.config per project.

However, you'll download the package only one time and reuse them locally for all your other projects.

Nekresh
  • 2,788
  • 21
  • 28
  • 4
    Note: If you don't update all the library packages, you will end up with 3 different versions of the same assembly. .NET can work with 3 different versions if they're in the GAC or with different names. But because they will have the same name. You will end up with nonworking builds because the old version was copied over the newer version. – graffic Dec 29 '11 at 16:52
2

In Package Manager Console you can write the following command:

Get-Project -all | ForEach-Object {Get-Package -ProjectName $_.Name -filter 
PACKAGE_NAME} | where-object { $_.id -eq 'PACKAGE_NAME' } | Install-Package 
PACKAGE_NAME -Version VERSION

You can use that command for install or update as well (Update-Package)

Dark_Knight
  • 327
  • 3
  • 18