30

I'm developing a .NET Core app on a Windows 10 machine (with Visual Studio 2015 update 3 + Microsoft .NET Core 1.0.1 VS 2015 Tooling Preview 2) which should published on an Ubuntu 16 machine. To do that, I have to move my source code to the end machine and compile it there, to get it to run. e.g. I'm not able to compile the code on windows and run it on linux. Question: Is there any way to compile the code on win machine and run it on linux?

Luke Girvin
  • 12,672
  • 8
  • 57
  • 79
amiry jd
  • 18,602
  • 10
  • 58
  • 127

4 Answers4

47

Using dotnet build command, you may specify --runtime flag

-r|--runtime < RUNTIME_IDENTIFIER >

Target runtime to build for. For a list of Runtime Identifiers (RIDs) you can use, see the RID catalog.

RIDs that represent concrete operating systems usually follow this pattern [os].[version]-[arch]

Fo example, to build a project and its dependencies for Ubuntu 16.04 runtime use:

dotnet build --runtime ubuntu.16.04-x64
Community
  • 1
  • 1
Set
  • 40,147
  • 18
  • 114
  • 133
  • 1
    Thanks. But i'm getting this error: `Can not find runtime target for framework '.NETCoreApp,Version=v1.0' compatible with one of the target runtimes: 'win10-x64, win81-x64, win8-x64, win7-x64'.` – amiry jd Jan 08 '17 at 15:26
  • 1
    @Javad_Amiry you need to specify that you project support those runtimes. See http://stackoverflow.com/a/37590605/2833802 and http://stackoverflow.com/a/40194003/2833802 SO answers – Set Jan 08 '17 at 16:16
  • 1
    Just to add a more recent update to this answer. You would need to set the RuntimeIdentifier in the csproj file with what ever runtimes you would target, i.e: win10-x64;ubuntu.16.04-x64 – Jean Roux May 30 '18 at 06:56
  • 1
    When should this runtime flag be used? Is it a general flag which applies to all use cases just to optimize the generated binaries for specific platform? Specifically, is it only used to build self-contained binaries or it can also be used to build app which runs on dotnet core SDK/Runtime? – Kok How Teh Jun 02 '19 at 11:58
8
dotnet publish **path to your solution** --configuration Release --framework netcoreapp3.0 --output .**output path** --self-contained false --runtime linux-x64 --verbosity quiet
Dharman
  • 21,838
  • 18
  • 57
  • 107
Wasyster
  • 1,655
  • 2
  • 19
  • 38
4

For anyone who's now seeing this not working anymore, it seems as of the update on the 10th of November 2020 you have to specify the project file now as it doesn't like using a specified runtime on a solution (.sln) anymore.

An issue about this was raised here (https://github.com/dotnet/sdk/issues/14281) but obviously that's not going to get resolved immediately.

So previously where this would work:

dotnet build --runtime ubuntu.xx.xx-x64

It wants something like this now:

dotnet build ProjectName.csproj --runtime ubuntu.xx.xx-x64
Rogod
  • 119
  • 9
0
dotnet build ProjectFile.csproj --runtime linux-x64
Alex from Jitbit
  • 39,345
  • 13
  • 111
  • 109