43

I have a .net solution (say A) with multiple projects(say B,C,D). I want to update all nuget packages for all projects in the solution. I know I can update nuget packages using command line but passing in the path to packages.config

nuget update A/B/packages.config

Is there a way to update packages for all packages.configs inside folder A using command line without having to specify them individually? (I know this can be done from inside visual studio.) Something like

nuget update A/*/packages.config
KnightFox
  • 3,132
  • 4
  • 20
  • 35

4 Answers4

50

As found in NuGet documentation, you can type:

Update-Package

This will :

Update all packages in all projects of the current solution to the latest versions.

To open the Package Manager Console:

Tools > NuGet Package Manager > Package Manager Console

Now, in order to have only one instance of all packages, I have, in my solution folder, a file named nuget.config that contains:

<configuration>
  <config>
    <add key="repositoryPath" value="..\Path\To\My\Packages" />
  </config>
</configuration>

You might need to reload your solution in order to make it work properly.

Maxime
  • 8,645
  • 5
  • 50
  • 53
  • 2
    The OP asked for the command line solution and this is not it. The post by Ekk looks to be the correct solution for a command line to update restore and then update to the latest version all packages in a solution. – Chris Ward Oct 09 '18 at 19:46
  • The OP rated this answer as the "accepted answer". Also, the OP already knows the command. He simply didn't know what configurations to put in nuget.config. – Maxime Oct 09 '18 at 20:26
29

You have used command line examples, but your question does not state if you require a command line answer. If you do not require command line, you can right-click on your solution in the Solution Explorer, and select Manage NuGet Packages for Solution ... This displays a dialog where you can make your selections. Other than that, you'd need to write a script at this point in time anyway (as far as I know).

BlackTigerX
  • 6,006
  • 7
  • 38
  • 48
clairestreb
  • 1,243
  • 12
  • 24
  • 1
    Very helpful to know the version of the library in every project – Adrian Jul 04 '19 at 21:48
  • yea except on large solutions this UI takes ages to load, or sometimes not at all. – Michael Brown Mar 20 '20 at 19:42
  • @Michael, day late and a dollar short, but that's the downfall of having nuget on all of your projects. Many gigs I've worked on only have one external solution that has nuget and stores all of the packages in a separate folder than the main solution. It works great on build machines that are not allowed to connect to the internet, and all projects have a file reference to external packages. – clairestreb Apr 17 '21 at 00:55
12

First you have to restore all packages using nuget restore solution_file.sln then update them to latest version by executing nuget update solution_file.sln.

Read more about nuget command line

Updated link to Nuget command line documentation

Nuget Package Manager Console documentation (Visual Studio for Windows)

Edit: Previous link is dead. Added working equivalent and bonus Package Manager Console link.

Ryan Varley
  • 361
  • 3
  • 11
Ekk
  • 5,627
  • 19
  • 27
2

You could use nuget's new "Central Package Management" feature.

Example problem:

Suppose you have monorepo (i.e. VS "solution" or VSCode "workspace") with multiple projects.

ProjectA.csproj:

<ItemGroup>
  <PackageReference Include="Foo.Bar.Baz" Version="1.0.0" />
  <PackageReference Include="Spam.Ham.Eggs" Version="4.0.0" />
</ItemGroup>

ProjectB.csproj:

<ItemGroup>
  <PackageReference Include="Foo.Bar.Qux" Version="1.2.3" />
  <PackageReference Include="Spam.Ham.Eggs" Version="4.5.6" />
</ItemGroup>

Some items are the same whereas others differ. And you need to remember to keep the versions in sync - the example shows that you forgot to do that!

Step 1: remove the versions

ProjectA.csproj:

<ItemGroup>
  <PackageReference Include="Foo.Bar.Baz" />
  <PackageReference Include="Spam.Ham.Eggs" />
</ItemGroup>

ProjectB.csproj:

<ItemGroup>
  <PackageReference Include="Foo.Bar.Qux" />
  <PackageReference Include="Spam.Ham.Eggs" Version="4.0.0" />  <!-- note version override for this project -->
</ItemGroup>

Step 2: add file named Directory.Packages.props to your repo's root

<Project>
  <PropertyGroup>
    <ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
  </PropertyGroup>
  <ItemGroup>
    <!-- use 'PackageVersion' rather than 'PackageReference' -->
    <PackageVersion Include="Foo.Bar.Baz" Version="1.2.3" />
    <PackageVersion Include="Foo.Bar.Qux" Version="1.2.3" />
    <PackageVersion Include="Spam.Ham.Eggs" Version="4.5.6" />
  </ItemGroup>
</Project>

Step 3: restore

For each project:

  • clear build output: dotnet clean
  • restore packages: dotnet restore

All your projects will now use the versions you've specified in the config file.

There are more options, like version overrides, and transitive dependency pinining - read the docs for more.

lonix
  • 14,255
  • 23
  • 85
  • 176