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.
- sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF
Then add the required repositories to 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
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.
- apt-get install curl
- mkdir .kre
- curl -sSL https:
Then run this update command.
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.
- CERTMGR=/usr/local/bin/certmgr
- sudo $CERTMGR -ssl -m https:
- sudo $CERTMGR -ssl -m https:
- sudo $CERTMGR -ssl -m https:
- mozroots --import --sync
We can observe, using this command, the .NET Framework list available on this machine.
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.
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).
- wget http:
- 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.
To access it, just browse to this URL:
http://localhost:5004.
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 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.
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.
- sudo apt-get install nautilus-actions
Then restart Nautilus and start Nautilus-Actions using the following command.
- nautilus -q
- 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.
- sudo apt-get install nodejs-legacy npm
- sudo npm install -g yo generator-aspnet
If everything went well, the “yo aspnet” command should print this prompt.
Using Yeoman you can ask for multiple file type creations. This list if available using the following command.
For now, we will create a shortcut to create an MVC project.
Then, fill in the corresponding command.
Finally, using the « Preferencies » menu, disable the default submenu creation.
This new shortcut we just created is now available when you click right in the file explorer.
When you need it, just follow the 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.
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 is easier. Just use a variable to store this value and send it as a parameter.
- filename=$(zenity --title="Filename of your new MVC View" --entry) ; yo aspnet:MvcView "$filename"
If we observe the file explorer, the new view was effectively created with the filename we specified in the dialog window.
We will create a script file “createMvcView.sh” wherein we will store the script we just built.
Then we need to add the execution right to this file properties.
As for the “new project” prompt, let's create a new Nautilus-Actions command.
This time, we will specify the bash script's path we just created.
If we invoke the contextual menu of the file explorer, we can see our command.
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 CLIYou 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.
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”.
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”.
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.
- sudo apt-get install xfce4 firefox gnome-terminal
- startxfce4
This installation should take several minutes to finally display a 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.
- # MONO
- sudo apt-get install build-essential
- wget http:
- 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:
- sudo $CERTMGR -ssl -m https:
- sudo $CERTMGR -ssl -m https:
- mozroots --import --sync
- # K*
- sudo apt-get install curl unzip gyp
- mkdir .kre
- curl -sSL https:
- kvm upgrade
- # LIBUV
- wget http:
- 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 that our installation went well, just run this command to list all the versions of the .NET Framework available.
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.
- sudo apt-get install git autoconf libtool automake build-essential mono-devel gettext
- PATH=$PREFIX/bin:$PATH
- git clone https:
- cd mono
- ./autogen.sh --prefix=$PREFIX
- make
- 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.
- LD_LIBRARY_PATH=/etc kpm restore
- LD_LIBRARY_PATH=/etc k kestrel
Run on the Raspberry
To get our MVC sample project, we will use the official GitHub one.
- wget https:
- unzip master.zip
Finally, run the following command using the HelloMvc location.
- LD_LIBRARY_PATH=/etc kpm restore
- LD_LIBRARY_PATH=/etc k kestrel
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.