Click here to Skip to main content
15,886,362 members
Articles / Web Development / IIS
Tip/Trick

ASP.NET 5 and Ubuntu

Rate me:
Please Sign up or sign in to vote.
4.94/5 (19 votes)
22 Feb 2015CPOL8 min read 75.4K   23   21
During this article, we are going to explore what is currently necessary to run an ASP.NET 5 website on Ubuntu.

Introduction

The last ASP.NET version claim itself officially compatible with Mac and Linux in order to open web development of Microsoft sphere to new developer communities. Microsoft impulse, because of his last announcements, a crossplatform effort in the ASP.NET area. During this article, we are going to explore what is currently necessary to run an ASP.NET 5 website on Ubuntu.

The Ubuntu logo

I wrote this article using the ASP.NET 5 beta2, so if you experience some inconveniences have a look at the Mark Rendle's article about what have changed for Linux in beta3.

Mono Project

The Mono Project, powered by Xamarin, is a project which tends to make the .NET Framework available to Microsoft’s foreign platforms. The “light” version of .NET, .NET Core, is not yet officially compatible with Mac and Linux, but it’s a matter of time. For this demonstration, we will use Mono to run our ASP.NET website on Ubuntu.

We will start by installing Mono. First of all, execute this command to add the Mono’s GPG key to the packages manager.

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF

image

Then add the required repositories in the configuration file.

echo "deb http://download.mono-project.com/repo/debian wheezy main" | sudo tee /etc/apt/sources.list.d/mono-xamarin.list

echo "deb http://download.mono-project.com/repo/debian wheezy-apache24-compat main" | sudo tee -a /etc/apt/sources.list.d/mono-xamarin.list

Finally, install Mono.

sudo apt-get install mono-complete

image

This procedure is also detailled here.

K*

You can launch ASP.NET 5 without using IIS nor Kestrel (the IIS Express equivalent for Mac and Linux). To do so, you need to use KVM. This CLI allow us to choose, site by site, the framework version we want to use. To install it, just run this command.

apt-get install curl

mkdir .kre

curl -sSL https://raw.githubusercontent.com/aspnet/Home/master/kvminstall.sh | sh && source ~/.kre/kvm/kvm.sh

image

Then run this update command.

kvm upgrade

image

This procedure can be found here too. We finally need to add Microsoft’s and Nuget’s specific HTTPS certificates to allow the dependencies resolving.

CERTMGR=/usr/local/bin/certmgr

sudo $CERTMGR -ssl -m https://go.microsoft.com

sudo $CERTMGR -ssl -m https://nugetgallery.blob.core.windows.net

sudo $CERTMGR -ssl -m https://nuget.org

mozroots --import --sync

image

We can observe, using this command, the .NET Framework list which are available on this machine.

kvm list

image

Run the website

For this demonstration, we will use the official sample project HelloMvc available on GitHub.

image_thumb[4]

We will start by resolving the project dependencies. Run the following command at the “project.json” file location.

kpm restore

image

This process can take some time since it gather the dependencies of our project recursively.

At the moment, we will need to run our website using Kestrel, but this will not be necessary anymore when the final versions of .NET Core and ASP.NET 5 will be released. To run Kestrel on Linux for now, we need to apply a manual fix which consist in compiling the “libuv” package, thanks to Carolyn Van Slyck (make sure you already installed the “gyp” and “build-essential” packages).

image

wget http://dist.libuv.org/dist/v1.0.0-rc2/libuv-v1.0.0-rc2.tar.gz

tar -xvf libuv-v1.0.0-rc2.tar.gz

cd libuv-v1.0.0-rc2/

./gyp_uv.py -f make -Duv_library=shared_library

make -C out

sudo cp out/Debug/lib.target/libuv.so /usr/lib/libuv.so.1.0.0-rc2

sudo ln -s libuv.so.1.0.0-rc2 /usr/lib/libuv.so.1

We can now run our website using Kestrel.

k kestrel

image

To access it, just browse this URL:  http://localhost:5004 

image

Update the website

In order to show you that it is possible du develop an ASP.NET 5 website without Visual Studio, we are going to modify the home page of our project. Open the “View/Home/index.cshtml” file in the text editor you want.

image

Update the title with “ASP.NET runs Great on Ubuntu !” and the result is available just refreshing your web browser, without compilation of website restart.

image

To go further, if you work on Linux or Mac, I recommand you to use Sublime Text and to install the OmniSharp extension for an enhanced developer experience.

There are tools that can ease this kind of development on Linux. We are going to see that it is possible to configure our own file creation’s shell extensions through Nautilus, the default file manager on Ubuntu.

Nautilus

Firstly, we need to install the “nautilus-actions” package.

sudo apt-get install nautilus-actions

image

Then restart Nautilus and start Nautilus-Actions using the following command.

nautilus -q

nautilus-actions-config-tool

image

Yeoman

Yeoman is a CLI designed to improve web development on new file content initialization, Grunt task management, Bower package management, etc. To install it, after NodeJS and NPM, run the following command.

sudo apt-get install nodejs-legacy npm

sudo npm install -g yo generator-aspnet

image

If everything went well, the “yo aspnet” command should print this prompt.

image

Using Yeoman you can ask for multiple file type creations. This list if available through the following command.

yo aspnet -help

image

For now, we will create a shortcut to create an MVC project.

image

Then, fill the corresponding command.

image

Finally, through the « Preferencies » menu, disable the default submenu creation.

image

This new shortcut we just created is now available when you click right in the file explorer.

image

When you need it, just follow the prompt.

image

For more information on Nautilus-Actions, you can find it here. About Yeoman and ASP.NET, see the original article and the NPM package page. We are now getting further and create shortcuts for files that need user inputs.

We will focus on the MVC View creation. The corresponding command is this.

yo aspnet:MvcView <name>

Zenity

Zenity is a tool that you can use to print dialog windows from a shell script. It should be already installed if you use the last version of Ubuntu. If this is not the case, just run this command.

sudo apt-get update

sudo apt-get install zenity

If you run the next command, you should see an input popup asking for a filename.

zenity --title="Filename of your new MVC View" --entry

To redirect this input to our Yeoman command, nothing easier. Just use a variable to store this value and sent it as a parameter.

filename=$(zenity --title="Filename of your new MVC View" --entry) ; yo aspnet:MvcView "$filename"

image

If we observe the file explorer, the new view was effectively created with the filename we specified in the dialog window.

image

We will create a script file “createMvcView.sh” wherein we will store the script we just built.

image

Then we need to add the execution right to this file properties.

image

As for the “new project” prompt, let’s create a new Nautilus-Actions command.

image

This time, we will specify the bash script’s path we just created.

image

If we invoke the contextual menu of the file explorer, we can see our command.

image

You now have all the skills to add shortcuts to the other file type creations available through this Yeoman generator. To go further, I invite you to explore this GitHub repository which allow you to easily install a complete list of these shortcuts, using the principle we learnt, on Ubuntu.

We will now overview how to use NuGet, the packages manager usually integrated to Visual Studio, using command line.

Nuget CLI

https://www.nuget.org/Content/Logos/nugetlogo.png

You can install the NuGet CLI and authorize HTTPS access using the following command.

sudo apt-get install nuget mono-devels

mozroots --import --sync

You can then list all the packages available using this other one.

nuget list

image

Get Bootstrap

To add Bootstrap to your project from a NuGet package, just run this command at the location we created our application earlier, using “yo aspnet”.

nuget install bootstrap

image

So we are able to use the same packages as in Visual Studio, except for the Mono and ASP.NET 5 compatibility, to develop using the .NET Framework. With .NET Core, this packages list should become more and more cross-platform.

Raspberry Pi

The Raspberry Pi is a microcomputer whose first model appeared in February 2012. Recently, the second version was released with very interesting characteristics for the same price of approximatively 40$: a 900MHz quad-core ARM Cortex-A7 CPU with 1GB RAM.

model-b-plus

Linux ARMv7

After several searches, I found an Ubuntu derived distribution compatible with the Raspberry. To install this image to your SD card, just follow this guide. When it’s done, let’s connect your Raspberry to power supply and plug a screen and a keyboard. The system already have a username / password: “linaro” / “linaro”.

image

We are going to install Xfce, the Mozilla Firefox browser and the Gnome terminal, even if it is not essential, in order to have a more user friendly workspace. To do this, run the following command.

sudo apt-get install xfce4 firefox gnome-terminal

startxfce4

WP_20150217_21_12_15_Pro

This installation should take several minutes to finally display a desktop environment.

unnamed

We will start by creating a new partition on the disk image we just installed (limited to 3Go), or we risk to encounter some space disk issues while we will install all our dependencies. We are going to use Gparted.

image (1)

ASP.NET 5

Since we already saw how to run ASP.NET 5 on Ubuntu, follow this script which you should run on the new partition we just created.

# MONO

sudo apt-get install build-essential

wget http://download.mono-project.com/sources/mono/mono-3.8.0.tar.bz2

tar -xvf mono-3.8.0.tar.bz2

cd mono-3.8.0/

./configure --prefix=/usr/local

make

sudo make install

# CERTIFICATES HTTPS

CERTMGR=/usr/local/bin/certmgr

sudo $CERTMGR -ssl -m https://go.microsoft.com

sudo $CERTMGR -ssl -m https://nugetgallery.blob.core.windows.net

sudo $CERTMGR -ssl -m https://nuget.org

mozroots --import --sync

# K*

sudo apt-get install curl unzip gyp

mkdir .kre

curl -sSL https://raw.githubusercontent.com/aspnet/Home/master/kvminstall.sh | sh && source ~/.kre/kvm/kvm.sh

kvm upgrade

# LIBUV

wget http://dist.libuv.org/dist/v1.0.0-rc2/libuv-v1.0.0-rc2.tar.gz

tar -xvf libuv-v1.0.0-rc2.tar.gz

cd libuv-v1.0.0-rc2/

./gyp_uv.py -f make -Duv_library=shared_library

make -C out

sudo cp out/Debug/lib.target/libuv.so /usr/lib/libuv.so.1.0.0-rc2

sudo ln -s libuv.so.1.0.0-rc2 /usr/lib/libuv.so.1

To check our installation went well, just run this command to list all the versions of the .NET Framework available.

kvm list

image

Mono Version

Exceptions may occurred due to the Mono version which could not be recent enough to run KVM, KPM or Kestrel. The exceptions I encountered are very several and various. If it happens, I recommend you to install the last version of Mono available on GitHub, compiling from the sources.

sudo apt-get install git autoconf libtool automake build-essential mono-devel gettext

PATH=$PREFIX/bin:$PATH

git clone https://github.com/mono/mono.git

cd mono

./autogen.sh --prefix=$PREFIX

make

make install

Once you did that you should prefix all your commands by “LD_LIBRARY_PATH=$$path$$” where “$$path$$” is the last Mono installation directory, like bellow.

LD_LIBRARY_PATH=/etc kpm restore

LD_LIBRARY_PATH=/etc k kestrel

Run on the Raspberry

To get our MVC sample project, we are going to use the official GitHub one.

wget https://github.com/aspnet/Home/archive/master.zip

unzip master.zip

Finally, run the following command through the HelloMvc location.

LD_LIBRARY_PATH=/etc kpm restore

LD_LIBRARY_PATH=/etc k kestrel

unnamed

Conclusion

As you can see, ASP.NET 5 was designed to be run on several platforms, and it’s already available on Alpha 3 version. As this, the Raspberry Pi greatly improved his performances and his compatibility due to his architecture.

Cross-platform is not yet available without Mono, because of the .NET Core Framework development still pending, but we can already see what ASP.NET 5 will look like on that platforms. Microsoft teams, .NET / ASP.NET / Visual Studio, realized a wonderful work in order to optimize and open .NET development to new frontiers.

License

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


Written By
Web Developer
France France
.NET Consultant hugocarnicelli.com, Microsoft MVP 2016.

Comments and Discussions

 
QuestionProduction? Pin
Cvetomir Todorov14-May-15 18:59
Cvetomir Todorov14-May-15 18:59 
AnswerRe: Production? Pin
Hugo Carnicelli14-May-15 22:27
professionalHugo Carnicelli14-May-15 22:27 
GeneralRe: Production? Pin
Cvetomir Todorov14-May-15 22:52
Cvetomir Todorov14-May-15 22:52 
GeneralRe: Production? Pin
Hugo Carnicelli14-May-15 23:04
professionalHugo Carnicelli14-May-15 23:04 
GeneralMy vote of 5 Pin
Humayun Kabir Mamun2-Mar-15 0:57
Humayun Kabir Mamun2-Mar-15 0:57 
SuggestionSmashing! Pin
ArmyBlond24-Feb-15 0:05
ArmyBlond24-Feb-15 0:05 
GeneralRe: Smashing! Pin
Hugo Carnicelli24-Feb-15 0:09
professionalHugo Carnicelli24-Feb-15 0:09 
GeneralRe: Smashing! Pin
ArmyBlond24-Feb-15 0:10
ArmyBlond24-Feb-15 0:10 
GeneralRe: Smashing! Pin
Hugo Carnicelli24-Feb-15 0:14
professionalHugo Carnicelli24-Feb-15 0:14 
GeneralRe: Smashing! Pin
ArmyBlond24-Feb-15 1:01
ArmyBlond24-Feb-15 1:01 
GeneralRe: Smashing! Pin
Hugo Carnicelli24-Feb-15 1:43
professionalHugo Carnicelli24-Feb-15 1:43 
GeneralRe: Smashing! Pin
ArmyBlond24-Feb-15 2:03
ArmyBlond24-Feb-15 2:03 
QuestionGreat article Pin
Stewart Rae (Telstra Health)23-Feb-15 11:56
Stewart Rae (Telstra Health)23-Feb-15 11:56 
Questionimages are not displayed Pin
sulutas81@outlook22-Feb-15 21:26
professionalsulutas81@outlook22-Feb-15 21:26 
AnswerRe: images are not displayed Pin
Hugo Carnicelli22-Feb-15 21:54
professionalHugo Carnicelli22-Feb-15 21:54 
GeneralRe: images are not displayed Pin
Aleksey_K18-Feb-17 18:27
Aleksey_K18-Feb-17 18:27 
Questionhave you consider to post this as a tip? Pin
Nelek22-Feb-15 6:58
protectorNelek22-Feb-15 6:58 
AnswerRe: have you consider to post this as a tip? Pin
Hugo Carnicelli22-Feb-15 7:03
professionalHugo Carnicelli22-Feb-15 7:03 
GeneralRe: have you consider to post this as a tip? Pin
Nelek22-Feb-15 7:08
protectorNelek22-Feb-15 7:08 
GeneralRe: have you consider to post this as a tip? Pin
Hugo Carnicelli22-Feb-15 7:14
professionalHugo Carnicelli22-Feb-15 7:14 

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.