Raspberry Pi and Mono – Hello World!

OK, it has to be done. The first article you write about anything new has to contain the immortal words “Hello World”. This article explains the process of getting Mono set up on a Raspberry Pi, then compiling and running your first application. But first, a bit of a sales pitch…

Why would a C# developer want to use a Raspberry Pi?

  • It’s fantastic for embedded projects, uses little power and costs peanuts.
  • It’s got great connectivity, it’s easy to add storage with external disks, a webcam or two, a wifi dongle and cellular connectivity – so the Raspberry Pi is the perfect platform for an Appliance application. Want a webcam up the tree in your back garden to take photos of your bird feeder? Want to strap it under your car to read data from the engine management system? Build a mobile robot? You probably don’t want to use a laptop – or even a more expensive embedded platform – for that.
  • The Pi is simple to get going even if you have no Linux knowledge (as I hope to demonstrate here). You can be up and running in a couple of hours. I was writing C# unit tests two hours after opening the box!
  • Did I mention the cost? £30 for a PC? Amazing!

Why should I use C# on the Pi?

  • C# is a beautiful language. Perhaps that sounds like nonsense but C# focusses on clean, readable code that’s easy to write and easy to maintain.
  • It’s cross platform – something like 90% of the .NET framework is supported in Mono. You can copy an executable from your Windows PC to the Pi and run it – simple as that.
  • The .NET framework does all the boring stuff for you: Opening sockets, accessing files, manipulating strings, serving web pages, connecting to databases… You name it, there’s a function to do it. All you have to do is write the code that’s unique to your application (the fun bit).
  • C# is a proper language! If you want to learn a language that will get you a job – that will teach you how “real” programming is done – C# is one of the big three (C++, Java and C# (see footnote!)). It’s a strongly typed, object oriented, elegant language with all the bells and whistles. Learning one of the big three is going to be better than Python any day of the week
  • It’s very similar to Java and much easier to pick up than C++. Garbage collection and a total lack of pointers make life simpler and there’s no price to pay in terms of capability. C# has some features (Delegates, foreach loops, linq to objects…) which make it more attractive to me than Java.
  • C# and Mono have a huge online community who are passionate about the language and the platform. The answer is never more than a Google away.
  • The development tools are 100% free. There’s an Express version of Visual Studio for the Windows side and a free IDE – MonoDevelop – for X Windows.

So, hopefully I’ve made my point. Now lets get cracking…

Step 1: Unpack your Pi, find a phone charger and a network cable as well as a USB keyboard. You’ll need a mouse too if you want to use X Windows but I am not going to bother with that now. It’s surplus to requirements.

Step 2: Download a Raspberry Pi disk image from the website. At the time of writing there is a bug with Mono on Debian – due to an issue with a rounding function in the Maths libraries which also causes problems with DateTime. For that reason I downloaded and installed the Arch Linux image. Arch is pitched as the “hardcore” distribution for the Pi but I actually found it very easy to use. Follow the instructions to get your disk image onto an SD card and boot up your Pi.

Step 3: Log in to your Pi using the standard username and password. If you’ve used Debian you’ll need to answer a few setup questions before you can log in.

Step 4: Make sure your system is up to date. In this step we get the package manager to download all the latest and greatest libraries. New libraries are released at least once a week and contain bug fixes and optimisations to make your Pi run quicker. You should repeat this step every week or so to make sure you’re up to date.

Debian:

sudo apt-get update
sudo apt-get upgrade

Arch:

sudo pacman -Syu

Step 5: Get a text editor. First thing I get on any new Linux machine is emacs. I know a lot of people prefer vi or something else, I don’t really care what you use, I use Emacs. Feel free to grab your favourite editor, the procedure is much the same.

Debian:

sudo apt-get install emacs

Arch:

sudo pacman -S emacs

Step 6: Install Mono (see, I told you it was easy, we’re only on step 6!).

Debian:

sudo apt-get install mono-complete

Arch:

sudo pacman -S mono-complete

Step 7: Write some code. We’re not using a Solution or a Project here, just writing a simple source file an compiling it. This listing shows how to create a directory for the files and open the source file in a text edtor. Debian and Arch are identical from this point on.

mkdir HelloWorld
cd HelloWorld
emacs HelloWorld.cs

Type or paste the following code into the editor…

using System;

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

Step 8: Compile and run it like this…

gmcs HelloWorld.cs
mono HelloWorld.exe

Wowzers! You just installed the whole .NET framework and compiled a working application in 8 steps. Doing the same exercise in Java or C++ wouldn’t be any quicker than that.

Of course this is just the start, there are a thousand other things you’ll want to do next. Sort out your display settings, install a database like sqlite or mysql, develop a user interface, add a webcam, sort our SSH access and so on… C# and Mono will help you bring all those things together and very rapidly make your Raspberry Pi do some very cool stuff.

The footnote:  OK, OK, I know, the “big three” I was banging on about are actually three of the top five languages.  I discounted C because it’s not object oriented.  If you’re using anything other than a resources limited embedded system, you should be working with objects and patterns.  I discounted Objective-C because I don’t understand it, or even know if it can be used on the Pi.  IMHO, you’re only going to use Objective C if you’re writing an iPad app and I don’t like Apple!

26 thoughts on “Raspberry Pi and Mono – Hello World!

  1. I am having trouble with step 8. After typing gcms helloworld.cs it says
    -bash: gcms: command not found. Do you know what I may have missed?

  2. Thank you so much for the page! I started to learn C for the pi… gave up after I learned that string concatenation involved more than just a +…. C# it is!

    Make any GUI’s yet for the pi? That’ll be what I’m up to next.

  3. Rebuilt SD card with Debian, ran apt-get install mono-complete.

    Still same error with gcms not found. What is gcms and can it be installed manually?

  4. Heh… there’s a typo
    its supposed to be gmcs HelloWorld.cs, and the vb version uses gmvb
    Thanks for a wonderfully concise walkthrough
    (Now to get ASP.Net, then ServiceStack working…)

  5. Very strange! gmcs is the mono C# compiler. I am shocked to find it’s not in the mono-complete package (it was when I wrote the article).

    Try: apt-get install mono-gcms

  6. Hey, was just going thru the steps and pacman -S mono-complete did not work. Stated error target not found: mono-complete. Typed in just pacman -S mono and now it’s installing. Just wanted to let you know. Great post. Thanks for the info 🙂

  7. Many thanks for this article. I’m revising C# for a project at work and don’t have a pc at home. Incidently, objective c can run on the PI. You need the gcc compiler, the lobjc package, and the gnustep framework (using apt-get).

  8. Followed this guide. Oddly enough, it only worked for me if I did ‘su root’ first. Using sudo it kept failing the install.

  9. Hi.

    During installtion of mono I get a lot of the following:
    Problem: O:System.Data.SqlTypes.SqlInt16.Conversion, with xpath: /Type/Members/Member[@MemberName=’op_Conversion’]/Docs

    All related to /Type/Members/Member.
    I performed sudo apt-get install mono-complete.

  10. Hi,
    Thanks a ton for a wonderful guide for installing mono in Raspberry Pi. I have just now got the Raspberry Pi and was wondering if it may run the mono asp.net applications.
    I have got the applications running in Ubuntu, and was trying to install mod_mono to host the ASP.net application, but everytime i am installing libapache2-mod-mono i am getting errors for unmet dependencies.
    Can u please help me out on the issue. Do i have to compile the mono project from source for it working?
    I will be highly obliged, if u may help on the issue.
    Regards.

    Abhinav Aggarwal

  11. Thanks for the quick install tut.
    My tip for the Editor is to use Geany instead of Emacs. You cannot only write the code with syntax highlighting, you can compile too.

  12. You forgot to mention Visual Basic. It is a great tool for those of us who are not quite up to the task of learning C#. Anyhoo, you’ve inspired me to go out and buy a Pi 😉

  13. I’m trying to squeeze in Raspberry Pi with all else that I am doing. It would be great to see more articles on using C# on the RPi.

  14. Hi,

    I’m using vs2015 community, is it possible to get the mono-debugger for this version?
    Or getting the source code?

    Thanks!

  15. I know this is a really old post, but I just want to comment on where you said that it’s quicker than using C++. It’s not. G++ comes with raspbian and thus you can skip out a stage. You can also just use nano or vi which comes with raspbian as well.

Leave a Reply

Your email address will not be published. Required fields are marked *