Setting up .NET Core development environment on Ubuntu Linux
Image from Unsplash

Setting up .NET Core development environment on Ubuntu Linux

A quick guide for switching from Windows based Visual Studio 2017 to Visual Studio Code

With growing popularity of dotnet Core, Git and Docker, developers who used to work mainly on Windows are now getting used to CLI environment and obviously Linux. Windows is still a development environment platform of choice, bit since Microsoft already developed and published Visual Studio Code as a cross platform IDE for Windows, Linux and Mac.

Although it is still not mature as Visual Studio 2017, it is a decent tool for .NET Core applications development. It relies a lot on the CLI and many things like adding extensions and creating solution and project are still left to the dotnet and core CLI, but my opinion is that over time there will be extensions for .NET Core platform which will make it as comfortable as development with Visual Studio 2017.

With all these new things developed to work across multiple platforms, there is no obstacle why you should not consider using different operating system other than Windows to setup your development environment. In the following steps I'll try to walk you through steps for setting up Linux OS as your development environment. I will use Ubuntu 18 as my Linux OS of choice but you can use any other Linux distro you prefer although steps may be a bit different depending on the package manager you distro of choice uses. Ubuntu has a great community support and frequent updates, so it is the easiest on in my opinion for the transition from Windows.

Now let's start with all the steps to make our Ubuntu Linux machine up and running our .NET Core code.

Setting up dotnet Core SDK 

Microsoft is providing latest SDK for download from https://www.microsoft.com/net/download but you can also use command line to install it following the instruction from Microsft's page for installing .NET Core SDK on Ubuntu 18 https://www.microsoft.com/net/download/linux-package-manager/ubuntu18-04/sdk-current or you can just copy paste the following commands to you Ubuntu command line window and execute them.

Let's first list all the options of dotnet new command to see how we create projects using different project templates

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

sudo apt-get install apt-transport-https
sudo apt-get update
sudo apt-get install dotnet-sdk-2.1
    

Once installing of .NET Core packages is done, make sure that dotnet is available from command line by executing

dotnet --version
    

The output should give you the installed version od dotnet Core SDK

dejan@ubuntu:~$ dotnet --version
2.1.402

Creating the solution and new project from command line

Since VSCode is not developed solely for .NET development it does not come with options to create new .NET solution and projects, so we'll use dotnet CLI to initially setup our solution. I guess this can be done with VSCode extension, but so far I did not find any that is doing this job well, so we are going to do it from the dotnet CLI.

First let's list the options for the new project templates

Templates                                         Short Name         Language          Tags                                 
---------------------------------------------------------------------------------------------------------------------------
Console Application                               console            [C#], F#, VB      Common/Console                       
Class library                                     classlib           [C#], F#, VB      Common/Library                       
Unit Test Project                                 mstest             [C#], F#, VB      Test/MSTest                          
NUnit 3 Test Project                              nunit              [C#], F#, VB      Test/NUnit                           
NUnit 3 Test Item                                 nunit-test         [C#], F#, VB      Test/NUnit                           
xUnit Test Project                                xunit              [C#], F#, VB      Test/xUnit                           
Razor Page                                        page               [C#]              Web/ASP.NET                          
MVC ViewImports                                   viewimports        [C#]              Web/ASP.NET                          
MVC ViewStart                                     viewstart          [C#]              Web/ASP.NET                          
ASP.NET Core Empty                                web                [C#], F#          Web/Empty                            
ASP.NET Core Web App (Model-View-Controller)      mvc                [C#], F#          Web/MVC                              
ASP.NET Core Web App                              razor              [C#]              Web/MVC/Razor Pages                  
ASP.NET Core with Angular                         angular            [C#]              Web/MVC/SPA                          
ASP.NET Core with React.js                        react              [C#]              Web/MVC/SPA                          
ASP.NET Core with React.js and Redux              reactredux         [C#]              Web/MVC/SPA                          
Razor Class Library                               razorclasslib      [C#]              Web/Razor/Library/Razor Class Library
ASP.NET Core Web API                              webapi             [C#], F#          Web/WebAPI                           
global.json file                                  globaljson                           Config                               
NuGet Config                                      nugetconfig                          Config                               
Web Config                                        webconfig                            Config                               
Solution File                                     sln                                  Solution                             

OK, now we can start by first creating the solution. To have things organized on on our file system, I created a folder in my home directory named Projects. Now in projects I tell dotnet cli to initialize solution. This will create .sln file and a folder for the project.

mkdir Projects
cd Projects

dotnet new classlib -o SampleProject.Data
dotnet new webapi -o SampleProject.Web.Api

dotnet sln SampleProject.sln add SampleProject.Data/SampleProject.Data.csproj

dotnet sln SampleProject.sln add SampleProject.Web.Api/SampleProject.Web.Api.csproj
    

Once we have the solution and projects created, we can add projects to sulution and we have our sample project ready to load in VSCode IDE and start debugging

Installing Visual Studio Code (VSCode)

Now we need our IDE to work on our project we created with dotnet CLI. There are few choices for .NET Core. I am going to focus here on Visual Studio Code since it is free and good enough to edit and debug you .NET Core applications. Apart from it there is JetBrains Rider, a powerful cross-platform IDE for .NET but it is not free.

So let's dig into the VSCode setup. You can download the package for Debian directly from official VSCode page https://code.visualstudio.com/, use the Ubuntu Software Center or just do it from the command line. I decided to do it from the command line. There is nice walk though for setting up VSCode on Linux on official VSCode page https://code.visualstudio.com/docs/setup/linux or you can just take the following snippet and run it in your Ubuntu terminal

sudo curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.gpg
sudo install -o root -g root -m 644 microsoft.gpg /etc/apt/trusted.gpg.d/
sudo sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/vscode stable main" > /etc/apt/sources.list.d/vscode.list'

sudo apt-get update
sudo apt-get install code
    

VSCode IDE comes with CLI included so you can manage it either from the IDE UI directly or through the CLI that comes with it. Just to quicly very, check the VSCode version installed from the terminal.

code --version
    

And you should get something like this in the repose 

dejan@ubuntu:~/Projects/SampleProject$ code --version
1.27.1
5944e81f3c46a3938a82c701f96d7a59b074cfdc
x64

You will need some extensions in order to work with .NET Core projects. This can be done from the VSCode IDE or you can also do it from the console. Two extensions I alwyas keep with my VSCode instance are: 

Let's install them through the command line as well

code --install-extension ms-vscode.csharp

code --install-extension fernandoescolar.vscode-solution-explorer
    

Now we are ready to open our sample project and get it up and running in debug mode.

Debugging code from VSCode

Once you open your project folder VSCode will setup and start OmniSharp Host to allow you to debug your code. Unfortunately if you have experienced failing to create necessary files because of lack of permission (if project folder is created with sudo) you might have to start VSCode as sudo as well. This can be easily solved by starting VSCode as sudo. You cannot just start it as sudo because it will require you to set the user profile folder. This can be done by passing parameter --user-data-dir

So go to terminal once again and start code as sudo with our profile folder

sudo code --user-data-dir profile
    

If you did not use sudo to create your Projects folder, everything should work without any hacks and you should be able to start VSCode from the Software center. 

You can now open your solution folder from the IDE and wait for Solution Explorer extension to parse your .sln file. It will add one more section in the three with solution organized as it is in your solution. The folder in our case with be in $HOME/Projects/SampleProject

Vscode Ubuntu

We have out solution loaded with both projects where one of them is Web API project. Since we can check the output of it in the browser once it is started, we will use this one for the debug demo. Navigate to your controller class in Web Api project and set the breakpoint to return line of the get HTTP method action.

Switch to debug view in VSCode and select .NET Core Launch (Web) option from the drop down menu. Now hit the play button to start debug. You will see in the console output that application is started and listening on port 5000.

You can navigate to http://localhost:5000/api/values in your browser or in terminal using curl and you will get the VSCode window focused with the breakpoint hit by your call to the endpoint inside the controller class

Note

You will get invalid certificate error in your browser when you try to open the endpoint but you can ignore it through browser options. This is not the case with curl. To get the response from curl you need to switch off HTTPS from the pipeline. Just comment line app.UseHttpsRedirection(); in Startup.cs file and you are good to go

curl http://localhost:5000/api/values
    

Vscode Ubuntu Debug Breakpoint

Whoila, you have your application up and running in your VSCode IDE on Ubuntu Linux.

Why did we do all of this?

Well, it is a good question and I do not think I have a good answer :). One thing is curiosity. Everyone is talking about cross platform compatibility, so why not give it a try. Secondly, did you try to run Docker on Windows? Not sure about your experience, but mine is terrible. I have my Debian headless VM in VMWare which I start when I want to do something with Docker.

If I was in Linux environment this would be just one more terminal window, not the whole VM.

Anyway, I am still not that comfortable with this setup and will heavily rely on Windows and Visual Studio 2017 for my development, even though I will keep using my VM to try out some Docker stuff, but the important thing is that you know have more options.

You are not stuck with windows and have to use for your every day tasks. Not saying Windows is bad, but it is nice to have an option.

What do you think about it? Tell me in the comments below.

Happy coding

References

Disclaimer

Purpose of the code contained in snippets or available for download in this article is solely for learning and demo purposes. Author will not be held responsible for any failure or damages caused due to any other usage.


About the author

DEJAN STOJANOVIC

Dejan is a passionate Software Architect/Developer. He is highly experienced in .NET programming platform including ASP.NET MVC and WebApi. He likes working on new technologies and exciting challenging projects

CONNECT WITH DEJAN  Loginlinkedin Logintwitter Logingoogleplus Logingoogleplus

JavaScript

read more

SQL/T-SQL

read more

Umbraco CMS

read more

PowerShell

read more

Comments for this article