5

Note: I have already read How can I install the VS2017 version of msbuild on a build server without installing the IDE? but this does not answer with a totally GUI-less script-only install.


Along the years, here is what I noticed:

  • I download a project from Github ; or open an old project of mine (say from 4 years ago)

  • run msbuild.exe theproject.sln

  • oops, I don't have the right Visual Studio version / .NET Framework version / the right SDK is missing (example situation here among many others)

  • then spend X hours browsing on websites like https://visualstudio.microsoft.com/downloads/#build-tools-for-visual-studio-2019, install a package, then notice it's not the right one (msbuild still fails), download another package, install it, etc.

  • at the end you have downloaded 8 GB of packages, waited for the download, waited for the install, for the reboot, and you're still not sure it works

  • your computer is now a mess with 5 different versions of SDKs installed at the same time that probably collide with each other (did version Z overwrite/uninstall version Y or not?)

This might be a solution to avoid this problem:

How to install the required MS build tools from command-line? (I don't want to use any IDE, I want to script everything)

If it was possible, I would just, once for all create a build.bat file for every project, that would be something like:

msbuildget --package=VC14 --installdir=c:\buildtools\vc14     # automatically download and install
C:\buildtools\vc14\bin\msbuild.exe myproject.sln

or

msbuildget --package=.NET-35 --installdir=c:\buildtools\net35   
C:\buildtools\net35\bin\msbuild.exe myproject.sln

How to do this?

With this method, even if you open a 6-year old project, you should be able to build it.

Basj
  • 41,386
  • 99
  • 383
  • 673

2 Answers2

6

I've just spent a good while trying to figure this out. I ended up with this:

.\vs_buildtool.exe --passive --wait --add Microsoft.VisualStudio.Workload.VCTools;includeRecommended

Note the use of includeRecommended to avoid adding components individually. includeOptional is also available. You can also use e.g. --includeRecommended flag to include recommended for all workloads (see here).

Or to install using winget:

winget install -e --id Microsoft.VisualStudio.2022.BuildTools --override "--passive --wait --add Microsoft.VisualStudio.Workload.VCTools;includeRecommended"

Hope this might be of use to someone else

Mad
  • 319
  • 3
  • 9
2

How to automate (from command-line) the installation of a Visual Studio Build Tools build environment, for C++ version X, .NET C# version Z, etc

First, you should note that, all the workloads or packages need to be installed and they will be integrated into Build Tool, what you need is the workload Component ID of them.

You can refer to this document to obtain the related Component ID of buildtool.

Besides, this document also list the command line installed instructions. And the order is the same as the build tool.

Suggestion

You can try the below script:

// This is for desktop development and also add the net framwork 3.5
vs_buildtool_xxx.exe --add Microsoft.VisualStudio.Workload.ManagedDesktopBuildTools^ 
                     --add Microsoft.Net.Component.3.5.DeveloperTools^ 
                     --add Microsoft.Net.Component.4.5.2.TargetingPack^
                     --installPath C:\BuildTools

C:\BuildTools\MSBuild\Current\Bin\MSBuild.exe myproject.sln

And you can add any more workloads or packages by commamd -add with the related Component ID.

If you want to build c++ project, you can try the following example:

vs_buildtool_xxx.exe --add Microsoft.VisualStudio.Workload.VCTools^ 
                     --add Microsoft.VisualStudio.Component.VC.Tools.x86.x64^ 
                     --add Microsoft.VisualStudio.Component.VC.140^
                     --installPath C:\BuildTools

C:\BuildTools\MSBuild\Current\Bin\MSBuild.exe myproject.sln

Microsoft.VisualStudio.Component.VC.140 means that VS2015 build tool for C++.


Important note: using command line is quite different from the vs_installer UI. When you click the c++ build tool in vs_installer UI, you could see that it will install related components automatically.

enter image description here

These components are listed under Microsoft.VisualStudio.Workload.VCTools workload and you can choose whether or not to install them.

However, it is not to specify that the workload will install all of them.

When you use command line, it will not install any related components automatically, so you should add them one by one manually.

For c++ projects, you could use these commands to install it:

vs_buildtool.exe --add Microsoft.VisualStudio.Workload.MSBuildTools^ 
                 --add Microsoft.VisualStudio.Workload.VCTools^ 
                 --add Microsoft.VisualStudio.Component.VC.Tools.x86.x64^ 
                 --add Microsoft.VisualStudio.Component.Windows10SDK.18362^ 
                 --add Microsoft.VisualStudio.Component.VC.CMake.Project^ 
                 --add Microsoft.VisualStudio.Component.TestTools.BuildTools^ 
                 --add Microsoft.VisualStudio.Component.VC.ASAN^ 
                 --add Microsoft.VisualStudio.Component.VC.140
Basj
  • 41,386
  • 99
  • 383
  • 673
Mr Qian
  • 21,064
  • 1
  • 31
  • 41
  • Thanks! I'll retry this the next time I'll have to reinstall MSVS. For an unknown reason, I did `vs_buildtools.exe --add Microsoft.VisualStudio.Workload.MSBuildTools --add Microsoft.VisualStudio.Workload.VCTools --add Microsoft.VisualStudio.Workload.ManagedDesktopBuildTools` successfully, but it finally only installed < 1 GB of software (which is surprising). Then I was unable to compile anything, some not-installed files were still needed... – Basj Jun 30 '20 at 08:12
  • Do you see a reason @PerryQianMSFT why `Microsoft.VisualStudio.Workload.MSBuildTools`, `Microsoft.VisualStudio.Workload.VCTools`, `Microsoft.VisualStudio.Workload.ManagedDesktopBuildTools` are **not enough** to be able to simply compile a Hello World `cl.exe test.cpp`? What is missing? (NB: it was *not* a PATH / vcvarsall problem). – Basj Jun 30 '20 at 08:13
  • Command line is different from vs_installer UI. If you want to use C++ workload, you should install the c++ workload and related components. If you use vs_installer to install c++ build workload and when you click on it, it will also install the related tools containing `cl.exe` automatically. However, if you use command line to install the build tool, it will not install the related component and you should add them manually. That is one of their differences. – Mr Qian Jun 30 '20 at 09:16
  • In your situation, for c++ project, you should also add this:`Microsoft.VisualStudio.Component.VC.Tools.x86.x64` – Mr Qian Jun 30 '20 at 09:17
  • for c++ project, use these: `Microsoft.VisualStudio.Workload.MSBuildTools, Microsoft.VisualStudio.Workload.VCTools,Microsoft.VisualStudio.Component.VC.Tools.x86.x64,Microsoft.VisualStudio.Component.Windows10SDK.18362,Microsoft.VisualStudio.Component.VC.CMake.Project,Microsoft.VisualStudio.Component.TestTools.BuildTools,Microsoft.VisualStudio.Component.VC.ASAN,Microsoft.VisualStudio.Component.VC.140` – Mr Qian Jun 30 '20 at 09:23
  • You can open `vs_installer.exe` and click on c++ build tools and you will see all the related workload and components on the right menu. And then you can find their component IDs to add them. You can use my command which I list above to install it. I have listed all of them and it almost 1GB. For c# desktop projects, you should use similar function. – Mr Qian Jun 30 '20 at 09:28
  • 1
    In a word, command line will not install the whole related c++ workload, you should add them one by one. – Mr Qian Jun 30 '20 at 09:29
  • @PerryQianMSFT: see this link: [`Microsoft.VisualStudio.Workload.VCTools`](https://learn.microsoft.com/en-us/visualstudio/install/workload-component-id-vs-build-tools?view=vs-2019#c-build-tools) already includes Microsoft.VisualStudio.Component.VC.Tools.x86.x64 and Microsoft.VisualStudio.Component.VC.140, so I don't understand why it didn't work with `vs_buildtools.exe --add Microsoft.VisualStudio.Workload.MSBuildTools --add Microsoft.VisualStudio.Workload.VCTools --add Microsoft.VisualStudio.Workload.ManagedDesktopBuildTools`. It should be automatically included, don't you think so? – Basj Jun 30 '20 at 09:30
  • 1
    Actually, these are components that belong to `Microsoft.VisualStudio.Workload.VCTools`. You can choose whether or not to install them. However, it is not to specify that the workload will install all of them. And through the command line, they will not install them. So you need to specify them – Mr Qian Jun 30 '20 at 09:34
  • 1
    This might be the source of my problem indeed @PerryQianMSFT. I thought that doing `--add Microsoft.VisualStudio.Workload.VCTools` from command-line would automatically install ALL the packages listed in ["Components included by this workload"](https://learn.microsoft.com/en-us/visualstudio/install/workload-component-id-vs-build-tools?view=vs-2019#c-build-tools). Is it wrong? If so, the title "Components included by this workload" is misleading... Maybe could you add a note about this in your answer? It could save hours for future readers! – Basj Jun 30 '20 at 09:37
  • 1
    Sure. It is really a bit annoying and not convenient enough. The command line method itself is tedious. So if you still want this feature, I suggest you could suggest a feature request on our [User Voice Forum](https://developercommunity.visualstudio.com/spaces/8/index.html). – Mr Qian Jun 30 '20 at 09:37
  • Ok, sure, will do that. – Mr Qian Jun 30 '20 at 09:39
  • I just edited and added a Note at the end @PerryQianMSFT, is it ok? – Basj Jun 30 '20 at 10:01
  • Thank you for your help too @PerryQianMSFT. I re-edited and used ^ line breaks (it's the right way to do it in batch files) for readability, I hope it's ok for you. – Basj Jun 30 '20 at 10:15
  • Sorry, I didn't understand how I should download "vs_buildtool_xxx.exe" file in my script? – shs_sf Jan 04 '21 at 22:51