Coding on Raspberry Pi remotely with Visual Studio Code

Jim Bennett from Microsoft, who showed you all how to get Visual Studio Code up and running on Raspberry Pi last week, is back to explain how to use VS Code for remote development on a headless Raspberry Pi.

Like a lot of Raspberry Pi users, I like to run my Raspberry Pi as a ‘headless’ device to control various electronics – such as a busy light to let my family know I’m in meetings, or my IoT powered ugly sweater.

The upside of headless is that my Raspberry Pi can be anywhere, not tied to a monitor, keyboard and mouse. The downside is programming and debugging it – do you plug your Raspberry Pi into a monitor and run the full Raspberry Pi OS desktop, or do you use Raspberry Pi OS Lite and try to program and debug over SSH using the command line? Or is there a better way?

Remote development with VS Code to the rescue

There is a better way – using Visual Studio Code remote development! Visual Studio Code, or VS Code, is a free, open source, developer’s text editor with a whole swathe of extensions to support you coding in multiple languages, and provide tools to support your development. I practically live day to day in VS Code: whether I’m writing blog posts, documentation or Python code, or programming microcontrollers, it’s my work ‘home’. You can run VS Code on Windows, macOS, and of course on a Raspberry Pi.

One of the extensions that helps here is the Remote SSH extension, part of a pack of remote development extensions. This extension allows you to connect to a remote device over SSH, and run VS Code as if you were running on that remote device. You see the remote file system, the VS Code terminal runs on the remote device, and you access the remote device’s hardware. When you are debugging, the debug session runs on the remote device, but VS Code runs on the host machine.

Photograph of Raspberry Pi 4
Raspberry Pi 4

For example – I can run VS Code on my MacBook Pro, and connect remotely to a Raspberry Pi 4 that is running headless. I can access the Raspberry Pi file system, run commands on a terminal connected to it, access whatever hardware my Raspberry Pi has, and debug on it.

Remote SSH needs a Raspberry Pi 3 or 4. It is not supported on older Raspberry Pis, or on Raspberry Pi Zero.

Set up remote development on Raspberry Pi

For remote development, your Raspberry Pi needs to be connected to your network either by ethernet or WiFi, and have SSH enabled. The Raspberry Pi documentation has a great article on setting up a headless Raspberry Pi if you don’t already know how to do this.

You also need to know either the IP address of the Raspberry Pi, or its hostname. If you don’t know how to do this, it is also covered in the Raspberry Pi documentation.

Connect to the Raspberry Pi from VS Code

Once the Raspberry Pi is set up, you can connect from VS Code on your Mac or PC.

First make sure you have VS Code installed. If not, you can install it from the VS Code downloads page.

From inside VS Code, you will need to install the Remote SSH extension. Select the Extensions tab from the sidebar menu, then search for Remote development. Select the Remote Development extension, and select the Install button.

Next you can connect to your Raspberry Pi. Launch the VS Code command palette using Ctrl+Shift+P on Linux or Windows, or Cmd+Shift+P on macOS. Search for and select Remote SSH: Connect current window to host (there’s also a connect to host option that will create a new window).

Enter the SSH connection details, using user@host. For the user, enter the Raspberry Pi username (the default is pi). For the host, enter the IP address of the Raspberry Pi, or the hostname. The hostname needs to end with .local, so if you are using the default hostname of raspberrypi, enter raspberrypi.local.

The .local syntax is supported on macOS and the latest versions of Windows or Linux. If it doesn’t work for you then you can install additional software locally to add support. On Linux, install Avahi using the command sudo apt-get install avahi-daemon. On Windows, install either Bonjour Print Services for Windows, or iTunes for Windows.

For example, to connect to my Raspberry Pi 400 with a hostname of pi-400 using the default pi user, I enter pi@pi-400.local.

The first time you connect, it will validate the fingerprint to ensure you are connecting to the correct host. Select Continue from this dialog.

Enter your Raspberry Pi’s password when promoted. The default is raspberry, but you should have changed this (really, you should!).

VS Code will then install the relevant tools on the Raspberry Pi and configure the remote SSH connection.

Code!

You will now be all set up and ready to code on your Raspberry Pi. Start by opening a folder or cloning a git repository and away you go coding, debugging and deploying your applications.

In the remote session, not all extensions you have installed locally will be available remotely. Any extensions that change the behavior of VS Code as an application, such as themes or tools for managing cloud resources, will be available.

Things like language packs and other programming tools are not installed in the remote session, so you’ll need to re-install them. When you install these extensions, you’ll see the Install button has changed to Install in SSH:< hostname > to show it’s being installed remotely.

VS Code may seem daunting at first – it’s a powerful tool with a huge range of extensions. The good news is Microsoft has you covered with lots of hands-on, self-guided learning guides on how to use it with different languages and development tools, from using Git version control, to developing web applications. There’s even a guide to learning Python basics with Wonder Woman!

Jim with his arms folded wearing a dark t shirt
Jim Bennett

You remember Jim – his blog Expecting Someone Geekier is well good. You can find him on Twitter @jimbobbennett and on github.

29 comments

Avatar

I’ve been using a similar sort of setup with CLion on PC, but lately more using QT Creator directly on the 400.

Avatar

When VS Code is first run there is a link to opt out of Telemetry.

Avatar

Jim Bennett can you provide an answer. Yes or no, is the RPi Foundation allowed to host the Visual Studio packages themselves with all the telemetry disabled by default, so they can downloaded directly from raspberrypi.org?
Thanks.

Avatar

I’ve found the remote VSCode installation on the Pi uses a lot of RAM.

Avatar

Unfortunately yeah – it uses a reasonable amount. This is why it hasn’t been ported to the older/smaller devices. For example I’d love to see support on the Zero W, but it just doesn’t have enough RAM.

Avatar

There is a way to do remote debugging using VS Code on a Raspberry Pi Zero W. I have written an indepth article on how to set it up here: Python Remote Debugging with Visual Studio Code and Raspberry Pi
https://www.linkedin.com/pulse/python-remote-debugging-visual-studio-code-raspberry-pi-mircea-dogaru

Avatar

Running ssh extension with a Pi3 with 1GB RAM causes the Pi to lockup after a while. I see the extensions using about 20% of the RAM which increases until none left. Makes it rather unusable. Good on a Pi with more RAM but I wonder if there is a memory leak causing it to gradually eat all the RAM

Avatar

Note, that for remote Python development you can also use Thonny (Run => Select interpreter => Remote Python 3 (SSH))

Avatar

… if you are using the (default) simple mode, then click on the button in the lower-right corner of Thonny’s main window, and select “Configure interpreter => Remote Python 3 (SSH)”

Avatar

I have been using this for several months now and I love it. It gives me almost everything I need/want with my pi.
But I have one question for the experts.
Is there a way to view or edit files that would, on the command line, require using sudo?

Avatar

I’ve found the NppFTP extension for Notepad++ to be very reliable for remote file viewing and editing on my Pi Zeros. The only problem I have with it is the white space / tab in python programs is not equivalent to 4 spaces, which occasionally trips me up.

Avatar

Go to Settings – Preferences – Language and select Replace by space to get spaces from the tab key.
I too use Notepad++ all the time to wrk on files on the pi.

Avatar

I try to install Visual studio on my RPi3. I type “sudo apt install code -y” and I get “cannot locate package “code” ???

Avatar

Raspberry Pi Operating System fully updated ?
sudo apt update
sudo apt full-upgrade
sudo reboot

Avatar

Microsoft Visual Studio Code is proprietary software with telemetry enabled by default.
You can censor all you want, but you cannot stop the truth from coming out.

Avatar

Check out VSCodium. Its the VSCode open sourced code compiled with all telemetry removed.

Avatar

I stopped using MS software at home, I’m not particularly fond of MS software, I know they use a lot of telemetry. But can we please stop the whole “MS is Evil, stop using VS Code” thing? I’ve used a fair amount of IDE in the past, and I love VS Code. It is the only piece of Microsoft software I use. Telemetry or not (it does have an opt-out setting, and it does show how to set it on first install), for my coding sessions, it is the best IDE I’ve used. So let’s stop filling the Pi forums with this nonsense conversation, and start focusing on praising our beloved littte computer. Don’t like VS Code? Don’t use it. Just stop whining about it :-)

Avatar

I did not want to use it. And I never intended to download it. But the Microsoft repo was forced on my PI to check for it anyway. There are countless others who do not know right now that is happening to their Pi as well.

Avatar

Be aware that this is only possible on later versions of the RPI.
You will get the following in the vscode logs on older Pis

> Unsupported architecture: armv6l

Avatar

just to clarify mohclips’ comment;
you can’t do this on a pi zero/ zero W. it has older architecture. (arm6, not the arm7 required architecture)
there is no plan to backport it either, as the zero doesn’t have enough RAM to support the vs code env.

https://github.com/microsoft/vscode-remote-release/issues/669

Avatar

This is absolutely amazing, I’m going to try it right now!!

Avatar

Hi Ashley,
Thank-you for this blog post. It’s very clearly written & informative! Is it possible to set up a SSH key from a Windows 10 development machine to the Raspberry Pi so that you don’t have to enter a password each time you connect?
Actually, I was able to get it working, but if you’re up to the task, maybe you or someone could “check my homework?”
Here’s the steps to try to set up a ssh key from win 10 to a Raspberry Pi I named “RPi4-1”:
In powershell, type:
ssh-keygen
File on which to save the key: C:\Users\\.ssh\ (make sure the backslashes are used throughout the file location)
Don’t use a passphrase because VS Code will simply require you enter it each time.
To copy the key over (for the first time):
cat $env:USERPROFILE\.ssh\.pub | ssh pi@ “mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys”
(If you’re doing it a 2nd time or you are OK with overwriting, use this:)
type $env:USERPROFILE\.ssh\.pub | ssh pi@ “cat >> .ssh/authorized_keys”

Thanks for any ideas or clarifications. I haven’t done something like this for a while, so I’m open to constructive criticism. My use of might interfere with >> commands.

Avatar

Whoops!
I meant to write…
File on which to save the key: C:\Users\\.ssh\
(make sure the backslashes are used throughout the file location)

Avatar

After read all replies here, I didn’t find anyone met the problem that I have.
The symptom is exact the same as the issues described at GitHub. https://github.com/microsoft/vscode-remote-release/issues/2145
https://github.com/microsoft/vscode-remote-release/issues/857
I wonder how you guys fix this issue?
Rpi 4 with Respbian buster :
“`
$ uname -a
Linux raspberrypi 5.10.17-v8+ #1403 SMP PREEMPT Mon Feb 22 11:37:54 GMT 2021 aarch64 GNU/Linux
$
$ ls -l /home/pi/.vscode-server/bin/f30a9b73e8ffc278e71575118b6bf568f04587c8/node
-rwxr-xr-x 1 pi pi 47216952 Mar 4 17:37 /home/pi/.vscode-server/bin/f30a9b73e8ffc278e71575118b6bf568f04587c8/node
$
$ /home/pi/.vscode-server/bin/f30a9b73e8ffc278e71575118b6bf568f04587c8/node out/vs/server/main.js
-bash: /home/pi/.vscode-server/bin/f30a9b73e8ffc278e71575118b6bf568f04587c8/node: No such file or directory
`

Avatar

an alternative for the pi zero is to use winSCP to open the file system on the pi, and set vs code as the external editor. ok, you still need a putty connection to send commands, but you get the benefit of editing the files remotely.

Avatar

Really enjoying using VSCode on the Pi in this way, but the installation didn’t work straight away for me. Running VSCode in Windows 10, after installing the remote development extension, and connecting, I didn’t get the prompts to choose the type of machine “Linux” and enter the remote host password. The connection appeared to hang but would error if I navigated away from VSCode. I used “Remote-SSH: Kill VS Code Server on Host” in the command palette and then connected using “Remote SSH: Connect current window to host” which worked! Might help someone who gets the same problem.

Avatar

Is it possible that the files im editing on the pi also are saved on my pc. at the moment i have to copy paste it and save it on that way.

Leave a Comment

Comments are closed