LoginSignup
4
4

More than 3 years have passed since last update.

.Net Core 3.1のコンソールアプリを、単一実行可能ファイルかつRuntimeのインストールなしでUbuntu 18.04で実行する

Last updated at Posted at 2020-03-25

Publish Single Fileとself-containedを試してみたので、やったことの備忘です。

下記の記事を参考にしながら進めました。
.NET Core CLI を使用して .NET Core アプリを発行する
.NET Core 3.0のPublish Single File概要

前提

やりたいこと

  • .Net Core 3.1のコンソールアプリをUbuntu 18.04で実行したい
  • Ubuntu 18.04に.Net Core 3.1のRuntimeをインストールしたくない
  • 管理が面倒なので、単一実行可能ファイルにしたい

環境

開発環境
- Windows 10
- Visual Studio 2019

実行環境
- Ubuntu 18.04

実装方法

1. コンソールアプリを作る

Visual Studioのテンプレートからコンソールアプリを選び、ソリューションを作成します。
動くことが確認できればいいので、1秒ごとに「Hello World!」と出力するようにProgram.csを修正しました。

Program.cs
using System;
using System.Threading;

namespace SingleFileConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {
                Console.WriteLine("Hello World!");
                Thread.Sleep(1000);
            }
        }
    }
}

2. アプリケーションを発行する

  1. 開発者用コマンドプロンプトを起動する
    • ツール(T) > コマンドライン(L) > 開発者用コマンドプロンプト(C)
  2. dotnet publish -c Release -r linux-x64 /p:PublishSingleFile=trueを実行する
  3. 実行ファイルが発行されていることを確認する(画像参照) 発行先.PNG

3. アプリケーションを実行する

  1. Windows 10で開発したアプリケーションを、Ubuntu 18.04にコピー
  2. ターミナルを立ち上げて、アプリケーションを配置したディレクトリまで移動
  3. chmod +x SingleFileConsoleでアプリケーションを実行可能形式にする
  4. ./SingleFileConsoleでアプリケーションを実行する

うまくいっていれば、下記の画像のように動作するはずです。
動作チェック.gif

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4