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

How To Install Microsoft .NET Core SDK On Linux

By sk
Published: Last Updated on 9.3K views

The .NET Core is 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.

1. Install Microsoft .NET Core SDK on Linux

As stated already, the .NET core is cross-platform, so it supports GNU/Linux, Mac OS and Windows. As of updating this guide, the latest .NET core version was 7.0.

The .Net core SDK is packaged for popular GNU/Linux operating systems including Debian, Fedora, CentOS Stream, Oracle Linux, RHEL, AlmaLinux, Rocky Linux, SUSE/openSUSE, and Ubuntu.

We can install .NET on Linux either manually, or via package manager, or using installer script. In this guide, we will see how to install .NET using the package manager and using the script.

1.1. Install .NET SDK using Package Manager

Heads Up: If you install .NET SDK, you don't need to install the corresponding runtime. The .NET SDK includes the runtime to develop and run .NET apps. Or, if you only need to run apps, install the Runtime. If you're installing the Runtime, I suggest you to install the ASP.NET Core Runtime as it includes both .NET and ASP.NET Core runtimes. .NET Core Runtime doesn't include ASP.NET Core Runtime.

1.1.1. Install .NET SDK in Alpine Linux

To install .NET SDK in Alpine Linux, run:

$ sudo apk add dotnet7-sdk

Install ASP.NET Core Runtime in Alpine Linux:

$ sudo apk add aspnetcore7-runtime

Install .NET Core Runtime in Alpine Linux:

$ sudo apk add dotnet7-runtime

1.1.2. Install .NET SDK in Debian

First, add the Microsoft package signing key to your list of trusted keys and add the package repository by running the following commands.

Debian 11:

$ wget https://packages.microsoft.com/config/debian/11/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
$ sudo dpkg -i packages-microsoft-prod.deb
$ rm packages-microsoft-prod.deb

Debian 10:

$ wget https://packages.microsoft.com/config/debian/10/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
$ sudo dpkg -i packages-microsoft-prod.deb
$ rm packages-microsoft-prod.deb

Update the repository lists and install .NET core SDK using commands:

$ sudo apt-get update
$ sudo apt-get install -y dotnet-sdk-7.0

Install ASP.NET Core Runtime:

$ sudo apt-get install -y aspnetcore-runtime-7.0

Install .NET Core Runtime:

$ sudo apt-get install -y dotnet-runtime-7.0

1.1.3. Install .NET SDK in Fedora

Heads Up: .NET 7 is not yet ready for Fedora. So install .NET version 6 in the meantime. We will update the guide after the version 7 is ready.

.NET 6 is available in the default repositories. To Install .NET Core SDK in Fedora, run:

$ sudo dnf install dotnet-sdk-6.0

Install ASP.NET Core Runtime:

$ sudo dnf install aspnetcore-runtime-6.0

Install .NET Core Runtime:

$ sudo dnf install dotnet-runtime-6.0

1.1.4. Install .NET SDK in CentOS Stream

.NET is available in the AppStream repositories for CentOS Stream 9 and 8 versions.

To install the latest .NET SDK in CentOS Stream, run:

$ sudo dnf install dotnet-sdk-7.0

Install ASP.NET Core Runtime:

$ sudo dnf install aspnetcore-runtime-7.0

Install .NET Core Runtime:

$ sudo dnf install dotnet-runtime-7.0

1.1.5. Install .NET SDK in RHEL, AlmaLinux, Rocky Linux

.NET is included in the AppStream repositories for RHEL, AlmaLinux and Rocky Linux 9 and 8 versions.

To install the latest .NET SDK in CentOS Stream, run:

$ sudo dnf install dotnet-sdk-7.0

Install ASP.NET Core Runtime:

$ sudo dnf install aspnetcore-runtime-7.0

Install .NET Core Runtime:

$ sudo dnf install dotnet-runtime-7.0

1.1.6. Install .NET SDK in openSUSE

Add the Microsoft package signing key and the package repository:

$ sudo zypper install libicu
$ sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc
$ wget 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-7.0

Install ASP.NET Core Runtime:

$ sudo zypper install aspnetcore-runtime-7.0

Install .NET Core Runtime:

$ sudo zypper install dotnet-runtime-7.0

1.1.7. Install .NET Core SDK in Ubuntu

Add the Microsoft package signing key to your list of trusted keys and add the package repository in Ubuntu 22.10 and earlier versions:

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

Update the package repository lists and Install .NET Core SDK using commands:

$ sudo apt-get update
$ sudo apt-get install -y dotnet-sdk-7.0

Install ASP.NET Core Runtime:

$ sudo apt-get install -y aspnetcore-runtime-7.0

Install .NET Core Runtime:

$ sudo apt-get install -y dotnet-runtime-7.0

For other distributions, refer the official installation section.

1.2. Install .NET SDK using Installer Script

The .NET developers have created a script to automate the .NET SDK installation. This script should work on Linux distributions that has BASH installed.

Please note that the SDK needs to be installed without user interaction and without admin rights.

Download the .NET installer script from https://dot.net/v1/dotnet-install.sh link.

$ wget https://dot.net/v1/dotnet-install.sh

Make the script executable using command:

$ sudo chmod +x ./dotnet-install.sh

Now, run the script to install .NET SDK on Linux. Please note that you should run this command as regular user (i.e. without admin rights)

$ ./dotnet-install.sh

Sample Output:

dotnet-install: Note that the intended use of this script is for Continuous Integration (CI) scenarios, where:
dotnet-install: - The SDK needs to be installed without user interaction and without admin rights.
dotnet-install: - The SDK installation doesn't need to persist across multiple CI runs.
dotnet-install: To set up a development environment or to run apps, use installers rather than this script. Visit https://dotnet.microsoft.com/download to get the installer.

dotnet-install: Attempting to download using aka.ms link https://dotnetcli.azureedge.net/dotnet/Sdk/6.0.404/dotnet-sdk-6.0.404-linux-x64.tar.gz
dotnet-install: Extracting zip from https://dotnetcli.azureedge.net/dotnet/Sdk/6.0.404/dotnet-sdk-6.0.404-linux-x64.tar.gz
dotnet-install: Installed version is 6.0.404
dotnet-install: Adding to current process PATH: `/home/ostechnix/.dotnet`. Note: This change will be visible only when sourcing script.
dotnet-install: Note that the script does not resolve dependencies during installation.
dotnet-install: To check the list of dependencies, go to https://learn.microsoft.com/dotnet/core/install, select your operating system and check the "Dependencies" section.
dotnet-install: Installation finished successfully.
Install .NET SDK on Linux using Installer Script
Install .NET SDK on Linux using Installer Script

By default, the script installs the LTS version of .NET SDK, which is version 6.

If you want install .NET SDK 7 version, simply use the --version latest parameter:

$ ./dotnet-install.sh --version latest

If you want to install .NET Runtime instead of the SDK, use the --runtime parameter.

$ ./dotnet-install.sh --version latest --runtime aspnetcore

You can also install a specific major version, for example .NET 7.0 SDK, with the --channel parameter:

$ ./dotnet-install.sh --channel 7.0

Finally, add the dotnet PATH (i.e. /home/ostechnix/.dotnet) to your ~/.profile or ~/.bashrc or equivalent and log out and log back in.

$ vi ~/.bashrc

Add the following line:

export PATH="$PATH:/home/ostechnix/.dotnet"

Replace ostechnix with your own.

Source the ~/.bashrc file or log out and log back in to take effect the changes.

$ source ~/.bashrc

2. Verify .NET Installation

To check if .NET is installed on your machine, do the following.

Check the installed .NET core SDK version using command:

$ dotnet --version
7.0.101

Check installed SDK versions:

$ dotnet --list-sdks
7.0.101 [/usr/share/dotnet/sdk]

Check installed Runtime versions:

$ dotnet --list-runtimes
Microsoft.AspNetCore.App 7.0.1 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.NETCore.App 7.0.1 [/usr/share/dotnet/shared/Microsoft.NETCore.App]

To get help, run:

$ dotnet --help

To view the help manual of a sub-command, run:

$ dotnet [command] --help

3. Create your First App using Dotnet

We have successfully installed Dotnet 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:

The template "Console App" was created successfully.

Processing post-creation actions...
Restoring /home/ostechnix/ostechnixApp/ostechnixApp.csproj:
  Determining projects to restore...
  Restored /home/ostechnix/ostechnixApp/ostechnixApp.csproj (in 57 ms).
Restore succeeded.
Create New Console App
Create New Console App

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
Program.cs  obj  ostechnixApp.csproj

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 
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");

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

$ dotnet run
Hello, World!
Run Hello World Program using 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/ostechnix/mycode/mycode.csproj...
Restoring packages for /home/ostechnix/mycode/mycode.csproj...
Generating MSBuild file /home/ostechnix/mycode/obj/mycode.csproj.nuget.g.props.
Generating MSBuild file /home/ostechnix/mycode/obj/mycode.csproj.nuget.g.targets.
Restore completed in 331.87 ms for /home/ostechnix/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

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

Visual Studio Code 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.

5. 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 now 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:

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. By using this site, we will assume that you're OK with it. Accept Read More