Hello World .NET Core On Linux

 Introduction

 
Hello World .NET Core On Linux
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
 
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
 
Now we run Putty and put the IP address of the remote computer where our Linux is installed.
 
Hello World .NET Core On Linux
 
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)
 
Hello World .NET Core On Linux
 
Hello World .NET Core On Linux
 
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 && \
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
 
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
 
We execute the following command:
 
dotnet new console
 
Hello World .NET Core On Linux
 
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
 
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)
 
sudo nano Program.cs
 
Hello World .NET Core On Linux
 
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
 
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
 
dotnet run
 
Hello World .NET Core On Linux
 
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
 
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
 
Hello World .NET Core On Linux
 
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
 
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
 
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
 
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. public static IWebHostBuilder CreateWebHostBuilder(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.
 

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!...


Similar Articles