Updating NuGet packages from command-line - deep dive

I set out to find a better way to update NuGet packages across multiple projects, solutions, and git repositories. I have used a combination of command line and Visual Studio until now but wanted to reduce the time spent on the rather boring task of updating packages. This post is a summary of the different things I learned, the tools I discovered, and a surprise at the end 😉

Running on recent versions of the NuGet packages we use on elmah.io has and always has been an important thing for me. New versions contain bug fixes, security patches, performance improvements, and new features. Also, the longer we wait with updating a package, the harder it gets to do the update (new versions contain breaking changes from time to time).

There are three different ways of updating NuGet packages. All four have the disadvantage that you need to manually go through each package one by one. Let's go through them before digging into tools helping with the process.

Visual Studio

You already know the Package Manager extension available in Visual Studio. Right-click Dependencies and select Manage NuGet Packages... to access the tool:

Advantages

  • Available within the IDE.
  • Good overview.
  • Can update packages across projects (within the same solution).

Disadvantages

  • Click click click. Requires a lot of mouse clicks.
  • No scripted updates.

Modify csproj

A fast way to update a NuGet package is through manual edit of the csproj file. This has been made possible after introducing the new and improved project format as part of .NET Core. To update a package, double click the project inside Visual Studio or open the csproj file in your favorite editor. Locate the PackageReference element of the NuGet package you are trying to update and input the new version:

Advantages

  • Fast to update a single package.
  • Supports updating multiple packages across files with regex or similar.

Disadvantages

  • You need to know the version to update to before making the change.

From the command-line

NuGet packages can be easily updated from the command-line using the nuget command. Most people probably use the dotnet tool as a wrapper for nuget why I'll use that tool as an example in this post.

To update a NuGet package from the command-line, use the same syntax as when installing new packages: dotnet add package. To update a package you will need to include the version switch

dotnet add package Spectre.Console --version 0.41.0

You can use -v for short if you want to minimize keystrokes.

Advantages

  • Available without having to launch the IDE.
  • Easily scripted.

Disadvantages

  • You need to know the version to update to before making the change.
  • More typing involved.
  • You need to update one package at a time.

Now that we have looked into the "native" solutions provided as part of .NET, let's have a look at some alternatives.

Third-party tools

As mentioned in the beginning, when starting this deep-dive I was looking for a better way to update packages to newer versions across multiple projects and solutions. I was looking for a tool that would let me choose which version to upgrade to since I typically don't want to simply update all packages to the recent version.

After asking on Twitter I was suggested to look into two different third-party tools. I'll demonstrate how each tool works and introduce you to a third tool in the end.

NuKeeper

The first tool I was recommended was NuKeeper. It has existed since 2018 why I was a bit surprised not knowing about this nice little tool. NuKeeper is a tool able to update multiple packages in one command. There's a great set of options available, making it a good choice if you want to script updates regularly. NuKeeper can be installed as a global tool:

dotnet tool install nukeeper --global

After installing NuKeeper as a global tool, you can run the update command:

As seen from the output, NuKeeper automatically updated the Spectre.Console to the recent version.

You can allow NuKeeper to install prereleases using the --useprerelease option:

Or you can control how to upgrade packages the Semver-based --change option:

Overall, NuKeeper is a great tool that I have already started using in some scenarios.

Advantages

  • Easily scripted.
  • Supports generation of pull requests.

Disadvantages

  • No interactive mode.

dotnet-outdated

The second tool I was suggested was dotnet-outdated. The tool is similar to NuKeeper but focuses more on outdated packages. dotnet-outdated can be easily installed as a global tool too:

dotnet tool install --global dotnet-outdated-tool

Once installed, dotnet-outdated adds an outdated action to the dotnet tool:

As seen from the screenshot, dotnet-outdated presents a list of possible package updates. In this example, only the Spectre.Console package has an available update. To make the update, simply include the --upgrade option:

The Spectre.Console package is updated to the recent stable version. Much like NuKeeper, you can upgrade to prereleases with the --pre-release option, and you can upgrade based on Semver using the --version-lock option:

Pretty sweet.

Advantages

  • Easily scripted.
  • Interactive mode.

Disadvantages

  • Couldn't find any. Interactive mode could offer some more options for picking the right version but none of the tools above have that option either.

NuPU

Ok, so I promised you a surprise to round off this post. Being the nerd I am, I decided to play around with creating a NuGet update tool. The tools above perfectly cover most people's need for updating packages, but I wanted to create something that would fit my wish list. The result is NuGet Package Updater (NuPU). NuPU implements a single use-case: run through a folder and sub-folders and present a list of updates for each NuGet package with newer versions available. Finally, let the user install one or more of those updates.

Install NuPU as a global tool:

dotnet tool install --global NuPU

Navigate to the root of your source code and run the nupu command:

As seen in the screenshot, NuPU will find all project files and go through each NuGet package one by one. For each NuGet package with updates, you will be presented with a list to pick which version to upgrade to or to stay on the current version.

Advantages

  • Let you pick exactly what version to update to.

Disadvantages

  • No way to script updates.
  • Not as advanced as NuKeeper and dotnet-outdated.
  • Probably filled with bugs 😂

elmah.io: Error logging and Uptime Monitoring for your web apps

This blog post is brought to you by elmah.io. elmah.io is error logging, uptime monitoring, deployment tracking, and service heartbeats for your .NET and JavaScript applications. Stop relying on your users to notify you when something is wrong or dig through hundreds of megabytes of log files spread across servers. With elmah.io, we store all of your log messages, notify you through popular channels like email, Slack, and Microsoft Teams, and help you fix errors fast.

See how we can help you monitor your website for crashes Monitor your website