Restore NuGet packages in Azure Pipelines

Azure DevOps Services | Azure DevOps Server 2022 - Azure DevOps Server 2019 | TFS 2018

With NuGet Package Restore you can install all your project's dependency without having to store them in source control. This allows for a cleaner development environment and a smaller repository size. You can restore your NuGet packages using the NuGet restore task, the NuGet CLI, or the .NET Core CLI. This article will show you how to restore your NuGet packages using both the YAML and the classic Azure Pipelines.

Prerequisites

Restore packages with NuGet restore

  1. Navigate to your classic pipeline definition, and then select Edit.

  2. Select + to add a new task. Search for NuGet, and then select Add to add the task to your pipeline.

  3. Name your task and select Restore from the Command.

  4. Select Feed(s) I select here, and select your feed from the dropdown menu. If you want to use your own config file, select Feeds in my NuGet.config and enter the path to your NuGet.config file and the service connection if you want to authenticate with feeds outside your organization.

  5. If you want to include packages from NuGet.org, check the Use packages from NuGet.org checkbox.

  6. Select Save & queue when you are done.

    Screenshot that shows how to configure the NuGet restore task.

Note

Classic NuGet restore uses the NuGetCommand@2 task. By default, this version uses NuGet 4.1.0. Use the NuGet Tool Installer task if you want to use a different NuGet version.

Restore packages with NuGet CLI

Place your nuget.config in the same folder as your .csproj or .slnfile. Your config file should look similar to the following example:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <!-- remove inherited connection strings -->
    <clear />
    <!-- add an Azure Artifacts feed -->
    <add key="FabrikamFiber" value="https://pkgs.dev.azure.com/microsoftLearnModule/_packaging/FabrikamFiber/nuget/v3/index.json" />
    <!-- Get packages from NuGet.org -->
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
  </packageSources>
</configuration>

To restore your NuGet packages, run the following command in your project directory:

nuget.exe restore

Restore packages with the .NET Core CLI task

- task: DotNetCoreCLI@2
  displayName: dotnet restore
  inputs:
    command: restore                      ## The dotnet command to run. Options: build, push, pack, restore, run, test, and custom.
    projects: '**/*.csproj'               ## Path to your csproj file
    feedsToUse: 'select'                  ## Options: select, config
    vstsFeed: '<projectName>/<feedName>'  ## Required when feedsToUse == Select
    includeNuGetOrg: true                 ## Use packages from NuGet.org

Restore packages from feeds in a different organization

To restore NuGet packages from feeds in a different Azure DevOps organization, you must use a personal access token to authenticate.

  1. Navigate to your Azure DevOps organization, and then select User settings > Personal Access Tokens.

    Screenshot showing how to create a personal access token.

  2. Create a personal access token with Packaging (read) scope and copy your PAT to the clipboard.

  1. Navigate to your pipeline definition and select the NuGet restore task. Make sure you're using version 2 or greater.

    Screenshot showing the NuGet restore task version.

  2. Select the Feeds and authentication section, and then select Feeds in my NuGet.config option.

  3. Enter the path to your NuGet.config file.

  4. Select New to add Credentials for feeds outside this organization/collection.

    Screenshot showing how to configure the NuGet restore task.

  5. Select External Azure DevOps Server, and then enter your feed URL (make sure it matches what's in your NuGet.config), a service connection name, and the personal access token you created earlier. Select Save when you are done.

    Screenshot showing how to add a NuGet service connection.

  6. Select Save & queue when you are done.

FAQ

My pipeline is failing to restore my NuGet packages?

The NuGet restore task can fail for several reasons. The most common scenario is when you add a new project that requires a target framework that is not supported by the NuGet version your pipeline is using. This failure doesn't occur generally in the local development environment because Visual Studio takes care of updating your solution accordingly. Make sure you upgrade your NuGet task to the latest version.

How do I use the latest version of NuGet?

For new pipelines, the NuGet Tool Installer will be added automatically to any pipeline that uses a NuGet task. We periodically update the NuGet default version around the same time we install Visual Studio updates on the Hosted build agents.

For existing pipelines, add the NuGet Tool Installer to your pipeline and select the NuGet version for all the subsequent tasks. Check out the dist.nuget to see all the available versions.

Screenshot showing the NuGet tool installer task.