I decided to play around with running C# on a Raspberry Pi for a variety of reasons. First and foremost: I’ve been wanting to branch out into another language for a while. JavaScript is great, and it will always be my true love. But I have been feeling pretty limited lately about what I can accomplish knowing only one language. And I’ve always heard that learning the second language is much easier than learning the first.

But why C#? The honest reason is that C# is really popular around OKC. Lots of companies use it, and in an effort to be competitive, I figured I would pick it up too. Plus I know a lot of people who know C# whom I could ask for help.

But once I selected C#, a problem arose. I use a Mac, and that doesn’t jive with C#. There were some solutions I found including getting Parallels and installing Windows on a VM and writing the C# in my Windows VM. But even typing that sentence just now made my head spin. I was looking for something simple that would allow me to lean on what I already know as much as possible.

Enter: My Raspberry Pi.

I was able to set up my Raspberry Pi to run C# apps without much trouble. This solved a few problems. I’m able to remote into my Pi from my Mac, and I didn’t have to install anything on my Mac. I wasn’t too worried about breaking my Raspberry Pi, because it was only \$35. It would be far easier to replace than my Mac, and if anything were corrupted it would likely be as simple as replacing the SD card. Easy-peasy. Also the set up to be able to run C# apps was incredibly easy. I joke around about things taking 5 minutes, but this literally took less than 5 minutes.

** I should note here that this process assumes you are already able to ssh into your Raspberry Pi. Or learn how to set up a pi from scratch. **

### 1. ssh into your Raspberry Pi using the terminal

Pretty easy: ssh username@pi and then enter your password.

### 2. Install Mono

Mono is an implementation of the .NET framework that can be installed on Linux. Since the Pi is running a flavor of Linux, this is what we’ll use to run our apps.

sudo apt-get update sudo apt-get install mono-complete

And that’s all you need!

### 3. Get a text editor && write your app!

Usually I prefer to use an IDE, but since I was writing a super simple Hello World app, I didn’t think I needed one. I chose nano, but you can use whichever terminal text editor you like. I just liked nano because it comes with bash, which is already on the Pi. Also, a side note: writing your app might take more than 5 minutes. Don’t @ me.

Use this command to create and edit a file: nano yourFileName.cs.

If you aren’t familiar with nano, it can be a little confusing. The thing I always fumble for is saving my work and closing. This is done with cmd-X. You’ll get a prompt to save and get kicked back into the directory the file lives in. If you want to see the contents of a file, you can use: cat yourFileName.cs.

** Remember to use the .cs extension since we’re writing C# apps! **

### 4. Compile to .exe

This is where Mono comes in.

Use this command: mcs yourFileName.cs

That will create: yourFileName.exe in the same directory. Pretty cool, right?

### 5. Run it!

To run the program use this command: mono yourFileName.exe. Just make sure you target the .exe and not the .cs file.

### Extra credit:

Let’s pretend for a moment that you don’t like writing apps in the terminal. (It’s ok, I don’t either!) There must be a way to write the app in an IDE and then run it, right? You’re in luck!

There are two options. First, you could simply plug your Pi into a monitor and download Visual Studio Code. VS Code is compatible with Linux (at least, according to the VS Code website) and you can write C# with it. This keeps you in the Pi 100% of the time.

However, I was looking for another solution. I’m very comfortable writing code on my Mac and wanted to stay there. Solution: Use VS Code for C# apps, and transfer the files to the Pi via scp. Scp comes included with bash, and is very easy to use.

scp /path/to/your/local/file remoteUser@some_address:/home/remoteUser/Documents

And there you have it! You’re ready to write C# apps and run them on a Raspberry Pi. Happy hacking! Be sure to let me know what cool projects you come up with!

Questions? Tweet me!