.NET Core is a cross-platform open-source web development framework. It is currently supported on Windows OS, Linux (Cent OS 7.1, Debian 8.2, Red Hat Enterprise Linux 7.2, Ubuntu 14.04) and OS X. The Microsoft .NET Core installation page mentions that “You just need a shell, a text editor and 10 minutes of your time” to get started. In this article we are going to check .Net Core’s claim to be cross-platform by installing and creating our first web application on Ubuntu.
You need to be on Ubuntu version 14.04 to install .NET Core. You can check your Ubuntu version by typing the following command in the terminal.
Adding the new apt-get feed
apt-get is a powerful command-line tool which works with Ubuntu’s Advanced Packaging Tool (APT) to install and upgrade software packages. We first need to set up the apt-get feed that hosts the .NET Core package by using the following command.
sudo sh -c 'echo "deb [arch=amd64] https://apt-mo.trafficmanager.net/repos/dotnet/ trusty main" > /etc/apt/sources.list.d/dotnetdev.list' sudo apt-key adv --keyserver apt-mo.trafficmanager.net --recv-keys 417A0893 sudo apt-get update
Installing .NET Core SDK
If you already have a previous version of .NET installed, you need to run this script first and then install the .NET Core package using the following command. Follow the instructions to start installation.
sudo apt-get install dotnet-dev-1.0.0-preview2-0031
This will install the .NET Core SDK and all its dependencies.
Now you can check the installation by typing the command “dotnet” on the terminal. If you get an error like the one in the image below, it means that the right version was not installed and you need to search for the latest version available and then install it. In my case, the version mentioned on the installation site(1.0.0-preview1-002702) was outdated (as rapid development is in process and new versions keep on rolling this might be happening quite often).
You have your .NET Core SDK installed now and you are ready to start writing your code. That was really easy. So, lets go ahead and create our first application that will print Hello World for us.
Hello World in .NET Core
First we will create a folder for our application named hwapp using the following code in the terminal.
mkdir hwapp cd hwapp
Now we need to initialize our application using the following command.
The dotnet new command creates a new .net project with some sample code. Right now this creates two files, Program.cs which contains the code to print Hello World and a project.json file which is the configuration file for your project. By default the project is created using C# as the programming language but you can choose a different language by adding a lang attribute to the command like this.
dotnet new –lang f#
This will create the project in f#.
Now the application is ready to be compiled and executed. This can be done by the following commands.
dotnet restore dotnet run
The restore command uses NuGet to get all the tools and dependencies that are required by the application. The project specific dependencies can be added to project.json file. The restore command will use this file to get all the project specific dependencies.
The run command is the one which will compile, generate the output files and launch your application. This command will be creating the bin and obj folders (if they do not already exist) in your project folder.
Voila! We have our first Hello World .NET Core application up and running. So far so good, but this is not enough, we need something more to start developing our web application. In our next step, we will be getting some ASP .NET project templates to start with the web development.
Yeoman is a scaffolding tool for web application project templates. There is a plethora of generators available for web apps on Yeoman. The command line tool ‘yo’ can be used to install the templates we want. Before installing any template we need to install Node.js, npm (Node Package Manager) and yo.
We will use the following commands to install Node.js and npm and then create a symbolic link for node so that it can be used by other programns.
sudo apt-get install nodejs sudo apt-get install npm sudo ln -s /usr/bin/nodejs /usr/bin/node
Once you have Node.js and npm installed, the following command can be used to install yo, bower, grunt and gulp.
sudo npm install -g yo bower grunt-cli gulp
You can check your Yeoman installation by typing the yo command in the terminal.
After that we need to install the ASP.NET generator.
sudo npm install -g generator-aspnet
Now, you are ready to use Yeoman to create your ASP.NET application. We will first create a directory myaspdotnetapp and then use the yo command to install the project template.
You need to follow the steps in the wizard which includes choosing the type of application you want to create. We are selecting “Web Application Basic [without Membership and Authorization]”. Next, we select Bootstrap (3.3.6) as our UI framework. Lastly, we provide the application name as “MyFirstASPCoreApp” and hit enter. The project is created and you can check the folder structure to verify the installation.
Now we use our
dotnet restore and
dotnet run commands to compile and run the project. After the application is started we can use the url provided by the CLI to check our web application.
Our development environment is ready and we have launched our first web application. Wait! But how do we make changes? An editor is the only thing missing in the whole picture. So, lets see what options do we have here.
You can choose any editor which you are comfortable with including SublimeText, Atom, etc. But here we are going to take a look into Microsoft’s Visual Studio Code for editing our ASP.NET application.
Introducing Visual Studio Code
You can download VS Code from here and the easiest way to launch the application is by running the file named “code” in the downloaded folder.
Use the “Open Folder” button in the explorer bar to select and load the project folder.
Now you will see all the project files and can start writing some code. There is a bonus for you here. By adding the extension for C#, you get the power of IntelliSense in your hand. To install the extension press Ctrl+P (Quick open) and type ext install with a space at the end to see the list of extensions. Choose what you like and you are ready to go.
To learn more about Visual Studio Code click here.
So, we are finally ready with Ubuntu as our development environment for developing .Net Core applications. .Net core does what it says. The set up process was easy and in around 20 minutes you are ready with a full fledged development environment for .NET Core. Go ahead and start playing with the awesome ASP.NET Core and let me know what you think.
Hope this article helps as the stepping stone in .NET world. Feel free to leave your comments below with questions, suggestions or anything you like.