Develop and Deploy ASP.NET 5 Apps on Linux

NOTE: This post is part 2 of a series on developing and deploying cross-platform web apps with ASP.NET 5:

  1. Develop and Deploy ASP.NET 5 Apps on Mac OS X
  2. Develop and Deploy ASP.NET 5 Apps on Linux (this post)
  3. Deploy ASP.NET 5 Apps to Docker on Linux
  4. Deploy ASP.NET 5 Apps to Docker on Azure

Download instructions and code for this post here: https://github.com/tonysneed/VsCode-AspNet5-Linux.

It is now possible to develop and deploy an ASP.NET application on Linux.  In case you haven’t heard, Microsoft’s CEO, Satya Nadella, has proclaimed, “Microsoft loves Linux.”

Microsoft-Hearts-Linux

The reason is simple: Linux is vital to Microsoft’s cloud service, Azure.  But more important, in embracing cross-platform initiatives, such as Core CLR, there is an acknowledgement that we no longer live in a world with a single dominant platform, and that developers not only need to write code that runs on multiple platforms, but that they should be comfortable writing apps with an IDE that will run on multiple platforms. For most of us, that IDE will be Visual Studio Code.

In this blog post I’ll show you how to set up a Linux virtual machine to run VS Code, so that you can both develop and deploy ASP.NET 5 applications.  If you’re on a Mac with Parallels, creating a virtual machine running Ubuntu Linux is just a few clicks away.

parallels-linux-vm

Once you’ve stood up your shiny new Linux VM, follow these instructions to install VS Code.  I found it convenient to create a VSCode folder under Home and extract the contents of VSCode-linux-x64.zip there.  Then you can create a link that will launch VS Code from the Terminal, so that you can type code . to start editing files in VS Code at that location.

sudo ln -s /home/parallels/VSCode/Code /usr/local/bin/code

Next you’ll need to follow these instructions to install ASP.NET 5 on Linux.  The first step is to install Mono.

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF
echo "deb http://download.mono-project.com/repo/debian wheezy main" | sudo tee /etc/apt/sources.list.d/mono-xamarin.list
sudo apt-get update
sudo apt-get install Mono-Complete

Second, you’ll need to install libuv, which is used by Kestrel for hosting ASP.NET 5 apps on Linux.

sudo apt-get install automake libtool curl
curl -sSL https://github.com/libuv/libuv/archive/v1.4.2.tar.gz | sudo tar zxfv - -C /usr/local/src
cd /usr/local/src/libuv-1.4.2
sudo sh autogen.sh
sudo ./configure
sudo make
sudo make install
sudo rm -rf /usr/local/src/libuv-1.4.2 && cd ~/
sudo ldconfig

Lastly, you’ll need to install the DotNet Version Manager, which is used to select and configure versions of the .NET runtime for hosting ASP.NET 5 apps.

curl -sSL https://raw.githubusercontent.com/aspnet/Home/dev/dnvminstall.sh | DNX_BRANCH=dev sh && source ~/.dnx/dnvm/dnvm.s
source /home/parallels/.dnx/dnvm/dnvm.sh
dnvm
dnvm upgrade

Entering dnvm will then bring up the version manager, which allows you to select and configure different versions of the ASP.NET 5 runtime.

dnvm-linux

To see which versions are installed, enter: dnvm list.

dnvm-list-linux

Next, you’ll create a directory and fire up VS Code to create your first ASP.NET 5 app.  We’ll start with a Hello World console app! The quickest approach is to grab two files from the ConsoleApp folder for the AspNet Samples Repo: project.json and program.cs.

Here is project.json for the console app, which lists the dependencies you’ll bring in.

{
    "dependencies": {

    },
    "commands": {
        "ConsoleApp": "ConsoleApp"
    },
    "frameworks": {
        "dnx451": { },
        "dnxcore50": {
            "dependencies": {
                "System.Console": "4.0.0-beta-*"
            }
        }
    }
}

And here is program.cs, which simply prints “Hello World” to the console.

using System;

public class Program
{
    public static void Main()
    {
        Console.WriteLine("Hello World");
    }
}

Editing program.cs in Visual Studio Code looks like this:

vscode-console-linux

To launch the app, open a Terminal at the ConsoleApp directory, then restore the dependencies and execute the program.

dnu restore
dnx . run

You should see “Hello World” printed to the Terminal window.

linux-console-helloworld

Console apps are well-suited for running tasks on a server, but more often you’ll use ASP.NET 5 to run web apps. The AspNet repo on GitHub has a HelloWeb sample, where you can grab two files: project.json and startup.cs.

Here is the project.json file for the web app.  Notice the “kestrel” command for starting an HTTP listener on Mac OS X and Linux.

{
    "version": "1.0.0-*",
    "dependencies": {
        "Kestrel": "1.0.0-*",
        "Microsoft.AspNet.Diagnostics": "1.0.0-*",
    },
    "commands": {
        "kestrel": "Microsoft.AspNet.Hosting --server Kestrel --server.urls http://localhost:5004"
    },
    "frameworks": {
        "dnx451": { },
        "dnxcore50": { }
    }
}

Here is the startup.cs file, which configures the pipeline with an endpoint for displaying a welcome page.

using Microsoft.AspNet.Builder;

namespace HelloWeb
{
    public class Startup
    {
        public void Configure(IApplicationBuilder app)
        {
            app.UseWelcomePage();
        }
    }
}

To start the web server, restore dependencies and then enter the kestrel command.  VS Code also allows you to execute commands from the command palette, but this doesn’t yet work on Linux.

dnu restore
dnx . kestrel

Then open a browser and enter the address: http://localhost:5004. You’ll see a message in the Terminal stating the web server has started. To end it, press Enter.

linux-web-welcome

Aside from running the console and web Hello World samples, you’re going to want to develop your own applications based on templates you’d find in Visual Studio on Windows.  To get a similar experience, you can install Yeoman, which scaffolds various kinds of ASP.NET 5 apps, such as MVC 6 or Web API (which is technically now part of MVC 6). To install Yeoman you’ll need the Node Package Manager, which you can download and install using the apt-get command.

sudo apt-get install nodejs-legacy npm

Once you have Node installed, you can use it to install Yeoman, the asp.net generator, grunt and bower – all in one fell swoop.

sudo npm install -g yo grunt-cli generator-aspnet bower

Having installed Yeoman, you can then navigate to a directory in Terminal where you’d like to create your new app.  Then execute:

yo aspnet

This brings up a number of choices.  For example, you can select Web API Application, which then scaffolds the standard Web API app with a ValuesController. If you run dnu restore and dnx . kestrel, as you did with the sample web app, you can browse to the following URL to get back JSON values: http://localhost:5001/api/values.

And that is how you can both develop and deploy ASP.NET 5 apps on Linux.  For the next part of this series, I’ll show you how to deploy an ASP.NET 5 app to a Docker container on Linux.

About Tony Sneed

Married with three children.
This entry was posted in Technical and tagged , , , . Bookmark the permalink.

35 Responses to Develop and Deploy ASP.NET 5 Apps on Linux

  1. Chris says:

    “sudo sh autogen.sha” should be “sudo sh autogen.sh”

  2. Alain Sergei Khumalov says:

    Have you test to deploy it on online server? This is good news if asp.net can run in Linux. It will be cheaper to host asp.net. At this time, I’m still using windows hosting at hostforlife.eu. But, if it is running perfectly on Linux server, I will test it too. 🙂

    • Tony Sneed says:

      Yes! ASP.NET 5 runs perfectly fine on Linux and in Docker containers in the Cloud. I have it hosted in a Linux VM on Azure.

      • Alain Sergei Khumalov says:

        Wow…. Great news….. So, how about in Linux shared hosting? Have you test it? Or it only works on VPS or cloud?

        Thank you

      • Tony Sneed says:

        It works on Linux Ubuntu 14.04. I’ve run the app both on Linux in a local VM as well as an Azure VM, both in a Docker container. So there’s no reason it would not work with shared hosting, VPS or cloud.

  3. Dale says:

    “Second, you’ll need to install libuv, which is used by Kestrel for hosting ASP.NET 5 apps on Linux.”

    So what’s Kestrel? If you’re talking about http://twitter.github.io/kestrel/, that hasn’t been active since 2012. I’ve seen references to Kestrel on a few Docker pages but none I read have any description of what it is or what it’s for.

  4. srikanth says:

    You are running this with Mono. Can this done with coreclr x64 build?

    • Tony Sneed says:

      The current ASPNET5 bits depend on Mono for cross-plat, but CoreCLR will replace that requirement probably before we get to RC. So stay tuned. Currently you can include a dependency on CoreCLR in your project.json file, so there won’t be a need to change your code.

  5. Ravi says:

    Hi,
    This article was helpful to me in setting up my first .Net apps in Ubuntu. Thanks!
    Now, it would be great to know of the limitations of what we can/can’t do with .Net apps in terms of cross platform. Is there any limitations on the type of the applications that we can deploy in Linux? I mean, would we be able to deploy a Web Service[SOAP/REST] or Windows Server App or a traditional Asp.Net App?
    Regards,
    Ravi

    • Tony Sneed says:

      So on Linux you’re presently limited to using the Kestrel web server. However, this will be production-ready by the time we get to RC. IIS and Windows Services are specific to Windows, and there is not yet support for Apache for hosting ASPNET5 apps.

      You can deploy REST web services with ASPNET5. SOAP is implemented by WCF, which is not supported by ASPNET5. So you’d need to use Mono for that, but I would recommend against SOAP/WCF going forward, as it is now considered deprecated. Likewise traditional ASP.NET apps, for example MVC5 or Web Forms, is only supported on Windows with the full .NET 4.x framework.

  6. Pingback: Making the jump to Linux | Cyte Design

  7. This looks like exactly what I needed. However, I have one question: Is there a way to develop asp.net web apps from windows, and publish them from windows into the linux host? Will this be just like typing the IP/Hostname and user credentials? Thanks!

    • Tony Sneed says:

      There are a few ways to publish an ASPNET5 app to a Linux VM, and it doesn’t matter where you develop the app. You can author it on Windows using either Visual Studio Code or full Visual Studio 2015. One way is to copy the source code files to the Linux machine, where you have installed DNX, as shown in this blog post. Another way is to use Docker, which can be done either on a local Linux VM or on a cloud service such as Azure. This my other blog post for instructions on deploying to Docker on a Linux VM.

  8. Aaron says:

    Hi Tony, great guide however I’m having an issue. I managed to get the basic page to display etc, sucessfully installed asp 5′ etc although when it comes to actually loading up a full project created from Yo it just seems to hang on loading the page, no errors when restoring or running…

    Would you have any advice?

  9. Aaron says:

    I was already using those which seemed to work fine. That’s when I wanted to get a project setup with MVC however as stated I seem to be running into issues where everything seems to restore and run fine but I just cannot actually load the page.

    Also, bear in mind this is all running locally so there should not be any port issues. Any help is greatly appreciated!

    Cheers

    • Tony Sneed says:

      Just a guess, but check the order of your statements in Startup.Configure. The very last statement needs to be the one which uses MVC. Also make sure your routes are set up properly. For further support on this, I suggest posting a question to Stack Overflow, where you can paste in code and get responses from others as well.

  10. Firstly, thank you for this tutorial..!!!!

    Can we host our aspnet 5 webapp on ubuntu, So that we can access from outside like hosting in IIS.?
    If yes, can you please explain me i am beginner for both aspnet 5 and visual code in ubuntu.

  11. Ignas says:

    Now it’s not working. Mono latest version isn’t compatible with this example.

    • Tony Sneed says:

      The examples from this post are indeed outdated. Instead of Mono, you should be using .NET Core. You can find current examples on the ASPNET GitHub repo, at the home repository in the samples folder. Just download or clone the home repo to get the latest samples, which include correct Docker files. I hope this helps.

  12. bluEEil says:

    Thanks alot for this post!
    I am puzzled though about how at the present I can develop and web api app on windows and then deploy it on an offline linux pc (This linux pc can be connected to www and I can only put files on it using an usb flash drive)

    Any ideas on how I can achieve that?

  13. Rama says:

    Can we develop asp.net application using VS2015 on Windows environment ,build a package for Linux and deploy it on linux.?

  14. Monica says:

    Hi Tony

    Thanks for the post. I have few queries. Mono is not supporting all features of .net like WPF, WWF, async. Also WCF is not fully supported. For SilverLight, moonlight was introduced which is not supported anymore as i have heard (please correct if i am wrong anywhere so far).
    So if i talk from general prospective, can we rely on ASP.Net core to provide full compatibility in Linux. For projects with new version and for existing project for old versions? RC2 is already out and it’s supporting ASP.Net. I am not familiar with all the details of RC2 but my worry is can i run all my existing .net projects on linux using core (providing necessarily changes, as it requires project.json file for compilation).

    • Tony Sneed says:

      .NET Core is a subset of the full .NET Framework, and ASP.NET Core is an entirely new web platform. So really your existing projects won’t run on .NET Core without being re-written. That’s one reason it is a v 1.0 project (it was released at the end of June). So it should be considered more for new development rather than porting legacy apps, which requires a great deal more effort.

      • Monica says:

        Thanks for input. It cleared the cloud but I landed myself in another question. I am sure you must be aware about NuGet package for MSBuild which carries different versions for windows and Unix. Can I use Unix’s MSBuild for project compilation after deploying project with NuGet Packages in unix-enabled environment ? Is that fully compatible (Unix’s MSBuild in Nuget package to compile .net projects)?

        Please suggest

      • Tony Sneed says:

        I’m not able to address this question presently. But I suggest you post it to Stack Overflow, where you’ll likely receive an answer.

  15. Vikash says:

    Can i host an entire asp.net mvc 5 web application which has webapi inbuild?
    can i host multiple websites?
    can we control from any control panel?
    can we install SSL for the website?

  16. Samuel Waithaka says:

    Hello guys.. Am trying to install the .NET Version Manager on my ubuntu platform using the above commands but i keep getting errors [
    samie@samie-Satellite-L755:~$ curl -sSL https://raw.githubusercontent.com/aspnet/Home/dev/dnvminstall.sh | DNX_BRANCH=dev sh && source ~/.dnx/dnvm/dnvm.s
    sh: 1: 404:: not found

    samie@samie-Satellite-L755:~$ source /home/parallels/.dnx/dnvm/dnvm.sh
    bash: /home/parallels/.dnx/dnvm/dnvm.sh: No such file or directory

    samie@samie-Satellite-L755:~$ dnvm
    No command ‘dnvm’ found, did you mean:
    Command ‘djvm’ from package ‘djvulibre-bin’ (universe)
    dnvm: command not found

    samie@samie-Satellite-L755:~$ dnvm upgrade
    No command ‘dnvm’ found, did you mean:
    Command ‘djvm’ from package ‘djvulibre-bin’ (universe)
    dnvm: command not found

    ]
    I`ll appreciate for any support

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s