How can I get MSBuild to restore any needed NuGet packages

David Thielen 2,751 Reputation points

I've looked at a ton of articles on this including here and here. None provide a solution that I can get to work.

I have everything turned on for automatically doing this in VisualStudio. But on our build machine we build the .sln files using MSBuild, not by opening up VisualStudio. So in MSBuild how can I best do this?

All of our .sln files are for VisualStudio 2019 and set to defaults on handling NuGet (I believe).

Microsoft Technologies based on the .NET software framework.
3,596 questions
Not Monitored
Not Monitored
Tag not monitored by Microsoft.
37,537 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Dylan Zhu-MSFT 6,406 Reputation points

    Hi DavidThielen,

    You could run this command: msbuild [sln file] -t:restore -p:RestorePackagesConfig=true, which could work on my side.
    Note: the package.config file locates at the project directory

    The document: Restoring PackageReference and packages.config with MSBuild

    Best Regards,

    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our **documentation to enable e-mail notifications if you want to receive the related email notification for this thread.**

    6 people found this answer helpful.

  2. Marcello Stagni 11 Reputation points

    If you need to restore and build at once, you can do

    msbuild [sln file filter] -t:restore,build -p:RestorePackagesConfig=true

    2 people found this answer helpful.

  3. flagrant99 21 Reputation points

    It's 2023 and it still doesn't work. Also is this supposed to be -t:restore or /restore? What is the proper way to perform a restore from MSBuild? Anybody know?