Category Archives: Mac and iOS

A quick startup using .NET Core on Linux

I know you may be thinking… This post is another rhetoric post by this guy, yes it is. 🙂 .NET Core is another product that I like, the first one being, .NET framework itself. It was last year when .NET Core got started and Microsoft said they are going to release the source code as a part of their open source environment and love. By following the open source project environment and ethics, Microsoft has been working very hard in bringing global developers toward 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, 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 quick post, I will walk you through getting started with .NET Core, installing it on a Linux machine and I will also give my views as to why 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 terminal based environment to perform multiple tasks. But first things first.

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

  1. C# 6
    • 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, please read my previous blog post at, Experimenting with C# 6’s new features.
  2. .NET Core
    • Of course, who wouldn’t love to use .NET on other platforms.
  3. Xamarin acquisition
    • I’m going to try this one out tonight. Fingers crossed.
  4. Rest are all just “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.

Screenshot (898)
Figure 1: .NET Core is a cross-platform programming framework by Microsoft.

Why Linux for .NET Core?

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

1. .NET Core is not yet complete

It would take a while before .NET gets released as 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.

  1. Install VirtualBox (or any other virtualization software that you prefer; I like VirtualBox for its simplicity).
  2. Set up an Ubuntu environment.
    • Remember to use Ubuntu 14.04. Later ones are not yet supported yet.
  3. Install the .NET Core on that machine.

If something goes wrong. 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.

2. 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 going to use anything that requires a command-line interface to be used on my Windows environment. In Linux, however, everything almost has a command-line interface and since the .NET Core is a command-based program by Microsoft that is why I have enjoyed using it on Linux as compared to Windows.

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

3. 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 does support it, its just that I don’t support that development environment. 😀

Installation of .NET Core

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

$ sudo apt-get install dotnet

Same command is used on Mac OS X and other operating systems other than Ubuntu and Debian derivatives. But until the .NET Core is in development process that 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.

Installation procedure of .NET Core on multiple platforms.

After this step, do 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.

> dotnet --help

If .NET is installed, it would simply show the version and other help material on the screen. Otherwise, you may want to make sure that that 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,

afzaal@afzaal-VirtualBox:~/Projects/Sample$ dotnet --help
.NET Command Line Tools (1.0.0-preview1-002702)
Usage: dotnet [common-options] [command] [arguments]

Arguments:
 [command]      The command to execute
 [arguments]    Arguments to pass to the command

Common Options (passed before the command):
 -v|--verbose   Enable verbose output
 --version      Display .NET CLI Version Number
 --info         Display .NET CLI Info

Common Commands:
 new           Initialize a basic .NET project
 restore       Restore dependencies specified in the .NET project
 build         Builds a .NET project
 publish       Publishes a .NET project for deployment (including the runtime)
 run           Compiles and immediately executes a .NET project
 test          Runs unit tests using the test runner specified in the project
 pack          Creates a NuGet package

So, you get the point that we are going to look deeper into the commands that dotnet provides us with.

  1. Creating a package
  2. Restoring the package
  3. Running the package
    • Build and Run both work, run would execute, build would just build the project. That was easy.
  4. Packaging the package

I will slice the stuff down to make it even more clearer and simpler to understand. One thing that you may have figured 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 is that this support has been removed until further notice. Until then, you need to pass a framework type to compile and build against.

Using .NET Core on Linux

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

1. Creating a new project

I agree that the framework development is not yet near releasing and so I think I should consider passing 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 “dotnet”. However, there are other parameters that collect a few more parameters and optional values such as:

  1. Type of project.
    • Executable
      • At the moment, .NET only generates DLL files as output. Console is the default.
    • 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:

$ 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 project is being created and how it can be used to modify the project itself, including the programming language being used.

$ dotnet new --help
.NET Initializer

Usage: dotnet new [options]

Options:
 -h|--help Show help information
 -l|--lang <LANGUAGE> Language of project [C#|F#]
 -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,

Screenshot (899)
Figure 2: Sample directory open. 

So, I just created the project here. I didn’t mess around with anything at all, you can see that the .NET itself creates the files.

Screenshot (900)
Figure 3: Creating the project in the same directory where we were.

Excuse the fact 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.
    • If you were to create an F# project. Then the program would be written in F# language and in case of default settings. The program is a C# language program.
  2. A project.json file.
    • This file contains the settings for the project and dependencies that are to be maintained in the project for building the project.

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

2. 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,

$ 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. Which 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.

Screenshot (902)
Figure 4: Project.lock.json file is now available in the project directory.

And so finally, you can continue to building the project and to running it on your own machine with the default settings and setup.

3. 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,

$ dotnet build

Remember that you need to have restored 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.
    • 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 output binaries.
  3. Runtime to use.
  4. Configuration etc.

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

4. Deploying the project

Instead of using the term running the project, I think it would be better if I could say deploying the project. One way or the other, running project is also deployed on the machine before it can run. First of all, the project I would show to be running, 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:

$ 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,

Screenshot (904)
Figure 5: Project output in terminal.

As seen, the project works and displays the message of, “Hello World!” on screen in terminal. This is a console project and a simple project with a console output command in C# only. That is 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 since 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:

$ 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.

afzaal@afzaal-VirtualBox:~/Projects/Sample$ dotnet pack
Project Sample (.NETCoreApp,Version=v1.0) was previously compiled. 
Skipping compilation.
Producing nuget package "Sample.1.0.0" for Sample
Sample -> /home/afzaal/Projects/Sample/bin/Debug/Sample.1.0.0.nupkg
Producing nuget package "Sample.1.0.0.symbols" for Sample
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,

afzaal@afzaal-VirtualBox:~/Projects/Sample$ dotnet pack --help
.NET Packager

Usage: dotnet pack [arguments] [options]

Arguments:
 <PROJECT> The project to compile, defaults to the current directory. 
 Can be a path to a project.json or a project directory

Options:
 -h|--help Show help information
 -o|--output <OUTPUT_DIR> Directory in which to place outputs
 --no-build Do not build project before packing
 -b|--build-base-path <OUTPUT_DIR> Directory in which to place temporary build outputs
 -c|--configuration <CONFIGURATION> Configuration under which to build
 --version-suffix <VERSION_SUFFIX> Defines what `*` should be replaced with in version 
  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 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.

Screenshot (905)
Figure 6: NuGet package in the default output directory.

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

Final words

Finally, I was thinking I should publish a minimal ebook about this guide. The content was getting longer and longer and I was getting tired and tired, however since this gave me an idea about many things I think 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 few suggestions for end users that I want to make.

  1. Do not use .NET Core for commercial software. It is going to change sooner,
  2. .NET Core is a bleeding edge technology and well, since there is no documentation, you are going to waste a lot of time in learning and asking questions. That is why, if you are considering 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 maybe because it is yet not mature.

I have a few feedback words on the framework itself.

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

At last, the framework is a great one to write programs for. I enjoyed programming for .NET Core because it doesn’t require much effort. Plus, the benefit of multiple programming languages is still there, Besides, Visual Studio Code is also a great IDE to be used and the C# extension makes it even better. I will be writing a lot about these features these days since I am free from all of the academics stuff these days. 🙂

See you in the next post.

Advertisements

Programming C# on Mac — One post guide

Introduction and Background

I have dedicated most of my time to C# and C# programmers, I have already written a brief description and guide for C# programmers to be able to port their applications, develop them, build them and install them on Ubuntu… Which indirectly focusses on all of the Linux environments. Basically, C# is a Windows programming language and many frameworks like .NET, Windows Runtime, ASP.NET etc. are all using C# as their “popular” programming language. But developers have also worked very hard to provide a cross-platform support for C# programming language, using Mono you can develop C# applications and build and deploy C# applications on non-Windows environments too. If you are interested in the basic post, please read this post: Using C# for cross-platform development. You will be shown how Mono can help you, how you can build the applications on multiple frameworks and use them on those frameworks, operating systems too, and not just on Windows or .NET framework environments.

In this post, I am going to demonstrate how you can use C# for programming a Mac environment. I have never been a “huge” fan of Apple products, however, since I love C# so I think it should also be promoted on Mac environments and among Mac developers! C# can highly leverage the performance of their applications. If you are interested in using Mono framework, I recommend that you read the guide for C# programming on Ubuntu, the framework and features are similar. However, the installation is somewhat different and that is the topic for my post and I will explain the methods and steps required to install, run and test the Mono project, then I will also show how you can install Xamarin Studio (the IDE for Mono on Mac). So basically, this post is about setting up the environment for C# programming on Mac. I remember, when Siddharth Koya asked me whether I can write a guide for Mac or not, I told him that it would take some time… However, Sid! It is here. You can read this and then continue to read the related Ubuntu guides for Mono programming.

Information about Mono platform

So, before I move further, there are some prerequisites to the platform, too! You need to make sure you are using the correct platform of Mac OS and also that you are having required packages installed. Otherwise, Mono may not work! Mono supports, Mac OS 10.7 and onwards. You must at least have 10.7, otherwise, you should upgrade your systems.

Setting up the environment

The method for installing the Mono on Mac is somewhat different from others, however is also very easy at the same time. You need to have internet connection before you can continue to install the packages. The packages are free for download, install and then use. So basically, all you need to do is, just capture those packages, run and install them and there, you will have C# environment installed on your Mac in no time! First of all, I will show you how to install the packages, starting by showing you the URL address of the website, later I will show you to check if environment is set up or not, later I will show you how to install the IDE for Mono.

Mono is basically a set of compilers, debuggers and assemblies required to compile and build the projects. However, there are some GUI applications which integrate the components and present you with an IDE (Integrated development environment), which can be used to develop and build applications rapidly, without having to execute scripts each time to make your project work, you are provided with a clean and intuitive interface that can be used to create, modify and build the projects. IDEs also allow you to debug the applications in case you want to fine tune them, remove bugs or make them performance efficient.

So basically, what IDEs are meant to do is that they provide you with controls, that you can use to perform your actions that typically are done using scripts.

Downloading and installing the packages

First of all, I will talk about installing the packages and assemblies of Mono project. Now, in Ubuntu we did that using the terminal, in Mac, you are going to download the package itself and then install it using the GUI installer. The process is analogous to what we have on Windows environment, however, there are a few security steps that you would have to undergo before you can install the package itself. So, let’s start with downloading and installing the packages.

You can follow the same URL for Mono Project, and download the packages for Mac instead. Go to Mono Project’s Download web page and download the packages for Mac. If you are on Mac, the website will automatically load the page for Mac.

Screenshot (6191)
Figure 1: Mono downloads for Mac.

Typically, you are going to install using the 32-bit version. However, you can also install the packages for 64-bit environment. It would take some time depending on your network, once that is downloaded. Open it, and it would give you an installer. The installer would lead you through the procedure and you will have your packages installed successfully!

Screenshot (6192)
Figure 2: Package being downloaded.

Once this gets downloaded, open it…

Screenshot (5898)
Figure 3: Mono framework installer.

This installer is actually going to guide you through different steps, that you require to install Mono project on your Mac, however just for the sake of formality I am going to describe these stages. Once you click continue and move forward a bit, you will be shown the license information about the product. You “should” read the license terms before you click “I Agree”. Even though the license and product are free, you should know how much freedom you actually have. So, read the license and then continue.

Screenshot (5899)
Figure 4: Must read the license terms before you continue and click Agree. 

Continue to the next steps now. Once you have entered the details for disk usage, drive selection and others, you will be asked for security check, your password! Enter the password and continue with installation of the package.

Screenshot (5901)
Figure 5: Security check before installation.

You will then continue to install the package. The installation process would take some time, and after a while it would show you a success message. This message tells that the package has been indeed installed.

Screenshot (5902)
Figure 6: Mono framework is installed.

Although the packages have been installed. But, I would “recommend” that you test the packages and the framework itself. So, in the next section I am going to show you how to test if everything is OK!

Simple hello world!

In this section, I am going to show you a simple hello world program, using which you can determine if framework is installed correctly, or if there were some troubles during your installation process. You are all already very much aware and familiar with the hello world programs, so basically I am going to use that one program and I am going to demonstrate how you can test the C# environment easily, using the terminal.

Remember: Without IDE, you are going to use terminal, so I would recommend that you go and learn terminal scripting. Otherwise, just follow the scripts I share and then install Xamarin Studio if you don’t feel easy with terminal.

Anyways, you would require to create a new file… What I do is that I try to create a separate folder for each new project, you should do the same. Create a new folder, anywhere, (I chose desktop) and then create a new file in it, name it “Program.cs”. To do so, you would use the following shell script. Uh huh, before you write the code, open the terminal… For most of Apple users, it is found under Utilities in Application tab.

Screenshot (6194)
Figure 7: Terminal program in the Utilities tab.

Once you have opened the terminal, go to the location where your folder is created. Currently, it would be like this:

Screenshot (6193)
Figure 8: Program directory empty at the moment.

Initially the directory is empty, so we would create the file here. In that directory, create a new file, I used the following shell script to create a new file:

echo "" > Program.cs

This would create a new file, named Program.cs in your currently active directory (make sure it is the directory that you want!), the file would be created. Have a look below:

Screenshot (6195)
Figure 9: Shell script for pwd; “present working directory”.

Just to ensure that we are on the right directory. Later, enter that shell script to create the file. Shell won’t tell you what happened, it would just create the file there.

Screenshot (6196)
Figure 10: Shell.

As already mentioned, Shell didn’t reply or show a message. It just executed that statement, command or what-ever you want to call it.

Screenshot (6197)
Figure 11: Directory, file and the shell visible. A new file “Program.cs” created in the directory.

All three of our items are visible here! Our file has been created and we can now edit the file to write a C# program.

using System;

namespace Text {
   class Program {
      public static void Main(string[] args) {
         Console.WriteLine(25);
      }
   }
}

Sadly, Mac wouldn’t allow me to use double quotation marks and would turn them into something else, generating a compiler error. That is why I had to use an integer value and not a string message. However, to compile this program… Go to your directory, open up terminal, enter the following commands:

mcs Program.cs
mono Program.exe

These are the commands to compile and execute the programs.

  1. mcs Program.cs
    • This command would compile and build the program. Generating the executables in the same directory.
    • mcs == Mono C Sharp compiler.
  2. mono Program.exe
    • mcs would generate the executable (.exe) in the same directory, and this command would execute it under mono runtime!

The output would also be shown right under the command itself, in the terminal.

Screenshot (6199)
Figure 12: Program and build output shown.

You can see the program output and build output! Program output is shown in the terminal, where as build output is shown in the directory. However, this shows that our environment is set up and that we can continue to install other stuff, or use this and start building our C# projects to run on Mac itself.

Installing IDE

I don’t recommend going terminal. IDE is a great tool, that can help you in many things…

  1. Syntax highlighting: You can see in the code above, it is hard to see what is going on, unless you are experienced.
  2. Code suggestions.
  3. Keyboard shortcut to compile, build and debug.
  4. Debuggers and breakpoints.
  5. Integrated environment.
  6. Configurations for project files and output directories.

Much more! So I would recommend that you start by installing an IDE on your machine, instead of doing all this yourself by a terminal. Which takes a lot of time, I would say.

The IDE for Mac is different from what we had in Ubuntu, in Ubuntu we had MonoDevelop, however on Mac we have Xamarin Studio. They are both provided by Mono Project (which is led by Xamarin). You will now need to download Xamarin Studio from their download web page, download the package for Mac and install it.

It would start and Mac would try to run a security check and other package checks,

Screenshot (5931)
Figure 13: Verification of the Xamarin Studio package.

This would take a while, let it take a while. Later, you know what to do! Drag and drop the application to install it.

Screenshot (5932)
Figure 14: Drag and drop the Xamarin Studio to install.

Drag and drop it in Applications, it would copy the files and then your IDE would be installed for you on your machine.

Screenshot (5936)
Figure 15: Xamarin Studio installed on Mac.

Click on it to start it! Mac would attempt to tell you that this application was downloaded from Internet and that you should not trust everyone on internet, just continue to the next step.

Screenshot (5937)
Figure 16: Security warning by Mac.

Open it and then you would have your IDE active in front of you!

Screenshot (5939)
Figure 17: Xamarin Studio active window.

You can now create a new project, and then continue to use the Mono Project in your environment to create applications in C#!

Points of Interest

This is a very short post about Programming C# on Mac, which covers the required topics about setting up the environment for C# programming on Mac, however to continue learning more about Mono Project, what it offers you, how you can program C# on other platforms, I recommend that you check out my previous posts about “Programming C# on Ubuntu”! I am sure you are going to enjoy those posts, if you like C#.

Programming C# on Linux or Mac is similar to using C# on Windows platform like .NET or Windows Runtime. Mono Project brings the same efficiency, same flexibility and same power to other platforms too, so that you can build your C# programs on Mac and Linux too. However, remember, that the programs built using Mono are executed on “mono” environment, or “mono runtime”. Mono runtime would be required before you can execute those applications, because Mono runtime provides the underlying foundation for this cross-platform support. Without this, you won’t be able to execute the programs, which is similar to installing .NET framework on Windows!

Anyways, this was to help you get started in Mac too, now you can check out the rest of posts! Hope to see you there too. 🙂