Visual Studio Code is a lightweight, cross-platform, open source code editor. You can install extensions to support a wide range of programming languages, connect to cloud services such as Azure, and even access other devices remotely.
One of these extensions is the Remote Development extension pack, which allows you to run Visual Studio Code (VS Code) to remotely connect to and program another device
This is a helpful extension for Pi users, as you can run VS Code on your main laptop or desktop and remotely code on the Pi, with full file system access, editing, uploading, GPIO programming, and debugging.
Only Pis using ARMv7 and v8 are supported, so only Pi 3 and 4s are supported. Earlier Pi devices and Pi Zeros are not supported. If you would like support for these devices, please up-vote this issue on GitHub
First setup your Pi for headless access by following the headless setup guide.
To enable remote development in Visual Studio Code, install the Remote Development Extension pack.
-
Launch Visual Studio Code
-
Select the Extensions tab from the left hand menu, or select View -> Extensions
-
Search for
remote development
and install the Remote Development extension pack from Microsoft by selecting the Install button
-
From Visual Studio Code, launch the command palette
- On macOS, press command+shift+p
- On Windows or Linux, press ctrl+shift+p
-
Search for
Remote-SSH: Connect to Host
and select it -
Select + Add new SSH Host
-
Enter
pi@raspberrypi.local
as the SSH connection command, changing the hostname fromraspberrypi
if you changed it when doing the headless setup. -
Select the SSH configuration file to update. This will store the SSH connection details to make connection easier. There will be an option in your home folder, which will vary depending on your OS and user name, so select this.
-
Once the connection has been configured, a dialog will appear saying the host is configured. Select Connect from this dialog.
-
A new VS Code window will open to host the connection. Enter your password (default is
raspberry
)
Once the connection has been established, the next time Remote-SSH: Connect to Host is selected a new window will be opened and the password requested, there will be no need to configure it again.
Once you are connected, Visual Studio Code will be running as if it was installed on the Pi directly. If you open a folder, you will only be able to select folders on the Pi. The terminal will be a terminal session running on the Pi. If you want to copy files to the Pi, you can drag them from your file explorer/finder and drop them into the Visual Studio Code explorer window and they will be copied into the relevant folder, the same as if you were accessing a local folder.
If you want to use extensions on the Pi, such as the Python extension, these will need to be installed directly as the remote session does not include extensions that you may have installed on your laptop or desktop
For example - to install the Python extension, follow these steps:
-
Select the Extensions tab from the left hand menu, or select View -> Extensions
-
Search for
Python
and install the Python extension from Microsoft by selecting Install in SSH: raspberrypi.local.There are a number of Python extensions available, so ensure you install the one from Microsoft
-
A reload will be required, so select Reload required
-
Visual Studio will reload the window, and you will be asked for your Raspberry Pi password again, so enter it.
Once this extension is installed you will be able to debug python applications on the remote device from your local Visual Studio Code instance.