How can I get MSBuild to restore any needed NuGet packages

David Thielen 2,281 Reputation points
2020-12-29T17:05:07.117+00:00

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).

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

3 answers

Sort by: Most helpful
  1. Dylan Zhu-MSFT 6,406 Reputation points
    2020-12-30T06:42:55.877+00:00

    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,
    Dylan


    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 6 Reputation points
    2022-04-08T14:53:28.87+00:00

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

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

    1 person found this answer helpful.
    0 comments No comments

  3. flagrant99 21 Reputation points
    2023-07-05T12:30:36.2333333+00:00

    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?