Linux Applications

Install and Get Started with .NET Core on CentOS

.NET Core also known as ASP.NET Core is a high performance, free and open source web framework. It is developed by Microsoft. .NET Core is a very powerful framework for developing web applications.

In this article, I am going to show you how to install and get started with .NET Core on CentOS 7. So, let’s get started.

Adding Microsoft Package Repository:

CentOS 7 does not have .NET Core packages in the official package repository of CentOS 7. But, you can easily add the official Microsoft package repository on CentOS 7 and install .NET Core from there using the YUM package manager.

Microsoft provides an RPM package to make it easy to add the Microsoft official package repository on CentOS 7.

To download and install the RPM package repository on CentOS 7, run the following command:

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

The RPM package should be installed.

Now, update the YUM package repository cache with the following command:

$ sudo yum makecache

The YUM package repository cache should be updated. As you can see, the Microsoft package repository is on the list. So, it is added correctly.

Installing .NET Core:

Now, you can install .NET Core SDK with the following command:

$ sudo yum install dotnet-sdk-2.2

To confirm the installation, press y and then press <Enter>.

YUM should start downloading and installing all the required RPM packages from the official package repository of Microsoft.

If you’re asked to accept the GPG key of the Microsoft official package repository, just press y and then press <Enter> to continue.

.NET Core SDK should be installed.

Now, to check whether the .NET Core SDK is working correctly, run the following command:

$ dotnet --version

As you can see, the version information is printed on the screen. So, it’s working.

Writing and Running Your First .NET Program:

In this section, I am going to show you how to write and run your first .NET program on CentOS 7.

First, navigate to a directory where you want to save your .NET project files. I save all my projects to ~/Projects/ directory. So, I am navigating to ~/Projects/

$ cd ~/Projects

Now, create a new .NET project helloWorld with the following command:

$ dotnet new console -o helloWorld

NOTE: Here, helloWorld is the name of the project. You can change it to anything you want.

A new project should be created.

A dedicated project directory should also be created as you can see in the screenshot below.

Now, navigate to the project directory helloWorld/ with the following command:

$ cd helloWorld/

In the project directory, you will have a Program.cs file. This is the main source code file. .NET Core uses C# (C-Sharp) programming language by default. So, you have to put your C# code in the Program.cs file.

By default, a simple program is written in the Program.cs file. The contents of the Program.cs file is shown in the screenshot below.

Here, the System library is imported with the using statement.

Then, a namespace helloWorld is created for the program.

C# is an Object-Oriented programming language. So, even a simple program as this needs to have a class of its own.  Here, a Program class is created. Notice that the class name is the same as the filename (without file extension).

The Program class has a Main method which takes an array of string as an argument. The Main method has void as the return type, so, it returns nothing.

Then, Console.WriteLine() function is used to print the text “Hello World” on the screen.

Now, let’s run the program to see if we really get the output we are expecting.

Now, run your .NET project as follows:

$ dotnet run

As you can see, the expected text is printed on the screen.

So, that’s how you install and run .NET projects on Ubuntu 18.04 LTS. Thanks for reading this article.

About the author

Shahriar Shovon

Freelancer & Linux System Administrator. Also loves Web API development with Node.js and JavaScript. I was born in Bangladesh. I am currently studying Electronics and Communication Engineering at Khulna University of Engineering & Technology (KUET), one of the demanding public engineering universities of Bangladesh.