Getting started with .NET on Linux

This post will describe how to install .NET (dnx) on a headless Linux virtual machine. First of all you might be wondering why running without a GUI. The reason is that running with a GUI can often be a bit of a hassle – with today’s high-resolution displays there’s more often than not, in my experience, difficult to get the virtual machine to display a descent resolution. Also the graphics is often lagging (again in my experience).
So instead we can run the VM in headless mode and since we can share directories and ports with the host machine we don’t really need a GUI on the VM.

There are several ways to run Linux in a virtual environment however the easiest way in my opinion is to use VirtualBox which is a virtual machines provider and Vagrant to configure and start up the virtual machine. You can install them from their web sites or by using Chocolatey (which you should) by typing choco install virtualbox and then choco install vagrant in the command prompt.

After VirtualBox and Vagrant installed, create a folder and name it e.g. ubuntu, cd into it and type vagrant init in the command prompt. This creates a vagrantfile in which we specify what machine we want, which ports that should be open to the host, provisioning details etc. To use an Ubuntu image replace base with e.g. ubuntu/trusty64 on the line that says = “base”.
When the vagrantfile is complete use vagrant up to start the machine.

When the machine has started we can run vagrant ssh to connect to the machine. Traditionally you would use PuTTY as the SSH client on Windows, however I feel its quite cumbersome to use it. Instead I use the Git Bash CLI that comes with Git for windows which has an SSH client built in.

Now that we’re connected to the Linux machine and have the terminal at our fingertips we shall create our first cross-platform .NET application. To do so we first need to get some new and shiny tools: DNVM (Dot NET Version Manager) for handling different versions of .NET runtimes, DNX (Dot NET Execution Runtime) for running the apps and DNU (DNX Utility) for building and managing project packages. At the time of writing .NET needs a little help from some friends to get by on Linux, more specifically, Mono and libuv must be installed. For your convenience there’s a pre-baked vagrantfile here that will install both of the above on vagrant up – it should just work™. Note, to restart the VM after the vagrantfile changed use the vagrant reload command.

The vagrantfile also made sure to install the dnvm so now it’s time to write our first app which of course will be a simple Hello World console application like this:

We also need a project.json to specify dependencies, commands etc:

If you’re not used to writing code in the terminal using e.g. Vim create the above files, Program.cs and project.json, in Windows and put them in the ubuntu folder which we created above. This folder is shared with the VM’s /vagrant directory. After the files are in the ubuntu folder, just cd into the vagrant directory on the VM: cd /vagrant. Now we should be able to run our program with these commands:
dnvm upgrade which gets dnu and dnx.
dnu restore which gets the necessary dependencies as specified in the project.json file.
dnx . run which instructs the runtime to execute the application using the project file in the current folder (as denoted by the dot).
The good old greeting to a brave new world filled with possibilities should now appear in the terminal!
In the next post I’ll demonstrate a little more useful application than this!

This Post Has One Comment

Leave a Reply

Close Menu