If you have a Raspberry Pi, but you prefer to develop .NET apps for it using Visual Studio, this tutorial will help you build and deploy remotely over SSH.
Note: You can install Visual Studio Code directly on a Raspberry Pi via the package manager, but this tutorial is for interacting with your Raspberry Pi using the full Visual Studio IDE.
Enable SSH on Raspberry Pi
To get started, you must enable SSH on your Raspberry Pi device. You can do this from the command line.
sudo systemctl enable ssh sudo systemctl start ssh
If your Raspberry Pi is a headless system, you can also enable SSH by placing a file named
ssh (no extension) in the root of your SD card’s boot partition.
Install .NET 5 on Raspberry Pi
To install .NET 5 Runtime on your Raspberry Pi, power the device on and connect it to the internet. You will need to know the Raspberry Pi’s local IP address in order to tunnel into it using SSH. You can easily SSH into your Raspberry Pi from a command prompt.
The secure shell will prompt you for the password for the user
pi. Once you are connected to the Raspberry Pi, issue the following command to download and install the .NET 5
dotnet SDK and runtime.
curl -sSL https://dot.net/v1/dotnet-install.sh | bash /dev/stdin -c Current --runtime dotnet
I will be developing and building my apps on my local machine. Then, I will deploy the self-contained app to the Raspberry Pi. If you will be building from your Raspberry Pi directly, you must install the .NET SDK and runtime by issuing the following command instead.
curl -sSL https://dot.net/v1/dotnet-install.sh | bash /dev/stdin -c Current
To execute .NET applications correctly from your Raspberry Pi, you must add the .dotnet directory to your system’s
$Path and add a
DOTNET_ROOT environment variable. You can do this by editing your user’s .bashrc file. You can also make the appropriate changes automatically by executing the following command.
echo 'export DOTNET_ROOT=$HOME/.dotnet' >> ~/.bashrc echo 'export PATH=$PATH:$HOME/.dotnet' >> ~/.bashrc source ~/.bashrc
Configure Visual Studio to Publish for Raspberry Pi
For this project, we will simply create a console application and run it on the Raspberry Pi. Open Visual Studio and create a new Console Application solution targeting .NET 5. I named mine RPiConsole.
using System; Console.WriteLine("Hello from wellsb.com"); Console.ReadLine();
The above example uses the new top-level statements feature available in .NET 5. Running the program will simply open print a single line to the console window.
To publish this project for Raspberry Pi, either right-click the project in the Solution Explorer and select Publish… or select Build > Publish from the menu bar. Add a new Publish profile to publish to a Local Folder.
Edit the resulting profile to change Deployment Mode to Self-Contained and Target Runtime to linux-arm. Take note of the Target Location. Once you save the changes, your .NET console application will be ready to publish.
Navigate to the target location you selected above and open a terminal from there. Push the file to your Raspberry Pi using the
scp RPiConsole [email protected]:/home/pi/
Run the .NET Program on your Raspberry Pi
If you closed the SSH connection with your Raspberry Pi, connect back to it. Grant the program
chmod +x RPiConsole
Now, you are ready to run the .NET 5 console application on your Pi.
The Bottom Line
In this tutorial, you set up a complete development and production environment for developing and running IoT applications. You learned how to configure publishing profiles in Visual Studio to be build .NET 5 apps for Linux on ARM, and you prepared your Raspberry Pi to be able to run these .NET programs. With the foundations in place, you are now ready to dive into the world of electronics. In the next tutorial, we will use the GPIO pins available on the Raspberry Pi to develop an actual IoT application.