51

I've published an ASP.NET Core application as a self-contained application targeting Ubuntu. The publish seems to work fine. I've copied the files to a pretty vanilla Ubuntu machine. Now, how do I run my application?

My understanding is that because it is a self-contained .NET Core application I do not need to download and install .NET Core anything. My application should contain everything it needs.

All tutorials seem to say I should call dotnet run. However, the "dotnet" command line doesn't exist (is it supposed to be published into the self-contained folder??) So if I call it, I get "command not found". Of course I could download .NET Core, but doesn't that go against the whole self-contained concept?

Here is a sample of the files I'm copying over:

Enter image description here

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mark
  • 5,223
  • 11
  • 51
  • 81
  • I have no Idea as i'm not a dotnet dev, but I think you might need to install the dotnet cli on Ubuntu first. Is this something you need? https://github.com/dotnet/cli – Sven van de Scheur Oct 24 '16 at 19:33
  • I would agree with your assessment except it seems like one of the main points of publishing the application in a "self-contained" fashion is not to need to download anything else. The dotnet cli MIGHT be an exception to that but I don't think so. – Mark Oct 24 '16 at 19:38
  • I use a lot of "self contained" docker containers, without the docker service they don't run ;) – Sven van de Scheur Oct 24 '16 at 19:41

4 Answers4

49

Answer

Now, how do I run my application? My understanding is that because it is a self-contained .NET Core application I do not need to download and install .NET Core anything. My application should contain everything it needs.

You are correct. Run the executable.

When you create a self-contained app, the publish output "contains the complete set of files (both your app files and all .NET Core files) needed to launch your app." That includes the executable.

Example Self-Contained Deployment

Here is the output of dotnet publish -c release -r ubuntu.14.04-x64 for a simple self-contained application. Copy the publish directory to Ubuntu and run the executable.

C:\MyApp\bin\release\netcoreapp1.0\ubuntu.14.04-x64\publish\

...

libsos.so
libsosplugin.so
libuv.so
Microsoft.CodeAnalysis.CSharp.dll
Microsoft.CodeAnalysis.dll
Microsoft.CodeAnalysis.VisualBasic.dll
Microsoft.CSharp.dll
Microsoft.VisualBasic.dll
Microsoft.Win32.Primitives.dll
Microsoft.Win32.Registry.dll
mscorlib.dll
mscorlib.ni.dll
MyApp                        <------- On Ubuntu, run this executable
MyApp.deps.json                       and you will see Hello World!
MyApp.dll
MyApp.pdb
MyApp.runtimeconfig.json
sosdocsunix.txt
System.AppContext.dll
System.Buffers.dll
System.Collections.Concurrent.dll
System.Collections.dll

...

C:\MyApp\project.json

{
  "buildOptions": {
    "debugType": "portable",
    "emitEntryPoint": true
  },
  "dependencies": {},
  "frameworks": {
    "netcoreapp1.0": {
      "dependencies": {
        "Microsoft.NETCore.App": "1.0.1"
      }
    }
  },
  "runtimes": {
    "ubuntu.14.04-x64" : {},
    "win10-x64" : {}
  }
}

C:\MyApp\Program.cs

public class Program
{
    public static void Main(string[] args)
    {
        System.Console.WriteLine("Hello World!");
    }
}

See Also

This document differentiates between framework-dependent and self-contained deployments.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Shaun Luttin
  • 133,272
  • 81
  • 405
  • 467
  • 1
    I suppose up there the "MyApp" is actually "MyApp.exe" with file extension .exe? – swcraft Apr 19 '19 at 15:42
  • 2
    @JP_medevice The `*.exe` extension is a Windows convention that does not apply to Linux. See also https://askubuntu.com/questions/156392/what-is-the-equivalent-of-an-exe-file – Shaun Luttin Apr 19 '19 at 20:38
  • 3
    For ubuntu ignorants life myself: To run the executable you type .\ExecutableName on the terminal. @ShaunLuttin maybe you could include this in your answer. – Raikol Amaro Jan 02 '20 at 15:14
  • 1
    Running as described just results in a `command not found` error.... How do you actually run the self-contained publish output on linux? – Douglas Gaskell Feb 15 '21 at 03:55
  • 1
    Actually you use a forward slash to specify a relative path to an executable, not a backwards slash. So it's `./ExecutableName` on the terminal – Profilüfter Mar 18 '22 at 16:28
  • @DouglasGaskell The answer has it all except for the Linux command to turn the "MyApp" file into an executable file by executing (inside the published folder): `chmod a+x ./MyApp`. Once completed you should be able to run the application by typing `./MyApp` followed by the Enter/Return key. (`./` means "from current directory/folder") – timmi4sa Jun 20 '23 at 13:18
34

Follow the below steps to run your application:

  1. Publish your application as a self contained application:

     dotnet publish -c release -r ubuntu.16.04-x64 --self-contained
    
  2. Copy the publish folder to the Ubuntu machine

  3. Open the Ubuntu machine terminal (CLI) and go to the project directory

  4. Provide execute permissions:

    chmod +x ./appname
    
  5. Execute the application

     ./appname
    

Author: Harit Kumar

Original answer here: How to run a .NET Core console application on Linux

AsukaMinato
  • 1,017
  • 12
  • 21
John Deer
  • 2,033
  • 2
  • 13
  • 16
9

It's worth noting that with the .NET Standard 2+, there are two required steps:

  • Edit the .csproj file and add a line with a list of target runtimes:

<PropertyGroup>

<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>

<!-- Add this with the required runtimes -->
<RuntimeIdentifiers>win10-x64;osx.10.11-x64;ubuntu.16.10-x64</RuntimeIdentifiers>

</PropertyGroup>

  • Restore and build the app: dotnet restore && dotnet build -c release -r RUNTIME

Where RUNTIME is one of the runtimes listed in the .csproj file.

Importantly note that you cannot do this without the .csproj file edit and calling dotnet restore, or the runtime will not be downloaded from NuGet, and the -r ... flag will not work.

Community
  • 1
  • 1
Doug
  • 32,844
  • 38
  • 166
  • 222
6

You may want to check out dotnet-packaging as well. It includes a dotnet deb command line utility which allows you to create a .deb file (i.e. a Ubuntu installer) which you can use to install your app on Ubuntu. It should make deployment easier for you.

To get started, you'll first need to add this section to your .csproj file:

<ItemGroup>
  <PackageReference Include="Packaging.Targets" Version="0.1.45" />
  <DotNetCliToolReference Include="dotnet-deb" Version="0.1.45" />
<ItemGroup>

Then, run dotnet restore and dotnet deb -c Release -r ubuntu.18.04-x64 -f netcoreapp2.0. This will create a .deb file you can use to deploy your application to Ubuntu.

Frederik Carlier
  • 4,606
  • 1
  • 25
  • 36