Develop .NET on Linux

(An English version of this articleisalsoavailable.)

I have multiple machines, one of which has Linux. Since it is a desktop machine, I often use it for development, but I would like to introduce the environment.

What can i do?

Since .NET Core is compatible with multiple platforms, you can do almost the same development as Windows.

What you can do

The following are the things I have confirmed that it is possible.

Impossible (or difficult)

How to create an environment

.NET Core .NET Core can be used with most major distributions, and various packages are also available, but I would like to recommend Shell Script. It is an installation using / dotnet / core / tools / dotnet-install-script).

Installing via a shell script makes it easy to install multiple versions of the .NET Core runtime and libraries.

For example, if you want to install the LTS version, it looks like this:

./dotnet-install.sh --channel LTS

You may need additional libraries that need to be installed. The one that generally seems to be less commonly included is libicu.

The installation location does not have to be a user-writable location. Additional templates, tools, etc. are under the control of each user.

Development environment

Visual Studio Code If you want to prepare a development environment for free, the following combinations are recommended.

With the above, you can create an environment where you can start development.

JetBrains Rider If you're willing to spend money on your development environment, it's an option to consider JetBrains Rider. JetBrains is also the developer of ReSharper, a refactoring plugin for Visual Studio, which provides advanced coding assistance. Therefore, it can be developed comfortably.

Rider has Rider alone, Rider and ReSharper bundled version, and All Products Pack including all IDE products. For details, please refer to the Purchase Page. (I personally subscribe to the All Products Pack because I may use it in other languages as well.) Note that most JetBrains IDEs, not just Rider, can be used in other environments such as Windows. Since it can be used, it is also useful in that the same IDE can be used on multiple OSs.

Also, NuGet package management can be done graphically, so it is recommended for those who have many opportunities to do C #.

Other

By using OmniSharp (also used in the above extension), you can use Language Server Protocol as you like. It is also possible to develop with an editor. This method depends on the editor, so I will omit it here.

I will try it immediately

Project creation

Once you have the environment in place, you can start development.

Use the dotnet command to create a project.

dotnet new console -o myapp

If you run it without any arguments, you will see the templates that you can use:

dotnet new
Usage: new [options]

Options:
  -h, --help          Displays help for this command.
  -l, --list          Lists templates containing the specified name. If no name is specified, lists all templates.
  -n, --name          The name for the output being created. If no name is specified, the name of the current directory is used.
  -o, --output        Location to place the generated output.
  -i, --install       Installs a source or a template pack.
  -u, --uninstall     Uninstalls a source or a template pack.
  --nuget-source      Specifies a NuGet source to use during install.
  --type              Filters templates based on available types. Predefined values are "project", "item" or "other".
  --dry-run           Displays a summary of what would happen if the given command line were run if it would result in a template creation.
  --force             Forces content to be generated even if it would change existing files.
  -lang, --language   Filters templates based on language and specifies the language of the template to create.
  --update-check      Check the currently installed template packs for updates.
  --update-apply      Check the currently installed template packs for update, and install the updates.


Templates                                         Short Name               Language          Tags                                 
----------------------------------------------------------------------------------------------------------------------------------
Console Application                               console                  [C#], F#, VB      Common/Console                       
Class library                                     classlib                 [C#], F#, VB      Common/Library                       
WPF Application                                   wpf                      [C#]              Common/WPF                           
WPF Class library                                 wpflib                   [C#]              Common/WPF                           
WPF Custom Control Library                        wpfcustomcontrollib      [C#]              Common/WPF                           
WPF User Control Library                          wpfusercontrollib        [C#]              Common/WPF                           
Windows Forms (WinForms) Application              winforms                 [C#]              Common/WinForms                      
Windows Forms (WinForms) Class library            winformslib              [C#]              Common/WinForms                      
Worker Service                                    worker                   [C#]              Common/Worker/Web                    
Unit Test Project                                 mstest                   [C#], F#, VB      Test/MSTest                          
NUnit 3 Test Project                              nunit                    [C#], F#, VB      Test/NUnit                           
NUnit 3 Test Item                                 nunit-test               [C#], F#, VB      Test/NUnit                           
xUnit Test Project                                xunit                    [C#], F#, VB      Test/xUnit                           
Razor Component                                   razorcomponent           [C#]              Web/ASP.NET                          
Razor Page                                        page                     [C#]              Web/ASP.NET                          
MVC ViewImports                                   viewimports              [C#]              Web/ASP.NET                          
MVC ViewStart                                     viewstart                [C#]              Web/ASP.NET                          
Blazor Server App                                 blazorserver             [C#]              Web/Blazor                           
Blazor WebAssembly App                            blazorwasm               [C#]              Web/Blazor/WebAssembly               
ASP.NET Core Empty                                web                      [C#], F#          Web/Empty                            
ASP.NET Core Web App (Model-View-Controller)      mvc                      [C#], F#          Web/MVC                              
ASP.NET Core Web App                              webapp                   [C#]              Web/MVC/Razor Pages                  
ASP.NET Core with Angular                         angular                  [C#]              Web/MVC/SPA                          
ASP.NET Core with React.js                        react                    [C#]              Web/MVC/SPA                          
ASP.NET Core with React.js and Redux              reactredux               [C#]              Web/MVC/SPA                          
Razor Class Library                               razorclasslib            [C#]              Web/Razor/Library/Razor Class Library
ASP.NET Core Web API                              webapi                   [C#], F#          Web/WebAPI                           
ASP.NET Core gRPC Service                         grpc                     [C#]              Web/gRPC                             
dotnet gitignore file                             gitignore                                  Config                               
global.json file                                  globaljson                                 Config                               
NuGet Config                                      nugetconfig                                Config                               
Dotnet local tool manifest file                   tool-manifest                              Config                               
Web Config                                        webconfig                                  Config                               
Solution File                                     sln                                        Solution                             
Protocol Buffer File                              proto                                      Web/gRPC      

You can get the missing template online and install it.

dotnet install hoge

This will generate the boilerplate code for your console application in the myapp directory, so you can open an editor and start developing. (Visual Studio Code requires a net environment because the necessary components of the C # plug-in and download are done at the first launch, and it may take a little next time.)

If you are using Rider, you can create a project from a dedicated dialog.

riderproject.png

Some templates are not supported, so in that case you will have to create a project from the command line.

.NET Core tools

By using the dotnet command, you can create and operate projects, and in the form of .NET Core Tools You can also install various tools.

For example, PowerShell can also be installed as part of .NET, in which case:

dotnet tool install -g powershell

This also applies if you want to install other ML.NET tools.

dotnet tool install -g mlnet

debug

It is also possible to debug with Visual Studio Code, and you can do it like a normal Visual Studio. vscode.png

However, it may take some getting used to because the settings need to be rewritten such as launch.json </ code>. An example is shown below.

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": ".NET Core Launch (console)",
            "type": "coreclr",
            "request": "launch",
            "WARNING01": "*********************************************************************************",
            "WARNING02": "The C# extension was unable to automatically decode projects in the current",
            "WARNING03": "workspace to create a runnable launch.json file. A template launch.json file has",
            "WARNING04": "been created as a placeholder.",
            "WARNING05": "",
            "WARNING06": "If OmniSharp is currently unable to load your project, you can attempt to resolve",
            "WARNING07": "this by restoring any missing project dependencies (example: run 'dotnet restore')",
            "WARNING08": "and by fixing any reported errors from building the projects in your workspace.",
            "WARNING09": "If this allows OmniSharp to now load your project then --",
            "WARNING10": "  * Delete this file",
            "WARNING11": "  * Open the Visual Studio Code command palette (View->Command Palette)",
            "WARNING12": "  * run the command: '.NET: Generate Assets for Build and Debug'.",
            "WARNING13": "",
            "WARNING14": "If your project requires a more complex launch configuration, you may wish to delete",
            "WARNING15": "this configuration and pick a different template using the 'Add Configuration...'",
            "WARNING16": "button at the bottom of this file.",
            "WARNING17": "*********************************************************************************",
            "preLaunchTask": "build",
            "program": "${workspaceFolder}/bin/Debug/netcoreapp3.1/myapp.dll",
            "args": [],
            "cwd": "${workspaceFolder}",
            "console": "internalConsole",
            "stopAtEntry": false
        },
        {
            "name": ".NET Core Attach",
            "type": "coreclr",
            "request": "attach",
            "processId": "${command:pickProcess}"
        }
    ]
}

The configuration file itself has a function to be automatically generated, but you need to set the platform name ( netcoreapp3.1 </ code>) and application name ( myapp.dll </ code>) by yourself.

When using Rider, it is quite easy because there are many parts that are automatically set. rider.png

Finally

Before the appearance of .NET Core, there were Mono etc., but .NET still had a strong character as a Windows environment, but after the appearance of .NET Core, development that is comparable to Linux environment including around tools is possible. It has become like.

There may be situations where it is convenient to be able to develop on Linux, especially when the deployment destination is Linux. In such a case, I think it is a big advantage to be able to create a development environment on Linux.

Recommended Posts