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.

21 comments
Jump to the comment form

Avatar

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

Reply to Anders

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.

Reply to Caleb

Avatar

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

Reply to Lewis Walsh

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.

Reply to Jim Bennett

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

Reply to 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

Reply to Walt

Avatar

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

Reply to Aivar Annamaa

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)”

Reply to Aivar Annamaa

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?

Reply to Charles Godwin

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.

Reply to Jon

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.

Reply to Ross

Avatar

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

Reply to Dean

Avatar

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

Reply to MW

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.

Reply to Tom

Avatar

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

Reply to John Vaccaro

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 :-)

Reply to Mário Almeida

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.

Reply to Ludwig

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

Reply to Mohclips

Leave a Comment