A Quick Startup Using .NET Core On Linux

I know you may be thinking … This post is another rhetoric post by"that guy"  and you are right .NET Core is another product that I like, the first one of its kind. It was last year when .NET Core started and Microsoft said they are going to release the source code as a part of their open source environment. By following the open source project environment and ethics, Microsoft has been working very hard in bringing global developers towards their environments, platforms, and services. For example, .NET framework works on Windows, C# language is used to build Windows Store applications, C# is also the primary language in Microsoft’s Web Application development framework, ASP.NET and much more. The online Cloud Service of Microsoft is also programmed in C#; primarily. These things are interrelated to each other. Thus, when Microsoft brings all of this as an open source project, things start to get a lot better!

Everyone knows about the background of .NET Core and if you don’t know, I recommend that you read the blog post on Microsoft, Introducing .NET Core. The(ir) basic idea was to provide a framework that would work with any platform, any application type and any framework to be targeted.

Introduction and Background

In this post, I will help you learn how to get started with .NET Core and how to install it on a Linux machine and I will also give my views as to what is the need to install .NET Core on a Linux machine instead of Windows machine, I will then walk you through different steps of .NET Core programming and how you can use a terminal-based environment to perform multiple tasks. 

I am sure that you have heard of .NET Core and other cool stuff that Microsoft has been releasing these years. Out of all of the services, the ones that I like are:

  1. C# 6

    a. In my own opinion, I think the language looks cleaner now. These sugar-coated features make the language easier to write too. If you haven’t yet read it, please read my previous blog post, Experimenting with C# 6’s new features.

  2. .NET Core

    a. Of course, who wouldn’t love to use .NET on the other platforms?

  3. Xamarin acquisition

    a. I’m going to try this one out tonight. Fingers crossed.

  4. The rest are all just the “usual” stuff around.

In this post, I am going to talk about .NET Core on Linux because I have already talked about the C# stuff.

.NET Core is a cross-platform programming framework by Microsoft
   Figure 1: .NET Core is a cross-platform programming framework by Microsoft


Why Linux for .NET Core?

Before I dive any deeper, I will provide a few of my considerations for using .NET Core on Linux and not on Windows (yet!). You don’t have to take them seriously or to always consider them, they are just my views and my opinions and you are free to have your own.

.NET Core is not yet complete

It would take a while before .NET releases a Public stable version. Until then, using this bleeding edge technology on your own machines won’t be a good idea and someday sooner you will consider removing the binaries. In such cases, it is always better to use it in the virtual machine somewhere. I have set up a few Linux (Ubuntu-based) virtual machines for my development purposes and I recommend that you go the same way.

  1. Install VirtualBox (or any other virtualization software that you prefer; I like VirtualBox for its simplicity).

  2. Set up an Ubuntu environment.

    a. Remember to use Ubuntu 14.04. Later ones are not yet supported.

  3. Install the .NET Core on that machine.

If something goes wrong then you can easily revert back where you want to. If the code plays dirty, you don’t have to worry about your data or your machine at all.

Everything is command-line

Windows OS is not your OS, if you like command-line interfaces. I am waiting for the BASH language to be introduced in Windows as in Ubuntu, until then, I am not able to use anything that requires a command-line interface to be used on my Windows environment. In Linux, however, almost everything has a command-line interface and since the .NET Core is a command-based program by Microsoft  I have enjoyed using it on Linux as compared to Windows.

Besides, on command-line interfaces, creating, building and running a .NET Core project is as simple as 1… 2… 3. You’ll see how, in the sections below.

I don’t use Mac

Besides these points, the only valid point left is then why shouldn’t we use Mac OS for .NET Core is because I don’t use it. You are free to use Mac OS for .NET Core development too. .NET Core supports it but I don’t support that development environment.

Installation of .NET Core


Although it is intended that soon, the command will be as simple as:

  1. $ sudo apt-get install dotnet  
Same command is used on Mac OS X and other operating systems; other than Ubuntu and Debian derivative but until the .NET Core is in the development process, this is not going to happen. Until then, there are other steps that you can perform to install the .NET Core on your own machine. I’d like to skip this part and let Microsoft give you the steps to install the framework.
After this step, remember to make sure that the platform has been installed successfully. In almost any environment, you can run the following command to get the success message.
  1. > dotnet --help  
If .NET is installed, it would simply show the version and the other help materials on the screen. Otherwise, you may want to make sure that the procedure did not incur any problems during the installation of the packages. Before I get started, I want to show you the help material provided with “dotnet” command,
  1. afzaal@afzaal-VirtualBox:~/Projects/Sample$ dotnet --help  
  2. .NET Command Line Tools (1.0.0-preview1-002702)  
  3.   
  4. Usage: dotnet [common-options] [command] [arguments]  
  5. Arguments  
  6.   
  7. [command]       The command to execute  
  8. [arguments]     Arguments to pass to the command  
  9. Common Options (passed before the command)  
  10.   
  11. -v|--verbose    Enable verbose output  
  12. --version       Display .NET CLI Version Number  
  13. --info          Display .NET CLI Info  
  14. Common Commands  
  15.   
  16. new             Initialize a basic .NET project  
  17. restore         Restore dependencies specified in the .NET project  
  18. build           Builds a .NET project  
  19. publish         Publishes a .NET project for deployment (including the runtime)  
  20. run             Compiles and immediately executes a .NET project  
  21. test            Runs unit tests using the test runner specified in the project  
  22. pack            Creates a NuGet package  
By now, you must have got the point that we are going to look deeper into the commands that .NET provides us and they are:
  1. Create a package

  2. Restore the package

  3. Run the package

    a. Build and Run both work, run would execute, build would just build the project. That was easy.

  4. Packaging the package

I will make it easy to make it even more clear and simple to understand. One thing that you must have figured out is that the option to compile the project natively is not provided as an explicit command in this set of options. As far as I can think, this support has been removed.

Using .NET Core on Linux

Although the procedure on both the environments is similar and alike, I am going to show you the procedure in Ubuntu. Moreover, I will be explaining the purpose of these commands and multiple options that .NET provides you. I don’t want you to feel lonely here, because most of the paragraphs here would be for Microsoft team working on .NET project, so I will be providing a few suggestions for the team too but, don’t worry, I’ll make sure the content seems to be accurate and to the point.

Create a new project

I agree that the framework development is not yet near releasing. Therefore, I think, I should pass on my own suggestions for the project too. At the moment, .NET Core supports creating a new project in the directory and uses the name of the directory as the default name (if at all required). Beginners in .NET Core are only aware of the commands that come right after the “.NET”. However, there are other parameters that collect a few more parameters and optional values such as:

  1. Type of project.

    a. Executable
       - At the moment, .NET only generates DLL files as an output. Console is the default.
    c. Dynamic-link library

  2. Language to be used for programming.

To create a new project, at the moment, you just have to execute the following command:

  1. $ dotnet new  
In my own opinion, if you are just learning, this is enough for you. However, if you execute the following command, you will get an idea of how you can modify the way the project is being created and how it can be used to modify the project itself, including the programming language being used.
  1. $ dotnet new --help  
  2. .NET Initializer  
  3.   
  4. Usage: dotnet new [options]  
  5.   
  6. Options  
  7.   
  8. -h|--help Show help information  
  9. -l|--lang <LANGUAGE> Language of project [C#|F#]  
  10. -t|--type <TYPE> Type of project  
Options are optional. However, you can pass those values if you want to create a project with a different programming language such as F#. You can also change the type; currently however, Console applications are only supported.

I already had a directory set up for my sample testing,

Sample directory open
                                    Figure 2: Sample directory open

Therefore, I just created the project here. You can see that the .NET itself creates the files.

directory
         Figure 3: Creating the project in the same directory where we were

Excuse the file that I created an F# project. I did that so that I can show that I can pass the language to be used in the project too. I removed that and instead created a C# program-based project. This is a minimal Console application.

In .NET Core applications, every project contains,
  1. A program’s source code.

    a. If you create an F# project then the program would be written in F# language

  2. A project.json file.

    a. This file contains the settings for the project and dependencies that are required to be maintained in the project for building the project.

However, once you are going to run, you need to build the project and this is what we are going to do in the subsequent steps.

Restoring the project

We didn’t delete the project. This simply means that the project needs to be restored and the dependencies need to be resolved before we can actually build to run the project. This can be done using the following command,

  1. $ dotnet restore  
Note: Make sure you are in the working directory where the project was created.

This command does the job of restoring the packages. If you try to build the project before restoring the dependencies, you are going to get the error message of:

Project {name} does not have a lock file.

.NET framework uses the lock file to look for the dependencies that a project requires and then starts the building and compilation process. It means this file is required before your project can be executed to ensure “it can execute”.

After this command gets executed, you will get another file in the project directory.

file
        Figure 4: Project.lock.json file is now available in the project directory


Finally, you can continue to build the project and run it on your own machine with the default settings and setup.

Building the project

As I have already mentioned above, the native compilation support has been removed from the toolchain and I think Ubuntu developers may have to wait for a while and this may only be supported on Windows environment, until then, However, we can somehow still execute the project as we would and we can perform other options too, such as publishing and NuGet package creation.

You can build a project using the following command,

  1. $ dotnet build  
Remember, you need to restore the project once. Build command would do the following things for you:
  1. It would build the project for you.

  2. It would create the output directories.

    a. However, as I am going to talk about this later, you can change the directories where the outputs are saved.

  3. It would prompt, if there are any errors while building the project.

We have seen the way, previous commands worked, so let’s slice this one into bits too. This command, too, supports manipulation. It provides you with optional flags such as:

  1. Framework to target.

  2. Directory to use for the output binaries.

  3. Runtime to use.

  4. Configuration etc.

This way, you can automate the process of building by passing the parameters to the .NET script.

Deploying the project

Instead of using the term running the project, I think it would be better, if I say deploying the project. One way or the other, the running project is also deployed on the machine before it can run. First of all, the project would be shown to be running and later I will show how to create NuGet packages.

To run the project, once you have built the project or not, you can just execute the following command:

  1. $ dotnet run  
This command also builds the project if the project is not yet built. Now, if I execute that command on my directory where the project resides, the output is something like this on my Ubuntu,

output
                                 Figure 5: Project output in terminal

As seen, the project works and displays the message of, “Hello World!” on the screen. This is a console project and a simple project with a console output command in C# only. This is the reason why the program works this way.

Creating NuGet packages

Besides this, I would like to share, how you can create the NuGet package from this project using the terminal? NuGet packages have been in the scene for  a very long time and they were previously very easy to create in Visual Studio environment. Process is even simpler in this framework of programming. You just have to execute the following command:
  1. $ dotnet pack  
This command packs the project in a NuGet package. I would like to show you the output that it generates so that you can understand how it is doing everything.
  1. afzaal@afzaal-VirtualBox:~/Projects/Sample$ dotnet pack  
  2. Project Sample (.NETCoreApp,Version=v1.0) was previously compiled.   
  3. Skipping compilation.  
  4. Producing nuget package "Sample.1.0.0" for Sample  
  5. Sample -> /home/afzaal/Projects/Sample/bin/Debug/Sample.1.0.0.nupkg  
  6. Producing nuget package "Sample.1.0.0.symbols" for Sample  
  7. Sample -> /home/afzaal/Projects/Sample/bin/Debug/Sample.1.0.0.symbols.nupkg  
It builds the project first, if the project was built then it skips that process. Once that has been done it create a new package and simply generates the file that can be published on the galleries. NuGet package management command allows you to perform some other functions too, such as updating the version number itself, it also allows you to specify framework etc. For more, have a look at the help output for this command,
  1. afzaal@afzaal-VirtualBox:~/Projects/Sample$ dotnet pack --help  
  2. .NET Packager  
  3.   
  4. Usage - dotnet pack [arguments] [options]  
  5.   
  6. Arguments  
  7.   
  8. <PROJECT> The project to compile, defaults to the current directory.   
  9. It can be a path to a project.json or a project directory  
  10.   
  11. Options  
  12.   
  13. -h|--help Show help information  
  14. -o|--output <OUTPUT_DIR> Directory in which to place outputs  
  15. --no-build Do not build project before packing  
  16. -b|--build-base-path <OUTPUT_DIR> Directory in which to place temporary build outputs  
  17. -c|--configuration <CONFIGURATION> Configuration under which to build  
  18. --version-suffix <VERSION_SUFFIX> Defines what `*` should be replaced with in version   
  19. field in project.json  
See the final one, where it shows the version suffix. It can be used to update the version based on the build version and so on. There is also a setting which allows you to modify the way building process updates the version count. This is a widely used method for changing the version number based on the build that produced the binary outputs.
The NuGet package file was saved in the default output directory.

output directory
                     Figure 6 - NuGet package in the default output directory

The rest is easy, you can just upload the package from here to the NuGet galleries.

Final words

Finally, I was thinking I should publish an ebook about this guide. The content was getting longer and longer. I was feeling tired but this gave me an idea about many things as I can write a comparison of .NET Core on Windows and Linux and I think I have enough time to do that.

Secondly, there are a few suggestions for the end users that I want to make.
  1. Do not use .NET Core for commercial software. It is going to change soon.

  2. .NET Core is a bleeding edge technology and since there is no documentation, you are going to waste a lot of time in learning and asking questions. Due to this, if you are planning to learn .NET framework, then learn the .NET framework and not .NET Core. .NET framework has a great amount of good resources, articles, tips and tutorials.

  3. If you want cross-platform features and great support like .NET framework, my recommendation is Mono Project over .NET Core which maybe because it is yet not mature.

I have feedback for the framework itself.

  1. It is going great.

  2. Since this is a cross-platform framework, features must not be available Windows-only such as the “dotnet compile –native” one. They must be made available to every platform.

Programs can be written easily as the framework is good. I enjoyed programming for .NET Core because it doesn’t require much effort. Additionally, the benefit of multiple programming languages is still there. Besides, Visual Studio Code is also a great IDE that can be used and the C# extension makes it even better. Since, I am free from all of the academic stuff these days, I will be writing a lot about these features.

Up Next
    Ebook Download
    View all
    Learn
    View all