Home Linux How To Install Microsoft .NET Core SDK On Linux
Install Microsoft .NET Core SDK On Linux

How To Install Microsoft .NET Core SDK On Linux

By sk
820 Views

The .NET Coreis a free, cross platform and open source framework developed by Microsoft to build desktop applications, mobile apps, web apps, IoT apps and gaming apps etc. If you're dotnet developer coming from Windows platform, .NET core helps you to setup your development environment easily on any Linux and Unix-like operating systems. This step by step guide explains how to install Microsoft .NET Core SDK on Linux and how to write your first app using .Net.

Install Microsoft .NET Core SDK On Linux

The .NET core supports GNU/Linux, Mac OS and Windows. .Net core can be installed on popular GNU/Linux operating systems including Debian, Fedora, CentOS, Oracle Linux, RHEL, SUSE/openSUSE, and Ubuntu. As of writing this guide, the latest .NET core version was 2.2.

On Debian 10:

Register the Microsoft key and add .NET repository:

$ wget -qO- https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.asc.gpg
$ sudo mv microsoft.asc.gpg /etc/apt/trusted.gpg.d/
$ wget -q https://packages.microsoft.com/config/debian/10/prod.list
$ sudo mv prod.list /etc/apt/sources.list.d/microsoft-prod.list
$ sudo chown root:root /etc/apt/trusted.gpg.d/microsoft.asc.gpg
$ sudo chown root:root /etc/apt/sources.list.d/microsoft-prod.list

Then install .NET core using commands:

$ sudo apt-get update
$ sudo apt-get install apt-transport-https
$ sudo apt-get update
$ sudo apt-get install dotnet-sdk-3.1

Install ASP.NET Core Runtime:

$ sudo apt-get install aspnetcore-runtime-3.1

Install .NET Core Runtime:

$ sudo apt-get install dotnet-runtime-3.1

On Debian 9:

Register Microsoft key and add .NET repository:

$ wget -qO- https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.asc.gpg
$ sudo mv microsoft.asc.gpg /etc/apt/trusted.gpg.d/
$ wget -q https://packages.microsoft.com/config/debian/9/prod.list
$ sudo mv prod.list /etc/apt/sources.list.d/microsoft-prod.list
$ sudo chown root:root /etc/apt/trusted.gpg.d/microsoft.asc.gpg
$ sudo chown root:root /etc/apt/sources.list.d/microsoft-prod.list

Install .NET SDK using commands:

$ sudo apt-get update
$ sudo apt-get install dotnet-sdk-3.1

Install ASP.NET Core RUntime:

$ sudo apt-get install aspnetcore-runtime-3.1

Install .NET Core Runtime:

$ sudo apt-get install dotnet-runtime-3.1

On Fedora 30:

Add Microsoft key and repository:

$ sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc
$ sudo wget -q -O /etc/yum.repos.d/microsoft-prod.repo https://packages.microsoft.com/config/fedora/30/prod.repo

Install .NET Core SDK:

$ sudo dnf install dotnet-sdk-3.1

Install ASP.NET Core Runtime:

$ sudo dnf install aspnetcore-runtime-3.1

Install .NET Core Runtime:

$ sudo dnf install dotnet-runtime-3.1

On Fedora 29:

Add Microsoft key and enable .NET repository:

$ sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc
$ sudo wget -q -O /etc/yum.repos.d/microsoft-prod.repo https://packages.microsoft.com/config/fedora/29/prod.repo

Now, Install .NET SDK:

$ sudo dnf update
$ sudo dnf install dotnet-sdk-2.2

Install ASP.NET Core Runtime:

$ sudo dnf install aspnetcore-runtime-3.1

Install .NET Core Runtime:

$ sudo dnf install dotnet-runtime-3.1

On CentOS 7:

Add Microsoft key and enable .NET core repository:

$ sudo rpm -Uvh https://packages.microsoft.com/config/centos/7/packages-microsoft-prod.rpm

Update the repositories and install .NET SDK:

$ sudo yum update
$ sudo yum install dotnet-sdk-3.1

Install ASP.NET Core Runtime:

$ sudo yum install aspnetcore-runtime-3.1

Install .NET Core Runtime:

$ sudo yum install dotnet-runtime-3.1

On openSUSE 15:

Add key, enable repository and install necessary dependencies using the following commands:

$ sudo zypper install libicu
$ sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc
$ wget -q https://packages.microsoft.com/config/opensuse/15/prod.repo
$ sudo mv prod.repo /etc/zypp/repos.d/microsoft-prod.repo
$ sudo chown root:root /etc/zypp/repos.d/microsoft-prod.repo

Update the repositories and Install .NET SDK using commands:

$ sudo zypper update
$ sudo zypper install dotnet-sdk-3.1

Install ASP.NET Core Runtime:

$ sudo zypper install aspnetcore-runtime-3.1

Install .NET Core Runtime:

$ sudo zypper install dotnet-runtime-3.1

On Ubuntu 19.04:

Register Microsoft key and add .NET core repository:

$ wget -q https://packages.microsoft.com/config/ubuntu/19.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
$ sudo dpkg -i packages-microsoft-prod.deb

Install .NET Core SDK:

$ sudo apt-get update
$ sudo apt-get install apt-transport-https
$ sudo apt-get update
$ sudo apt-get install dotnet-sdk-3.1

Install ASP.NET Core Runtime:

$ sudo apt-get install aspnetcore-runtime-3.1

Install .NET Core Runtime:

$ sudo apt-get install dotnet-runtime-3.1

On Ubuntu 18.04 LTS:

Register the Microsoft key and add .NET core repository using commands:

$ wget -q https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
$ sudo dpkg -i packages-microsoft-prod.deb

Enable 'Universe' repository using:

$ sudo add-apt-repository universe

Then, install .NET Core SDK using command:

$ sudo apt-get install apt-transport-https
$ sudo apt-get update
$ sudo apt-get install dotnet-sdk-3.1

Install ASP.NET Core Runtime:

$ sudo apt-get install aspnetcore-runtime-3.1

Install .NET Core Runtime:

$ sudo apt-get install dotnet-runtime-3.1

On Ubuntu 16.04 LTS:

Register Microsoft key and .NET repository using commands:

$ wget -q https://packages.microsoft.com/config/ubuntu/16.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
$ sudo dpkg -i packages-microsoft-prod.deb

And then, Install .NET core SDK:

$ sudo apt-get install apt-transport-https
$ sudo apt-get update
$ sudo apt-get install dotnet-sdk-3.1

Install ASP.NET Core Runtime:

$ sudo apt-get install aspnetcore-runtime-3.1

Install .NET Core Runtime:

$ sudo apt-get install dotnet-runtime-3.1

For other distributions, refer the official installation section.

Create Your First App

We have successfully installed .Net Core SDK in our Linux box. It is time to create our first app using dotnet.

For the purpose of this guide, I am going to create a new app called "ostechnixApp". To do so, simply run the following command:

$ dotnet new console -o ostechnixApp

Sample output:

Welcome to .NET Core!
---------------------
Learn more about .NET Core: https://aka.ms/dotnet-docs
Use 'dotnet --help' to see available commands or visit: https://aka.ms/dotnet-cli-docs

Telemetry
---------
The .NET Core tools collect usage data in order to help us improve your experience. The data is anonymous and doesn't include command-line arguments. The data is collected by Microsoft and shared with the community. You can opt-out of telemetry by setting the DOTNET_CLI_TELEMETRY_OPTOUT environment variable to '1' or 'true' using your favorite shell.

Read more about .NET Core CLI Tools telemetry: https://aka.ms/dotnet-cli-telemetry

ASP.NET Core
------------
Successfully installed the ASP.NET Core HTTPS Development Certificate.
To trust the certificate run 'dotnet dev-certs https --trust' (Windows and macOS only). For establishing trust on other platforms refer to the platform specific documentation.
For more information on configuring HTTPS see https://go.microsoft.com/fwlink/?linkid=848054.
Getting ready...
The template "Console Application" was created successfully.

Processing post-creation actions...
Running 'dotnet restore' on ostechnixApp/ostechnixApp.csproj...
Restoring packages for /home/sk/ostechnixApp/ostechnixApp.csproj...
Generating MSBuild file /home/sk/ostechnixApp/obj/ostechnixApp.csproj.nuget.g.props.
Generating MSBuild file /home/sk/ostechnixApp/obj/ostechnixApp.csproj.nuget.g.targets.
Restore completed in 894.27 ms for /home/sk/ostechnixApp/ostechnixApp.csproj.

Restore succeeded.

As you can see in the above output, .Net has created a new application of type console. The parameter -o creates a directory named "ostechnixApp" where you store your app data with all necessary files.

Let us switch to ostechnixApp directory and see what's in there.

$ cd ostechnixApp/
$ ls
obj ostechnixApp.csproj Program.cs

As you there are three files named ostechnixApp.csproj and Program.cs and one directory named obj. By default, the Program.cs file will contain the code to run the 'Hello World' program in the console. Let us have a look at the code.

$ cat Program.cs 
using System;

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

To run the newly created app, simply run the following command:

$ dotnet run
Hello World!
run dotnet

Run Hello World program using dotnet

Simple, isn't it? Yes, it is! Now, you can write your code in the Program.cs file and run it as shown above.

Alternatively, you can create a new directory, for example mycode, using commands:

$ mkdir ~/.mycode
$ cd mycode/

...and make that as your new development environment by running the following command:

$ dotnet new console

Sample output:

The template "Console Application" was created successfully.

Processing post-creation actions...
Running 'dotnet restore' on /home/sk/mycode/mycode.csproj...
Restoring packages for /home/sk/mycode/mycode.csproj...
Generating MSBuild file /home/sk/mycode/obj/mycode.csproj.nuget.g.props.
Generating MSBuild file /home/sk/mycode/obj/mycode.csproj.nuget.g.targets.
Restore completed in 331.87 ms for /home/sk/mycode/mycode.csproj.

Restore succeeded.

The above command will create two files named mycode.csproj and Program.cs and one directory named obj. Open the Program.cs file in your favorite editor, delete or modify the existing 'hello world' code with your own code.

Once the code is written, save and close the Program.cs file and run the app using command:

$ dotnet run

To check the installed .NET core SDK version, simply run:

$ dotnet --version
2.2.101

To get help, run:

$ dotnet --help

Get Microsoft Visual Studio Code Editor

To write the code, you can use your favorite editors of your choice. Microsoft has also its own editor named "Microsoft Visual Studio Code" with support for .NET. It is an open source, lightweight and powerful source code editor. It comes with built-in support for JavaScript, TypeScript and Node.js and has a rich ecosystem of extensions for other languages (such as C++, C#, Python, PHP, Go) and runtimes (such as .NET and Unity). It is a cross-platform code editor, so you can use it in Microsoft Windows, GNU/Linux, and Mac OS X. You can use it if you're interested.

To know how to install and use it on Linux, please refer the following guide.

This page has some basic tutorials to learn .NET Core and .NET Core SDK tools using Visual Studio Code editor. Go and check them to learn more.

Telemetry

By default, the .NET core SDK will collect the usage data using a feature called 'Telemetry'. The collected data is anonymous and shared to the development team and community under the Creative Commons Attribution License. So the .NET team will understand how the tools are used and decide how they can be improved over time. If you don't want to share your usage information, you can simply opt-out of telemetry by setting the DOTNET_CLI_TELEMETRY_OPTOUT environment variable to '1' or 'true' using your favorite shell.

And, that's all. You know how to install .NET Core SDK on various Linux platforms and how to create a basic app using it. To learn more about .NET usage, refer the official website given below.

Resource:

Thanks for stopping by!

Help us to help you:

Have a Good day!!

You May Also Like

4 comments

Eric D. December 23, 2018 - 3:18 am

I guess it’s because I’ve already “built” my development environment without Microsoft’s products….that I feel that I wouldn’t ever do this. Sorry, but MS had their chance of working with the open source community, but they blew it, now we’re supposed to jump for joy because they’re “giving” their Dev tools away? It’s too late! Why would I use anything from MS….when I have Code-Blocks…..Code-Lite…..Anjuta…..NotepadQQ……Ninja-IDE (for Python) Geany….and other tools that are better equipped and suited for the programming environment? Seems to me their effort to extend an olive branch….is 20 years too late. So I guess for those that want this…..more power to you…..as for me? No thanks.

Reply
Karl P December 23, 2018 - 10:05 pm

Unlike Eric, I’m excited about using C# on Linux. I’m not aware of another standard language that is statically typed, memory safe, high concurrency (async/await), and has proper error handling (Exceptions). We currently use python but we have overgrown it.
It’s good to have competition with Java.

Reply
Mark December 28, 2018 - 1:26 am

Just say no.

Reply
freedom_it_ January 28, 2019 - 6:37 am

Logically If I would use M$ technology in linux there is no reason for linux from my view. I would stay with windoze instead, for much better INTEGRATION of framework.
By the way M$ has been “made” a member of linux foundation by “board management”, for a little donation like half of a million.
If I could have some enough money even I could donate much more millions to linux foundation to exclude M$ from open source community…

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More