Build .NET 4.5 on Linux in 5 minutes

Although .NET 4.5 solutions (.sln) cannot use the convenient dotnet CLI (from .NET Core 1.1), you still can make it. This article will get you building .NET 4.5 Solutions within 5 minutes and prove that it's not just possible, it's easy!

Objective

This article WILL NOT teach you how to wrap your application in a Docker Container. The procedure for that is different.

We use Docker here purely to have a bash running on top of your Windows without the need of a VM (at least not a VM that you will have to lift), which is therefore a lot faster (and easier, I gotta say).

The sole objective of this article is to teach you How To Build .NET 4.5 Applications in a Linux Environment using Mono and XBuild.

If you are already on a Linux Environment?

In case you are running on a Windows Machine, run through the first 2 steps to get a bash machine where you can test this procedure.

Otherwise, if you are already on MacOS or Linux, you can skip steps (1) and (2) and go straight to the step (3).

Developing on Windows, testing on Linux? Don't do a VM, use a docker container — it's much faster

I covered How Docker Works in the article Docker + Java Microservices: Choosing the Base Image for Java 8/9 Microservices (on Linux and Windows).

1. Installing Docker for Windows

You can get the official latest Docker for Windows. It has good support for both Linux Containers (running on top of Hyper-V) and Windows Containers (which we are not going to use here).

2. Start an Interactive Docker Bash

You can very easily and quickly have a fully operational Linux Bash right inside your Windows Command Prompt by running attached to a ubuntu docker container, functioning seamlessly.

Open your Docker Shell / Docker Command Prompt, installed alongside your Docker for Windows, and run the following command:

docker run -t -i ubuntu /bin/bash

You will see that your prompt is now a bash superuser-styled prompt (with the #). Welcome to your Ubuntu Linux.

And yes, it did only take you 5 seconds to put it up, after the images were downloaded. Docker Magic!

Building the .NET 4.5 Application from your Ubuntu Linux Bash

3. Update your Aptitude Package Cache

The Ubuntu Docker image doesn't come with any cache. So first thing you gotta do is to update it.

apt-get update

4. Install Nuget, Mono-devell and Mono-XBuild

These are the things you will need to build in Linux.

apt-get install -y nuget mono-devel mono-xbuild

5. Update your Nuget, you will need the most recent version

Some packages just can't be downloaded by older version of nuget. Here, for example, I had to update mine from 2.8 to 3.5. You do that by running the following command:

nuget update -self

6. Clone your git repository

The command bellow clones a sample repository that will be used for the purpose of this exercise:

apt-get install -y git
mkdir /src
cd /src
git clone https://github.com/hudsonmendes/benchmark_dotnet_mono_build_webapi.git
cd benchmark_dotnet_mono_build_webapi

7. Restore your Nuget Packages

That will be very important for the build to succeed, in most of cases.

nuget restore

7. Build

It's SHOW TIME!

xbuild AndrewJenkins.sln

Run the command below to notice that you now have a .exe built in the your app's bin folder.

ls ApiPeople/bin/Debug/*.exe

Once you got that, wrapping it up in a Docker Container should be a no-brainer.

Hope you liked it!

Like what you read? Give Hudson Mendes a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.