ASP.Net 5 and Ubuntu

Introduction

The last ASP.NET version claims to be officially compatible with Mac and Linux for open web development of Microsoft software for new developer communities. The Microsoft impulse is the result of these last announcements announcing cross-platform efforts in the ASP.NET area. During this article, we will explore what is currently necessary to run an ASP.NET 5 website on Ubuntu.

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 that 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.
  1. sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF  
Mono Project

Then add the required repositories to the configuration file.
  1. echo "deb http://download.mono-project.com/repo/debian wheezy main" | sudo tee /etc/apt/sources.list.d/mono-xamarin.list    
  2. 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.
  1. sudo apt-get install mono-complete   
packages manager

This procedure is also detailed 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 allows us to choose, site by site, the framework version we want to use. To install it, just run the following command.
  1. apt-get install curl    
  2. mkdir .kre    
  3. curl -sSL https://raw.githubusercontent.com/aspnet/Home/master/kvminstall.sh | sh && source ~/.kre/kvm/kvm.sh   
kvminstall

Then run this update command.
  1. kvm upgrade  
run this update

This procedure can be found here too. We finally need to add Microsoft's and Nuget's specific HTTPS certificates to allow the dependency resolution.
  1. CERTMGR=/usr/local/bin/certmgr    
  2. sudo $CERTMGR -ssl -m https://go.microsoft.com    
  3. sudo $CERTMGR -ssl -m https://nugetgallery.blob.core.windows.net    
  4. sudo $CERTMGR -ssl -m https://nuget.org    
  5. mozroots --import --sync     
allow the dependencies

We can observe, using this command, the .NET Framework list available on this machine.
  1. kvm list  
kvm list

Run the website

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



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

This process can take some time since it gathers 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 the .NET Core and ASP.NET 5 will be released. To run Kestrel on Linux for now, we need to apply a manual fix that consists of compiling the “libuv” package, thanks to Carolyn Van Slyck (ensure you have already installed the “gyp” and “build-essential” packages).

Slyck
  1. wget http://dist.libuv.org/dist/v1.0.0-rc2/libuv-v1.0.0-rc2.tar.gz    
  2. tar -xvf libuv-v1.0.0-rc2.tar.gz    
  3. cd libuv-v1.0.0-rc2/    
  4. ./gyp_uv.py -f make -Duv_library=shared_library    
  5. make -C out    
  6. sudo cp out/Debug/lib.target/libuv.so /usr/lib/libuv.so.1.0.0-rc2    
  7. sudo ln -s libuv.so.1.0.0-rc2 /usr/lib/libuv.so.1    
We can now run our website using Kestrel.
  1. k kestrel   
k kestrel

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

To access it

Update the website

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

Update the website

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

compilation of website

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 will see that it is possible to configure our own file creation's shell extensions using Nautilus, the default file manager on Ubuntu.

Nautilus

First, we need to install the “nautilus-actions” package.
  1. sudo apt-get install nautilus-actions  
install nautilus

Then restart Nautilus and start Nautilus-Actions using the following command.
  1. nautilus -q    
  2. nautilus-actions-config-tool  
Yeoman

Yeoman is a CLI designed to improve web development on new file content initialization, Grunt task management, Bower package management and so on. To install it, after NodeJS and NPM, run the following command.
  1. sudo apt-get install nodejs-legacy npm    
  2. sudo npm install -g yo generator-aspnet    
Yeoman

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

yo aspnet

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

command
  1. yo aspnet -help    
For now, we will create a shortcut to create an MVC project.

create an MVC project

Then, fill in the corresponding command.

fill the command

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

Preferencies

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

explorer

When you need it, just follow the prompt.

prompt

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.
  1. 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.
  1. sudo apt-get update    
  2. sudo apt-get install zenity   
If you run the next command, you should see an input popup asking for a filename.
  1. zenity --title="Filename of your new MVC View" --entry     
To redirect this input to our Yeoman command, nothing is easier. Just use a variable to store this value and send it as a parameter.
  1. filename=$(zenity --title="Filename of your new MVC View" --entry) ; yo aspnet:MvcView "$filename"   
Zenity

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

dialog window

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

createMvcView

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

file properties

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

new project

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

script

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

contextual menu

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

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

Nuget CLI

You can install the NuGet CLI and authorize HTTPS access using the following command.
  1. sudo apt-get install nuget mono-devels    
  2. mozroots --import --sync    
You can then list all the packages available using this other one.
  1. nuget list   
NuGet CLI

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”.
  1. nuget install bootstrap   
nuget install bootstrap

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 the .NET Core, this packages list should become increasingly 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.



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 a power supply and plug in a screen and a keyboard. The system already has the username / word: “linaro” / “linaro”.

Ubuntu

We will 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.
  1. sudo apt-get install xfce4 firefox gnome-terminal    
  2. startxfce4   


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

desktop environment

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 will use Gparted.



ASP.NET 5

Since we already saw how to run ASP.NET 5 on Ubuntu, use the following script to run on the new partition we just created.
  1. # MONO    
  2. sudo apt-get install build-essential    
  3. wget http://download.mono-project.com/sources/mono/mono-3.8.0.tar.bz2    
  4. tar -xvf mono-3.8.0.tar.bz2    
  5. cd mono-3.8.0/    
  6. ./configure --prefix=/usr/local    
  7. make    
  8. sudo make install    
  9. # CERTIFICATES HTTPS    
  10. CERTMGR=/usr/local/bin/certmgr    
  11. sudo $CERTMGR -ssl -m https://go.microsoft.com    
  12. sudo $CERTMGR -ssl -m https://nugetgallery.blob.core.windows.net    
  13. sudo $CERTMGR -ssl -m https://nuget.org    
  14. mozroots --import --sync    
  15. # K*    
  16. sudo apt-get install curl unzip gyp    
  17. mkdir .kre    
  18. curl -sSL https://raw.githubusercontent.com/aspnet/Home/master/kvminstall.sh | sh && source ~/.kre/kvm/kvm.sh    
  19. kvm upgrade    
  20. # LIBUV    
  21. wget http://dist.libuv.org/dist/v1.0.0-rc2/libuv-v1.0.0-rc2.tar.gz    
  22. tar -xvf libuv-v1.0.0-rc2.tar.gz    
  23. cd libuv-v1.0.0-rc2/    
  24. ./gyp_uv.py -f make -Duv_library=shared_library    
  25. make -C out    
  26. sudo cp out/Debug/lib.target/libuv.so /usr/lib/libuv.so.1.0.0-rc2    
  27. sudo ln -s libuv.so.1.0.0-rc2 /usr/lib/libuv.so.1   
To check that our installation went well, just run this command to list all the versions of the .NET Framework available.
  1. kvm list   
command to list

Mono Version

Exceptions may have occurred due to the Mono version that 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.
  1. sudo apt-get install git autoconf libtool automake build-essential mono-devel gettext    
  2. PATH=$PREFIX/bin:$PATH    
  3. git clone https://github.com/mono/mono.git    
  4. cd mono    
  5. ./autogen.sh --prefix=$PREFIX    
  6. make    
  7. make install  
Once you have done that you should prefix all your commands by “LD_LIBRARY_PATH=$$path$$” where “$$path$$” is the last Mono installation directory, like the following.
  1. LD_LIBRARY_PATH=/etc kpm restore    
  2. LD_LIBRARY_PATH=/etc k kestrel    
Run on the Raspberry

To get our MVC sample project, we will use the official GitHub one.
  1. wget https://github.com/aspnet/Home/archive/master.zip    
  2. unzip master.zip    
Finally, run the following command using the HelloMvc location.
  1. LD_LIBRARY_PATH=/etc kpm restore    
  2. LD_LIBRARY_PATH=/etc k kestrel   
Run on the Raspberry

Conclusion

As you can see, ASP.NET 5 was designed to be run on several platforms and it's already available on the Alpha 3 version. With this, the Raspberry Pi has greatly improved its performances and its compatibility due to its 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 platform. Microsoft teams, .NET / ASP.NET / Visual Studio, realized a wonderful work in order to optimize and open .NET development to new frontiers.


Similar Articles