Hello World .NET Core On Linux (2023)

Introduction

Hello World .NET Core On Linux (1)

In this article, we will explain how to install .NET Core on a Linux operating system (Ubuntu 20.04 LTS) and be able to run .NET Core applications, in addition to running them and also being able to run for example ASP.NET Core and that you can see from the client to test its operation.

Why run Microsoft applications on Linux? The answers can be many, we could say for going beyond the walls that existed for a long time and the “rivalry” that they supposedly made to see between these two OS environments, another for attracting all that Linux community so that they can work more comfortably, well supported C # applications or web pages with all the power of .NET Core, also another answer may be to reduce costs in a development company or for yourself as a user programmer, in short, there are many reasons why.NET Core is supported on Linux based operating systems.

.NET Core is compatible with Ubuntu.This article describes how to install .NET Core on Ubuntu. When a version of Ubuntu is no longer supported, .NET Core is no longer compatible with that version. However, there are slightly more custom instructions where you can install .NET Core on those versions.

Supported Distributions

In the following graph, according to the source (https://docs.microsoft.com/en-us/dotnet/core/install/linux-ubuntu) we have the comparative table of versions with support so far and some that do not have it.

Hello World .NET Core On Linux (2)

In the case of this article, we will use Ubuntu 20.04 LTS (https://ubuntu.com/#download) together with .NET Core 3.1 later we will go with .NET Core 5.0

We will need the following:

  • Linux installed in some installed and configured instance (https://www.process.st/checklist/ubuntu-server-setup-process/) I left it until step 14 this way you already have a well-configured installation.
  • Putty to access the Linux server via SSH.
  • Very eager to learn.

Table of Contents

  1. Installation of .NET Core 3.1 SDK and Runtime in a Linux environment.
  2. Creation and compilation of your first application in .NET Core.
  3. Create an application in ASP.NET Core.
  4. Test our ASP.NET Core application from a web browser.

Installation of .NET Core 3.1 SDK and Runtime in Linux environment

Once Ubuntu is installed, go to the link where it explains step by step for who is their first time, we have to access the operating system via SSH (Secure Shell) where I use Putty as a reference, but they can use the one that has the most affinity.

Now, we need to have interconnection between two computers or virtual machines, call a client and another server, in my case it is two computers on the same network to be able to have control over the Linux environment that is just where we want to run .NET Core, for this once the Putty is installed, we check whether both computers are well connected to the network with the Ping command in the Windows console.

Hello World .NET Core On Linux (3)

Now we run Putty and put the IP address of the remote computer where our Linux is installed.

Hello World .NET Core On Linux (4)

Next, we already establish a connection via SSH, if everything should appear, the window of the Linux Shell where it requests access, which are the previously configured credentials of the Ubuntu user (username/password)

(Video) Tutorial: Hello World with .Net Core and Visual Studio Code in Linux

Hello World .NET Core On Linux (5)

Hello World .NET Core On Linux (6)

Up to this point, we are already inside the Linux machine, now, since we are going to run installations, it is important to try to switch to the root user who has been the one with the most privileges at the UNIX level, therefore, we must execute the first commands to change from our normal user to root.

hughfernandez@neptuno:~$ su -

It will ask for the root user password and up to this point we can proceed to install .NET Core, but first, it is always recommended to run recent updates with the following commands.

sudo apt-get update # Gets the list of available updates.
sudo apt-get upgrade # Strictly update current packages
sudo apt-get dist-upgrade # Install (new) updates
sudo reboot # Restart th
e operating system

Since you rebooted the system before installing .NET, run the following commands to add the Microsoft package signing key to your list of trusted keys, and add the package repository.

wget https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb

sudo dpkg -i packages-microsoft-prod.deb

SDK Installation

To install the .NET Core SDK, run the following commands:

sudo apt-get update; \

sudo apt-get install -y apt-transport-https && \

sudo apt-get update && \

sudo apt-get install -y dotnet-sdk-3.1

Runtime installation

The following commands install ASP.NET Core Runtime, which is the most compatible runtime for .NET Core.In your terminal, run the following commands.

sudo apt-get update; \

sudo apt-get install -y apt-transport-https && \

(Video) Hello World! in.net core using Visual Studio Code (Ubuntu)

sudo apt-get update && \

sudo apt-get install -y aspnetcore-runtime-3.1

As an alternative to the ASP.NET Core Runtime, you can install the .NET Core Runtime that does not include ASP.NET Core support: replace aspnetcore-runtime-3.1 in the above command with dotnet-runtime-3.1.

sudo apt-get install -y dotnet-runtime-3.1

Creation and compilation of your first application in .NET Core

In this way, we can already have .NET Core 3.1 already installed both its SDK version and Runtime to run our applications. If you want to know what projects you can run that are included in this framework where you can get a complete list by executing the following command:

dotnet new --help

Hello World .NET Core On Linux (7)

We are going to start our "hello world" firstly by taking an order where we are going to have our projects in Linux, to advance we go to the Home folder as root user and we will see the folder of our Ubuntu user, in this case, my user is "hughfernandez”And there inside we are going to create the folder where we are going to execute our first application with the command.

mkdir holamundoConsola

We access it and see that we have nothing, but we are already on the folder.

Hello World .NET Core On Linux (8)

We execute the following command:

dotnet new console

Hello World .NET Core On Linux (9)

If we do an inspection of the generated listing the files of our console project with the command:

ls -1l

Hello World .NET Core On Linux (10)

If we already want to review a specific file such as "Program.cs" we have to execute the following command (it is important to write the file exactly as it appears otherwise the nano will open a new file)

(Video) C# Hello World using dotnet in Linux Mint

sudo nano Program.cs

Hello World .NET Core On Linux (11)

As you can see in the previous image, I was able to access the .cs file called “Program” and I was able to add “Hello world from Linux!”, To save the changes we press Control + X, it will ask to overwrite the changes and return to the command environment again in Linux.

Hello World .NET Core On Linux (12)

Up to this point, we can already compile our application and run it with the following commands

dotnet build

Hello World .NET Core On Linux (13)

dotnet run

Hello World .NET Core On Linux (14)

In this way we already created the console solution, we entered the file "Program.cs", we modified the line of code with our hello world and finally the compilation and execution of the program, but this time under a Linux environment.

Create an application in ASP.NET Core

For the ASP.NET core theme, we must create the folder as in the previous example, in my case I gave it the name of “demoAsp”, we enter it and execute the following command to create an ASP.NET Core project,

dotnet new razor

or

dotnet new mvc

Hello World .NET Core On Linux (15)

If they ask for the project name, the command "dotnet new" defaults to the name of the folder they created where they are executing the command

(Video) Quick Start! Set up .Net Core Dev Environment on Ubuntu

Hello World .NET Core On Linux (16)

To run our app in ASP.NET Core, you must first compile it with the command "dotnet build" so that it generates the folder "/bin/Debug/netcoreapp3.1", then execute the following command within the last folder mentioned above

dotnet demoAsp.dll

Hello World .NET Core On Linux (17)

To only do a direct test, if everything is just not located in the root folder of the project and normally run the "dotnet run".

Test our ASP.NET Core application from a web browser

Already in this way and our application in ASP.NET Core is running on the Kestrel server of .NET Core, but we do not yet have an exit to be able to access it from a browser, this because it runs within localhost and Linux assumes that it can only be accessed, therefore, the following command must be executed from Linux itself,

dotnet run --urls "http://0.0.0.0:5000"

Hello World .NET Core On Linux (18)

In this case, the “URLs” argument specifies Kestrel on which IP addresses and ports it should listen to. By default, Kestrel will only listen to the localhost address so we must explicitly specify that we want it to listen to any IP address to perform this test.

Now to test that we can access from any client we go to the Windows computer and open any web browser and enter the IP address of the Linux machine and port 5000 "http: // the-ip-of-your-server: 5000"To verify that the web application is working,

Hello World .NET Core On Linux (19)

If you need to be able to access the website remotely (externally) from other computers automatically without executing the URLs command, you can program it directly in the project, you must link it differently.

To do this, you must access the files of the newly created project and open "Program.cs". Here you add a call to the "UseUrls" method as follows.

  1. publicstaticIWebHostBuilderCreateWebHostBuilder(string[]args)=>
  2. WebHost.CreateDefaultBuilder(args)
  3. .UseUrls("http://0.0.0.0:5000")
  4. .UseStartup<Startup>();

This binds the ASP.NET Core website to all the server's IP addresses, rather than just the localhost, which is what it does by default.

(Video) Learn C# with CSharpFritz - Hosting ASP.NET Core MVC with Linux

Conclusion

Up to this point and if you were able to successfully follow all the steps in this article, you are already able to do the following.

  • Configure the install Linux Ubuntu.
  • Know how to remotely access a Linux machine using the SSH protocol.
  • Know how to install both SDK and .NET Core Runtime in the version you want to be available.
  • Know how to move within the Linux directories.
  • Start generating projects in .NET Core in a Linux environment.
  • Compile and be able to run .NET Core applications on Linux.
  • Run a page in ASP.NET Core from any web browser.

Enjoy!...

FAQs

Can you use .NET Core on Linux? ›

Supported Platform: Windows® (Authoring), Linux® (Execution), and macOS (Execution). This example shows how to create a . NET assembly using the Library Compiler and integrate it into a . NET Core application that can run on Linux or macOS.

How to install .NET Core in Linux? ›

To install the . NET Core 3.1 SDK, run sudo apt install dotnet-sdk-3.1 . Enter y and press Enter. The package manager downloads and installs both .
...
Identify the correct package name
  1. The product is either dotnet or aspnetcore. ...
  2. The type is either sdk or runtime.
  3. At the time of this writing, the supported .
Jan 25, 2022

How do I host a dotnet core in Linux? ›

Prerequisites
  1. Visit the Download . NET Core page.
  2. Select the latest non-preview . NET Core version.
  3. Download the latest non-preview runtime in the table under Run apps - Runtime.
  4. Select the Linux Package manager instructions link and follow the CentOS instructions.
Jan 9, 2023

Is .NET Core obsolete? ›

The long-term-support (LTS) version 3.1 of Microsoft . NET Core Framework is slated to go out of support on December 13th, 2022. Microsoft recommends upgrading . NET Core 3.1 applications to .

Can C# run on Linux? ›

To compile, Decompile and Run C# code in Linux, follow the below-mentioned steps: Firstly, we need to install mono-complete, to run software for Mono or Microsoft. NET. Step 1: To Install mono-complete, open up your Linux terminal and type the following command, and hit enter.

How do I start .NET Core from command line? ›

. NET Core Command-Line Interface
  1. Command Structure. The following is a command structure. ...
  2. Create a New Project. To create a new . ...
  3. Add Package Reference. We often need to add NuGet package reference for different purposes. ...
  4. Restore Packages. ...
  5. Build Project. ...
  6. Run project. ...
  7. Getting Help.

How to host a .NET Core in Ubuntu? ›

Install an ASP.NET Core Web API on Linux (Ubuntu 18.04) and host with Nginx and SSL
  1. Install .net core 3.1.
  2. Install Nginx.
  3. Build and copy your application.
  4. Configure your Nginx server block.
  5. Create your Asp.Net service.
  6. Secure your hosted application with SSL.

How to run C# code in Linux terminal? ›

Run C# on Linux
  1. Open Terminal ( ctrl+alt+T ).
  2. Type the command sudo apt install mono-complete to install mono-complete.
  3. Open a text editor (we are going to use Gedit) and save the following program with a . ...
  4. Now, you can compile the program using mcs filename.

How install .NET Core 2.1 on Linux? ›

Installing ASP.NET Core 2.1 on Ubuntu 18.4 Linux
  1. Requirements.
  2. Register Microsoft key and feed.
  3. Install the .NET Core 2.1 Runtime.
  4. Install the .NET SDK.
  5. Create a new ASP.NET Core web application.
  6. Publish and copy over the app.
  7. Install MySQL.
  8. Install Apache as reverse proxy.
Jul 17, 2018

How do I know if .NET Core is installed Linux? ›

Open the command prompt. (2) Run the below command If you are on Linux system. type dotnet --version - Doesn't work on windows 10.
...
NET Core is installed on Windows is:
  1. Press Windows + R.
  2. Type cmd.
  3. On the command prompt, type dotnet --version.

What is replacing .NET Core? ›

NET 6 is an open-source and cross-platform framework, meaning that it automatically replaces . NET Core and . NET Framework with a single, more powerful, unified platform.

What is .NET Core called now? ›

In 2014, Microsoft introduced .NET Core as a cross-platform, open-source successor to .NET Framework. This new implementation of .NET kept the name .NET Core through version 3.1. The next version after .NET Core 3.1 was named .NET 5.

Is .NET Core faster than node? ›

NET Core has an easier time working with CPU-intensive tasks and rendering static pages since the in-built IIS server kernel caching makes this process very straightforward. Therefore, . NET core vs node.

Is C# becoming obsolete? ›

C# is a programming language that is worth learning.

You can use it as a Full Stack language and programming in C# will be the best decision. In 2022, It will be a widely used language and is widely recognized among other programming languages such as Java and Python.

What is the alternative to C# on Linux? ›

Other interesting Linux alternatives to C# are Java, C++, Rust and C (programming language).

What OS can C# run on? ›

While originally built to run on Windows, C# was quickly ported to Linux and macOS by the Mono project. Today, C# is open source and runs on the cross-platform .

What is .NET core CLI? ›

The .NET command-line interface (CLI) is a cross-platform toolchain for developing, building, running, and publishing .NET applications. The .NET CLI is included with the .NET SDK. For more information about how to install the .NET SDK, see Install .NET Core.

How do you get to the start of a command line in Linux? ›

You can move the cursor to the beginning of the line with CTRL+A. Similarly, use CTRL+E to move the cursor to the end of the line. Alt+F moves one word forward, and Alt+B moves one word back.

How do I enable dotnet in cmd? ›

Steps
  1. Open a command prompt with administrator user rights (Run as Administrator).
  2. To install .NET Framework 3.5 from installation media located on the D: drive, use the following command: Windows Command Prompt Copy. DISM /Online /Enable-Feature /FeatureName:NetFx3 /All /LimitAccess /Source:d:\sources\sxs.
Dec 15, 2021

How does .NET Core work on Linux? ›

The . NET Core runtime allows you to run applications on Linux that were made with . NET Core but didn't include the runtime. With the SDK you can run but also develop and build .

How do I run a .NET Core console app on Ubuntu? ›

How To Run . Net Core Console Application With Ubuntu
  1. Need to create . net core console application POC. ...
  2. Create a docker image of dotnet core application. ...
  3. Once the docker is installed successfully. ...
  4. Run the same image on Ubuntu Machine. ...
  5. Step 5 - Export Image so we can import in Ubuntu machine.
Aug 30, 2022

How do I host a .NET Core website? ›

In general, to deploy an ASP.NET Core app to a hosting environment:
  1. Deploy the published app to a folder on the hosting server.
  2. Set up a process manager that starts the app when requests arrive and restarts the app after it crashes or the server reboots.
Nov 16, 2022

How to run C# code in Visual Studio code Linux? ›

How to Run C# in VSCode
  1. Install . NET 5.0. ...
  2. Create a new C# project in VSCode. Next, create a new project and open it in VSCode: ...
  3. Run Your C# Code in VSCode. To execute your code, simply run: ...
  4. Debug Your C# Code in VSCode. First, be sure you installed the official C# extension mentioned above. ...
  5. Compile Your Code.
Sep 29, 2021

How to run C# code in Ubuntu? ›

C# Code Compilation

Then, open the Terminal and hit the following commands to compile the code. The aforesaid command will generate an executable file like windows. Now hit the ./test.exe or mono test.exe command to run the C# binary; Here, the screenshot summarized everything we have done so far.

Can C# run on command line? ›

To run, first, open a command prompt, click "Start", then type cmd.exe . You may then have to cd into the directory that holds your source files. Run the C# compiler like this: c:\windows\Microsoft.NET\Framework\v3.

How to install .NET Core 3.1 in Ubuntu? ›

While executing the command sudo apt-get install dotnet-sdk-3.1 , you may get dependency errors related to libicu not found in Ubuntu 20.04 LTS. If this is the case, then execute the following commands in terminal [courtesy: Phillip Haydon]. Now try the sudo apt-get install dotnet-sdk-3.1 , it should work.

How to install .NET Core 6 on Ubuntu? ›

You can do that via the following commands:
  1. sudo apt update && sudo apt -y install dotnet6. ...
  2. $ docker run --rm -it ubuntu:jammy root@4c9d58f507a3:/# apt update && apt install -y dotnet6 root@444999ffd672:/# dotnet --version 6.0.108.
Aug 10, 2022

How to install dotnet SDK in Linux? ›

Contents
  1. Install Microsoft .NET Core SDK on Linux. 1.1. Install .NET SDK using Package Manager. 1.1.1. Install .NET SDK in Alpine Linux. 1.1.2. Install .NET SDK in Debian. 1.1.3. Install .NET SDK in Fedora. 1.1.4. ...
  2. Verify .NET Installation.
  3. Create your First App using Dotnet.
  4. Get Microsoft Visual Studio Code Editor.
  5. Telemetry.
Dec 22, 2022

How do I check cores in Linux? ›

You can use one of the following command to find the number of physical CPU cores including all cores on Linux:
  1. lscpu command.
  2. cat /proc/cpuinfo.
  3. top or htop command.
  4. nproc command.
  5. dmidecode -t processor command.
  6. getconf _NPROCESSORS_ONLN command.
Sep 21, 2022

What is the command to check dotnet version in Linux? ›

You can see both the SDK versions and runtime versions with the command dotnet --info .

How to install dotnet from command line? ›

Examples
  1. Install the latest long-term supported (LTS) version to the default location: Windows: PowerShell Copy. ...
  2. Install the latest preview version of the 6.0.1xx SDK to the specified location: Windows: PowerShell Copy. ...
  3. Install the 6.0.0 version of the shared runtime: Windows: PowerShell Copy.
Jan 7, 2023

How do I start a dotnet core application? ›

  1. Start Visual Studio and select Create a new project.
  2. In the Create a new project dialog, select ASP.NET Core Web Application > Next.
  3. In the Configure your new project dialog, enter MvcMovie for Project name. ...
  4. Select Create.
  5. In the Create a new ASP.NET Core web application dialog, select:
Feb 13, 2023

How do I run .NET 5 app on Linux? ›

Create . NET 5.0 App Using Visual Studio
  1. In Visual Studio create a . NET 5.0 C# project named MyDotNet5App . ...
  2. In the Solution Explorer in Visual Studio, right-click the project name and select Add > Project Reference. ...
  3. Open the C# source file Program. ...
  4. Build and run the application.

Which command is used to run .NET Core applications? ›

The dotnet run command provides a convenient option to run your application from the source code with one command. It's useful for fast iterative development from the command line. The command depends on the dotnet build command to build the code.

How do I run a .NET Core console application from command prompt? ›

In . NET Core, it runs from the dll, so you have to just run the application by running the command prompt and using the command - dotnet run. Open your command prompt and go to that folder where your application persists. This resulted in printing "Hello World!" as it is written in our console application.

What is the difference between asp net core and .NET core? ›

NET Core is a runtime. It can execute applications that are built for it. ASP.NET Core is a collection of libraries that form a Framework for building web applications. ASP.NET Core libraries can be used on both .

How to run asp net core locally? ›

FROM VISUAL STUDIO: You can simply run the ASP.NET Core Web API from Visual Studio in Development Mode by Pressing F5. FROM COMMAND PROMPT: Run the API Project from the command line by opening a command prompt window in the root project folder of the project and use the dotnet run command.

What is the net command in Linux? ›

The NET TIME command allows you to view the time on a remote server or synchronise the time on the local server with the time on the remote server. Without any options, the NET TIME command displays the time on the remote server. Displays the time on the remote server in a format ready for /bin/date.

What is .NET Core CLI? ›

The .NET command-line interface (CLI) is a cross-platform toolchain for developing, building, running, and publishing .NET applications. The .NET CLI is included with the .NET SDK. For more information about how to install the .NET SDK, see Install .NET Core.

How do I console in .NET Core? ›

. NET Core Console App Set Up
  1. Create a console application. In Visual Studio, you can do this with File | New, or you can use the dotnet CLI command “dotnet new console”.
  2. Add some NuGet package references. ...
  3. Update our Program class. ...
  4. Put our code into the ConsoleService class.
Jun 8, 2021

Videos

1. #150: Getting started with .NET Core on Linux
(Asp.Net Monsters)
2. ASP.NET Core - Hello World
(Industrial IT and Automation)
3. Getting Started with .NET Core on Linux
(DevConf)
4. How to run .NET core executable app in Linux (Ubuntu) | Live Demo
(Shobhit Walia)
5. Get started with VS Code using C# and .NET Core on Ubuntu
(dotNet Việt Nam Official)
6. Creating background .NET Core services on Linux
(dotnet)
Top Articles
Latest Posts
Article information

Author: Margart Wisoky

Last Updated: 12/08/2022

Views: 5421

Rating: 4.8 / 5 (58 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Margart Wisoky

Birthday: 1993-05-13

Address: 2113 Abernathy Knoll, New Tamerafurt, CT 66893-2169

Phone: +25815234346805

Job: Central Developer

Hobby: Machining, Pottery, Rafting, Cosplaying, Jogging, Taekwondo, Scouting

Introduction: My name is Margart Wisoky, I am a gorgeous, shiny, successful, beautiful, adventurous, excited, pleasant person who loves writing and wants to share my knowledge and understanding with you.