Click here to Skip to main content
15,742,357 members
Articles / Programming Languages / C#
Posted 13 Aug 2020

Tagged as


33 bookmarked

Programming Linux With .NET Core 3: Introduction & Chapter 2

Rate me:
Please Sign up or sign in to vote.
5.00/5 (10 votes)
13 Aug 2020CPOL22 min read
Learn to program Linux via .NET Core 3 and Visual Studio code: Getting started
Download Visual Studio Code, open a terminal and start writing and running .NET Core 3 apps.


About one year ago, I built a new desktop computer and installed Win10 & Ubuntu as a dual boot machine.

I quickly became enamored with Ubuntu and Linux. I haven't booted into Win10 on this machine in 8 months now.

I've been running and developing on Windows systems since Win 3.1 so it was a big deal. However, I really like Microsoft's development environment and way of working. When I recently discovered that I could write C# code that ran on both Windows and Linux, I was really excited. It is truly amazing to write code on Windows and then bring it over and build and run it on Linux with no changes. No, you still can't build a Windows Form app on Linux but look beyond that. There are a lot of other things you can do without the need for Windows Forms or graphical User Interface. That's why I decided to write this book. I wanted to just see what we can do using only Visual Studio Code and the .NET Core SDK.

I'm still deciding how much of the beginning details I want to include in the book so a lot of readers here at CodeProject may find themselves skipping over explanations of data types and basics, but I hope this will open up the idea that you can write C# on Linux and it is really cool and a lot of fun.

What I'm Hoping To Achieve

I am just trying to write an accessible (anyone can code) fun book which lets me and my readers get an idea of what we can do on the Linux platform using .NET Core. I'm hoping it even harkens back to K&R C a bit -- although mine surely won't be nearly as clear and direct as that great little resource. In the end, I hope I provide something that becomes a discussion starter about .NET & C# on Linux that energizes readers to have fun with development.


What Does This Book Cover?

This book focuses on using Visual Studio Code and the Linux command line to create and build projects and applications based on DotNet Core 3.x.

All programs will be written in C#. Of course, Web-based projects may include TypeScript, JavaScript, CSS, HTML, etc.

Who Is This Book For?

This book is a very fast introduction to Programming Linux with DotNet Core. If you are a brave beginner, you will be able to use the book to begin learning and writing software on Linux (I run Ubuntu) using C# and the DotNet Core 3.x libraries.

I will do my best to introduce material in such a way that even if you’re a beginner, you’ll be able to follow the steps and start learning. However, I may move really fast on some topics and you may require other resources to obtain more depth on certain subjects.

The main audience for this book is people with some development experience. If you’ve been doing some coding for at least three to six months and you like programming, then you’ll do fine here. If you have a lot more experience, I believe you’ll find this a nice introduction to various things you can create on Linux with DotNet Core 3.x and associated technologies.


I will provide sample code on all of the topics I touch upon, but I will not stay limited to just sample code. We will work through at least three complete utilities (often console-based - text user interface TUI) but also some web applications where appropriate.

I will not focus on User Interface issues. This book is more about the code and less about the User Interface (UI). However, I like to create applications that are as easy to use as possible so where appropriate and applicable, I will discuss creating a good user experience (UX) though that may not always be with graphical UI. And, when it is a graphical UI, it may be a web-based UI.

What This Book Is Not

Again, this book is about learning to program in the Linux environment by using the dotnet command-line and Visual Studio Code. It is about learning to code, with only a slight emphasis on how to create User Interfaces. If you’re looking for the Linux version of creating WinForm apps, this is not it.

What This Book Is

But, if you’re interested in learning dotnet core, C#, writing utilities and learning more about Linux basics, this is the place for you.

I really think of this book as a hobbyist’s run through Linux while learning DotNet Core programming. It’s supposed to be fun...but I’m a geek. :)

All Source Code At GitHub

I’ll make all of the code available in a GitHub repository (repo) and it’ll all be open source and freely available for use in your own projects.

DotNet Core

The dotnet command-line (console based) application is amazing and extremely easy to use. If you’re a Linux user, you’re probably accustomed to using the command-line anyways. This book will focus on using dotnet console application to do everything that we can. That includes creating projects, compiling and building from source files, adding libraries, and everything else.

That should give you an idea about how this book will work and what you’ll learn. Now, let’s get started with installing dotnet core, Visual Studio Code and writing our first console app.

MacOS Bonus

I've also tested these programs on my MacMini. You can install Visual Studio Code, .NET Core SDK and .NET Core runtime and run everything in a terminal window there even though Macs run the zsh (shell) by default.

The commands you follow in this book will be the same whether you're running a Mac or Linux. FYI - there may be some differences when installing Visual Studio Code and the .NET SDK, etc. but the differences will be easy enough that you can figure them out.

Chapter 2 - Jumping Into .NET Core

  • Linux terminal (command-line)
  • Installing .Net Core 3 SDK and Runtime
  • Installing Visual Studio Code
  • Building and running our first console (terminal) program

Linux Terminal

The first thing we want to do is open up a terminal and check to see if you have dotnet installed.

If you can’t find the terminal on your Linux box, and if you are running Gnome desktop (like I am on Ubuntu), then you can click the Activities menu (it’s kind of like the Win10 Start button) and then type: terminal.

Image 1

Finally, just click the Terminal icon and it’ll open up for you.

Test for dotnet Executable

Once you have a command prompt, type the following and press <ENTER>.

dotnet --version

Image 2

If you are on a system where the dotnet sdk has not been installed, then you’ll get a reply something like mine that the command is not found.

Installing .NET Core SDK

Let’s install dotnet.

Open up your favorite web browser (mine’s FireFox) and go to:

You’ll see something like:

Image 3

Go ahead and click the [Install .NET Core] button.

This will take you to a long page that lists all of the various Linux distributions on which you can install .NET Core (I’ll probably vary how I refer to dotnet core throughout the book).

Image 4

I Run Ubuntu 18.04 and Debian 10

I’m actually running Ubuntu 18.04 Desktop as my main machine. However, you may notice that I follow the Debian installation. That’s because I’m running Debian (with Gnome desktop) in a VirtualBox instance so I can walk you through all of the fresh installation work. After testing, I’ve determined that the commands for installing on Ubuntu 18.04 and Debian 10 are exactly the same, so they are interchangeable. That’s because Ubuntu is based on Debian.

Ubuntu 18.04 Or Debian 10

Here’s the first step that you’ll need to start installing on Ubuntu 18.04:

Copy the following, paste it into your terminal and press <ENTER> and it will start running.

-O packages-microsoft-prod.deb

When that completes, copy the next line and paste it into your terminal window and press <ENTER>.

sudo dpkg -i packages-microsoft-prod.deb

You can see the first command ran and now the terminal is waiting for me to hit <ENTER> to start the second command:

Image 5

I actually forgot to add sudo when I ran the dpkg command and I got an error. But here's what happened when I ran it again under sudo.

Image 6

After I typed my password, I saw the following:

Image 7

It mentions the sudoers file which is a file that contains the usernames of users which are allowed to elevate to superuser status.

Linux Sudo (Super User)

This is important, because this book is all about learning Linux too and you’re bound to run into these types of things.

Let’s see how to fix this problem.

Type su root and press <ENTER>.

You will be prompted for your root password. I hope you remember it. :)

Image 8

I typed mine in and now you can see that I’m logged in as root, because in the next image the prompt has the root@debian prefix which displays the user that is logged in.

We need to give my user account, raddev, sudo powers. In your case, you need to give your username sudo powers.

Install Sudo

To do that, you may have to install sudo, as I did.

Type the following and press <ENTER>.

apt-get install sudo -y

Image 9

Add User to Sudo File

Now you can add the user to the sudo file.

Type the following and press <ENTER> again.

sudo adduser raddev sudo

Image 10

Exit Root

Now, let’s exit from the root user. To do so, type:


and press <ENTER>.

You’ll be back on your user account. Now, let’s try installing dotnet core sdk again.

Log Out / Log In

Mine, didn’t work. I tried again and found out that you need to log out and login again (or reboot) before your account will have sudo rights, so go ahead and log out and log back in again. That worked for me.

Once you’re logged in, go ahead and open a terminal again and then run the following command again:

sudo dpkg -i packages-microsoft-prod.db

Image 11

Mine installed successfully.

Install the .NET Core SDK

The .NET Core SDK is the set of libraries that have all of the .NET Core functionality that allows you to write programs in .NET Core. This is the set of libraries that Microsoft has created which wrap up Operating System calls to create files, get input from the user, display output to user and thousands of other things. SDK stands for Software Development Kit.

This is the kit that you will use to build your custom programs which run on top of the .NET Core.

When you learn how to write programs using the .NET Core SDK, you are learning how to program in .NET Core.

Now we install the main dotnet sdk with the following long multi-line command:

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

Copy and paste it into your terminal, press <ENTER> and it will start installing.

When it completes, you’ll see something like the following:

Image 12

.NET Core Runtime

Now we need to install the DotNet Core runtime. The .NET Core Runtime is the application which will actually run the code you write. You will write code which calls functions that are in the .NET Core SDK, then you will build that code into an executable and run that code inside the .NET Runtime.

Here’s the command we will run in the terminal window:

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

Here’s the result:

Image 13

Everything is Installed

That’s everything we need for the dotnet core sdk and runtime. Let’s check it and see what we have.

$ Prompt : Linux Terminal

For now on, I will just show you the $ prompt and then the command you will type.

That way, I won’t keep saying, type <ENTER>.

It will look like:

$ command command-options

$ ls -al 

Any time you see that, then run the command just as it is shown. Of course, to submit any command in the terminal, you have to press <ENTER>.

Dotnet Is An Executable

Now that .NET Core is installed, we can run the dotnet command.

If we pass the --version to the dotnet command, it will let us know which version is installed.

Go to your terminal and type:

$ dotnet --version  

Note: Keep in mind that these commands are case-sensitive so you need to type them exactly as they appear here.

Image 14

We are going to use dotnet for many things so you’ll be seeing much more of it as we go. We’ll use it to create our projects, build the code, run our apps and more. However, right now, we need a code editor.

We Need a Code Editor

We are on our way to developing our first app. However, we need an editor that will make it easier to write our code. Of course, the editor we are using in this book is the freely available Visual Studio Code.

Visual Studio Code Is Not Visual Studio

Keep in mind that Visual Studio Code is a light-weight editor (in comparison to Visual Studio).

Visual Studio won’t actually run on Linux. There is a Mac version of Visual Studio, but there is not one for Linux. But that is fine, because Visual Studio Code is an amazing editor and very close to a full IDE (Integrated Development Environment).

Visual Studio Code doesn’t have all the project templates and all the tools that Visual Studio has, but I believe once you run it you will be amazed.

We will eventually learn how to add a debugger plugin that will allow us to step through our code.

Installing Visual Studio Code (VSC)

To learn more about VSC, just point your favorite browser at:

However, there is a better way to install Visual Studio Code than by downloading the Debian package from the site. I will walk you through the smoothest and best way to install Visual Studio Code on Linux.

Yes, it’s more terminal installation. Run the following command:

$ sudo apt update

Image 15

Next, run this:

$ sudo apt install software-properties-common apt-transport-https curl

Image 16

It’ll confirm that you want to install the packages. Type y and press <ENTER>

Image 17

Now we use curl to import the Microsoft GPG key (security key) required to get Microsoft apps.

Image 18

The Visual Studio Code repository has to be added to our system so it can be installed.

Note: The following command is so long that it wraps to the next line but there is not a carriage return in there.

$ sudo add-apt-repository "deb [arch=amd64] stable main"

Image 19

There’s no output, it just installs the repository and returns to prompt.

Run the following:

$ sudo apt update

Image 20

You can see that the new Microsoft repo list is now read in.

Run the Install of Visual Studio Code

Now, we can finally install Visual Studio Code.

$ sudo apt install code

Image 21

Visual Studio Code Install Complete

Visual Studio Code is officially installed.

Let’s start it up.

Click Activities and type “Visual” in the search bar. You’ll see the Visual Studio Code icon.

Image 22

Click the icon and Visual Studio Code will start up.

Image 23

To learn how to use Visual Studio Code (VSC), we need to create a project so we can begin to examine the many features of Visual Studio Code.

Create a New Project

Let’s switch back to a terminal window and create a project using the dotnet command line tool.

Setting Up Directories

Let’s take a moment to talk about how I set up my directories to organize my development work.

From my home directory, I create a directory named dev.

Under that directory, I will have directories named after the various technology types I am working on such as webdev, dotnet, C++, etc.

In our case, from our home directory, we will create a directory named dev and then a dotnet directory under that one.

What the Linux Prompt Displays

Here’s what my prompt looks like when I am in that directory:


That means that the user raddev is logged into a computer with the name raddev-1.

After the colon (:), you can see the current path which is ~/dev/dotnet and finally, there is the prompt $. Anything you type comes after the $ prompt.

Home Directory

Keep in mind that ~ represents your home directory. That means if you give the command:

$ cd ~

Then you will be moved to your home directory.

However, the full path of that directory is /home/username where username is your username.

Print Working Directory (pwd)

If you ever want to see the full path of the directory you are in, you can issue the pwd command:

$ pwd

Image 24

As you can see, I did a change directory (cd) to home ~ and then I did a pwd and now I’m in the /home/raddev directory.

Now you’ll have a better idea about where you are in the Linux file system.

I have created a dev directory under my home dir. And under the dev directory, I have created a dotnet directory. I’ll move back into the directory from my home with:

$cd dev/dotnet

Image 25

Make Directory (mkdir)

You can copy/paste the command below and run it right now and then you’ll have the same directory structure that I have.

$ mkdir -p ~/dev/dotnet

That creates the dev directory under your home directory and a dotnet directory under the dev directory.

The -p parameter creates any parent directories (dev) that may not exist.

Home Directory (~) Really Helps

It’s very cool that the ~ represents you home directory /home/username because I don’t have to know your username to provide you with a good mkdir command. Instead, we just use ~ to refer to that part of the path.

Once you run the command, go ahead and change directory to the directory.

$ cd ~/dev/dotnet

Once you are in the directory, you’ll know you are because you’ll see the path right before the $ prompt.

Image 26

We are ready to create a new dotnet project.

We are going to create a simple console (terminal) based program. It’s very easy to do.

All we have to do is tell dotnet two things:

  1. What type of project we want to create (console)
    • We have to tell it the type of project, because, as you will see, you can create many different types of .NET projects using dotnet command.
  2. What we want to name the new project.

dotnet new

Make sure you are in the proper folder (~/dev/dotnet/). Go ahead and run the command:

$dotnet new console -o FirstConsole

Image 27

Folders In Folders

The -o in the dotnet command tells dotnet the output name of the project. This will create a new folder in the folder that you are currently in. That’s why you want to be in the ~/dev/dotnet folder. When the project is created, you’ll have a new folder ~/deve/dotnet/FirstConsole/ and inside that folder will be all of the project files that the dotnet command creates.

Change directory into the new project directory:

$ cd FirstConsole

Do a list of the directory (lsl):

$ ls -al

Image 28

There is only one source file in this project (Program.cs). The other file is the project file.

We can build the program right now.

$ dotnet build

Image 29

The program compiled and built successfully because we did not receive any warnings or errors.

Now, we can run the program.

$ dotnet run

Image 30

That’s it. Just one line of output.

By the way, dotnet will build and run the app for you when you give the one single command:

$ dotnet run

That way, if you just want to run the application but you’ve made changes, then you just give that one command and dotnet does all the work for you.

Visual Studio Code

Let’s open this project up in Visual Studio Code and take a look at the source code to close out this first chapter.

If VSC isn’t open, go ahead and open it up.

You’ll see the normal Start Page in VSC.

Go to the [File] menu and select the [Open Folder…] menu item.

Image 31

Once you select the menu item, a file browser app will open up.

You’ll click on [Home] on the left and then navigate down into dev/dotnet/ and select the FirstConsole folder that dotnet created for your project.

Once you click the FirstConsole folder, then click the [OK] button in the upper right.

Image 32

Visual Studio Code will load the folder and files and will display them in the file view on the left side.

Image 33

You can see that the VSC title bar contains the name of the project (FirstConsole).

And, on the left side, there are seven icons (5 at top, 2 at bottom) which allow you to navigate to other VSC functionality.

Currently the File view icon is selected.

Image 34

The magnifying glass is the search functionality.Image 35

The three-node shape allows you to access Git, the version control software.Image 36 This allows you to track changes in your source code.

The bug with the triangle allows you to access the debuggerImage 37 .

The icon with four blocksImage 38 allows you to access plugins that help you extend the functionality of VSC. One of these will be critical to use since it will provide Intellisense features (code completion and information) and allow us to debug and step over our C# code line-by-line.

The circle person icon allows you to sign into your Microsoft developer account.Image 39

Finally, the gear allows you to get to VSC configuration items and to check for updates and more. Image 40

There are no files open right now, but let’s go ahead and click on Program.cs file in the file navigator so we can edit some code.

Image 41

Install Plugin

When you click that item, you will see the code appear on the right and a small pop up dialog box will appear down at the right hand side of VSC.

VSC knows that you opened a C# file so it’s attempting to help by letting you know that you want to install the C# plugin which will provide you with additional features. Go ahead and click the [Install] button.

When you do, it will begin to install.

Image 42

Notice that the plugin icon is now chosen (highlighted white). You can see that the package OmniSharp for Linux (x64) was installed.

Click the File Navigator icon (top icon) again so you can see your C# Program.cs file again.

Select the Program.cs file on the left so the file will open in the editor.

Let’s change the text that is output to the console (terminal) so that instead of just “Hello World!”, the user will see “Hello, Linux-World!”.

All you have to do is click inside the file and type that code between the double-quotes.

Console.WriteLine("Hello, Linux-World!");

Image 43

Small Blue Circle With Number

You may see a small blue circle with a number inside it appear on top of the document icon in the upper left. That is an indication that the file has changed but hasn’t been saved.

Go ahead and save the file (CTRL+s) and the blue circle will disappear. Obviously, to see the changes you’ve made in your source code appear when you run your program, you have to save them to your file.

Flip back over to your terminal. You should still be in the ~/dev/dotnet/FirstConsole directory.

Run the following command and you’ll see the changes you made. Keep in mind, when you execute the run command, you are also telling dotnet to build the source code if there are changes.

$ dotnet run

Image 44

How Does the Program Work?

The basic code simply defines a Main() method or function. This is considered the entry point into the application. That just means it is where everything starts.

That means when you tell dotnet to build the application it sets up some internal things so that the Operating System knows where to look to start the program.

When this program starts, it only has one command to run (Console.WriteLine).

System.Console Library

That command is actually a function that was written by the C# developers. The actual function name is WriteLine, but they put the function inside a library named Console. They do this to organize functions. Actually the full name of the library is System.Console.

The reason you don’t see the System part of the name is because it has been added at the top of the Program.cs file (using System;) so that you don’t have to type it every time.

Libraries Like Boxes

You can think of these libraries like boxes. When you want to run a particular function, you need to go to the correct box and look inside it. Some boxes (libraries) have other boxes inside them.

In our case, we looked inside the System library (box) and we found the Console library and then inside there we found a method or function named WriteLine() that knows how to output a message to the terminal (console) window.

You can look this Library and function up on Microsoft’s developer web site. It looks something like the following:

Image 45

Notice that on the left side, you can see the WriteLine method has been selected and on the right side we see the main document which explains the method.

From the top, we see the name Console.WriteLine, next we see the Namespace (Library or box) which is System. The next thing we see is Assembly: System.Console.dll. That is the actual file that is a part of the .NET Core SDK that we installed. It is a special file that has the functions (methods) inside it that we call from our programs. DLL stands for Dynamic Link Library. That is a fancy way of saying that you can link this special library into you program as needed (dynamically) so that you can have functionality that the Microsoft developers created for your use.

Finally, under the Overloads section (which means there are many ways to call the WriteLine method), we see the second option WriteLine(String) and a definition (highlighted in grey).

This is the form of the WriteLine() method that we are using. We pass it a String (one or more characters wrapped in double quotes indicates a String type).

When we pass it a String, it writes it to the standard output stream (terminal).

We will go into far more details as we move through this book. For now, this is a good solid understanding of what is happening in this first basic program.


This has been a long introduction into the world of programming and into a bit of the Linux world. You’ve learned a lot to get this far and now you have an understanding of:

  1. How to use Visual Studio Code
  2. How to move around in the Linux File System
  3. How to create directories
  4. How to use the dotnet command to create, build and run projects
  5. Basics of what a simple Console app does and how it does it


  • 13th August, 2020: First publication


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Software Developer (Senior) RADDev Publishing
United States United States
"Everything should be made as simple as possible, but not simpler."

Comments and Discussions

Question"No, you still can't build a Windows Form app on Linux .." is wrong. Pin
krunoslav ostrouska17-Aug-20 3:18
krunoslav ostrouska17-Aug-20 3:18 
Question+points for 'MacOS' Pin
Garth J Lancaster13-Aug-20 22:52
professionalGarth J Lancaster13-Aug-20 22:52 
AnswerRe: +points for 'MacOS' Pin
raddevus14-Aug-20 3:16
mvaraddevus14-Aug-20 3:16 
QuestionHave you looked at WSL2 for your Windows Environment Pin
Matthew Dennis13-Aug-20 15:24
sysadminMatthew Dennis13-Aug-20 15:24 
AnswerRe: Have you looked at WSL2 for your Windows Environment Pin
raddevus14-Aug-20 3:17
mvaraddevus14-Aug-20 3:17 
GeneralRe: Have you looked at WSL2 for your Windows Environment Pin
mldisibio14-Aug-20 13:26
mldisibio14-Aug-20 13:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.