I recently heard about remote development with Visual Studio Code. It has always been a bit of a hassle developing on the Raspberry Pi; being logged in over ssh and coding in nano or VIM (I wish I was a VIM aficionado) can be real hard if not used to it. Now however, it’s incredibly simple to set up a development environment where you code on your regular computer remotely on the Raspberry Pi!

Setting up SSH

First and foremost, you need to make sure that you have passwordless ssh setup so you can easily log in on the RPi. This will be a bit different depending on the operating system, but the basic idea is the same. I will demonstrate how to set it up on Windows 10. SSH will of course have to be enabled on the RPi already - you can do this from the raspi-config command.

On your development machine:

First we will have to generate a public/private key pair that we’ll later upload to the RPi to tell it that it really is us trying to SSH into it.

Open up a PowerShell prompt in administrative mode and issue the following commands:

cd ~\.ssh
ssh-keygen

The key generation will prompt you for a filename. Just go with the default unless you already have some saved. If you do change the name, you need to remember to change the following commands accordingly.

You will also be asked if you want to use a passphrase, and for Remote Development to work you should not add a passphrase. Yes, this is less secure but the idea is that VSCode should be able to login without having to supply any passwords at all. Be sure to keep your private key safe and you should be good.

You will now have to load the generated key into the ssh agent. Please note: if you have cmder installed this will differ a bit, so please skip to the section below instead.

Without cmder installed

First, we’ll make sure the agent is started, and will start automatically upon reboot:

Set-Service ssh-agent -StartupType Automatic
Start-Service ssh-agent

And to load the key into the agent, simply type in:

ssh-add ~\.ssh\id_rsa

With cmder installed

If you are using cmder as your terminal of choice, and I highly recommend it, the above steps don’t apply as they bundle their own ssh. Instead open up the profile file for cmder located at <cmder install path>\config\user-profile.cmd and remove the double colon in front of the line mentioning start-ssh-agent.cmd so it looks like this:

call "%GIT_INSTALL_ROOT%/cmd/start-ssh-agent.cmd"

Save the file and restart cmder. This change will automatically copy all ssh key files you have under your ssh profiles into the agent at startup.

Copy the key to your Raspberry Pi

We will now have to copy the public key we generated to the RPi.

Please note: In these examples I assume that you haven’t changed the default username of pi, and that the IP address of your RPi is 192.168.0.10, so change your commands according to your setup.

First we’ll create a new directory to hold the public key. From your development machine, type in:

ssh pi@192.168.0.10 mkdir ~/.ssh

It will ask you for the password on the RPi since we haven’t gotten the public key copied yet, so don’t be alarmed. Next, type in:

scp id_rsa.pub pi@192.168.0.10:~\.ssh\authorized_keys

scp stands for “secure copy”, and this command will copy the file id_rsa.pub from your development machine to the Raspberry Pi and put it in the right directory. When this is done, we can check our progress:

ssh pi@192.168.0.10

If you get connected to the Raspberry Pi without having to enter a password, you’re done! If not, scroll back up and try to figure out where you might have missed a step.

Setting up Visual Studio Code for remote development

So now for the fun part! Open up Visual Studio Code, and go into the extensions library. Install the extension named Remote Development:

Remote Development extension page

This is an extension bundle that will install all the remote development extensions released by Microsoft. They have a couple of different ones, at the time of this article the bundle includes extensions for remote Docker development, WSL development and over SSH, which is the one we will be using.

Once the extensions have been installed, open up the command palette with CTRL-SHIFT-P and start to type in remote ssh and choose the Add new host option.

The command palette showing the remote SSH commands

This will change the command palette into an input box where you can put in the host, so type in ssh pi@192.168.0.10 and press enter. It will then ask which file to save it to. I just chose the default config file for my user.

Once this is finished, open up the command palette again (CTRL-SHIFT-P) and choose Connect to host instead, and choose the host you just created. A new Visual Studio Code window will popup, and it will start the installation process of the VSCode server on the Raspberry Pi automatically.

When this is done, you are ready to start coding! You are now remotely connected to the Raspberry Pi, and everything you create and save will be saved directly on the Pi -  even the terminal windows you open will run in the context of the Raspberry Pi. Very cool!