In this tutorial, we’ll show you a practical way to create and execute files remotely on your Raspberry Pi using VS Code on your computer. All you have to do is to install the Remote-SSH extension on VS Code. This extension allows you to establish an SSH connection with your Pi, create files, write code and execute it directly on your Raspberry Pi board from your computer using VS Code interface. This is the perfect solution if your Raspberry Pi is running headless.
Prerequisites
If you’re reading this post, we assume that you are familiar with or are getting started with the Raspberry Pi. You can read our getting started guide below:
We assume you have an operating system installed on your Pi (Raspberry Pi OS or similar), you have enabled SSH on the Pi, and you know your Pi hostname, username, and password — all of this is set up during the installation process.
We also assume you already have Visual Studio Code (VS Code) installed on your computer.
Installing Remote – SSH Extension on VS Code
First, you need to install the Remote – SSH extension on VS Code. Click on the Extensions icon on the left sidebar, search for remote, and install the Remote – SSH extension.
Create a Remote Connection with the Raspberry Pi on VS Code
After installing, a new Desktop icon will show up on your sidebar. Click on that icon. Then, select Remote on the Remote Explorer field. Then, Click on the + sign to add a new remote machine.
Then, you’ll be asked to run the SSH command to establish a communication with your host.
At this point, you need to know your Raspberry pi localhost name or IP address and username. For example, in my case:
- localhost: raspberrypi2
- IP address: 192.168.1.106
- username: pi
So, I need to write the following command on that field:
ssh pi@raspberrypi2
Or, if I wanted to use the IP address instead:
ssh [email protected]
Then, press Enter. You’ll be asked to select an SSH configuration file where VS code will save the SSH settings for that host. You can choose the one under the Users folder.
You’ll get a message saying the host was added.
On the top left corner, click on the Refresh icon next to Remote.
The Raspberry Pi remote host will show up under the SSH menu. Then, click on the icon to connect in a new window as shown below.
This will open a new window in VS Code dedicated to that remote machine (the Raspberry Pi). You’ll be asked to enter the Raspberry Pi password.
Now, you have a remote connection established with your Raspberry Pi. Notice that the Terminal window at the bottom is connected to your Raspberry Pi. Any commands that you run on that Terminal window, will be run on the Raspberry Pi.
You can open Raspberry Pi folders on VS Code to start creating and writing files. Click on the Open Folder button to open a folder on your Raspberry Pi where you want to start creating or editing your files.
Choose a folder on your Raspberry Pi where you want to start creating your files.
I chose the Documents folder, but you can choose any other location on the Raspberry Pi. The Desktop folder might also be a convenient location. After selecting the folder, click OK.
Insert your Raspberry Pi password again.
You’ll be asked if you trust the authors of the folder. Tick the option Trust the authors of all files in the parent folder ‘pi’, and click Yes, I trust the authors to proceed.
It will open the selected folder on the left sidebar. Now, you can use the VS Code menus to create new files and/or folders under that folder. To open the Terminal window again, go to Terminal > New Terminal.
Create a New Python File in the Raspberry Pi Remotely using VS Code
Once you’ve selected a working folder, you can create and write a new file in that folder. Hover your mouse over the “Documents” tab and click on the New File… button to create a new file. For demonstration purposes, you can create a file called test.py.
It will create and open a new Python file called test.py on the Documents folder of the Raspberry Pi filesystem. You can write to that folder using VS Code environment. You can also delete or move that folder from VS Code.
At this point, you can write your Python code on that file. For demonstration purposes, copy the following line to that file. This will simply print a message on the Terminal window when executed.
print("Hello from Raspberry Pi")
Save your file. To save your file you can simply press Ctrl+S or Cmd+S.
Now, you have a file called test.py on your Raspberry Pi Documents folder with that line of code.
Running a Python File on the Raspberry Pi Remotely using VS Code (SSH)
To run that file simply call the command python followed by the name of the file on the terminal window. In this case:
python test.py
Press Enter after typing the command.
You should get “Hello from Raspberry Pi” on the Terminal window, which is what that files does (prints a message).
Closing the Remote Connection with Raspberry Pi on VS Code
To close the remote session, go to File > Close Remote Connection.
Congratulations! Now, you know how to code on Raspberry Pi remotely using VS Code on your computer using the Remote – SSH extension.
Wrapping Up
In this tutorial, you learned a very convenient, practical, and easy way to program on your Raspberry Pi remotely via SSH using VS Code on your computer. Using the Remote – SSH extension you can create new files and folders on the Pi, and write and execute code using the command line.
This is our favorite method to program the Raspberry Pi, because you don’t have to manually transfer the files from your computer to the Pi. Using this method, you’re already writing the code on the Pi. Additionally, the Terminal window allows you to execute commands remotely via SSH on the Pi, which is very practical to run your code.
We hope you found this tutorial useful. If you also like to program boards like the ESP32 and ESP8266, you can also use VS Code for that. Learn how with the following tutorial:
You can check all our Raspberry Pi projects on the following link:
Thanks for reading.
I like the idea of remote programming and execution shown, but I find VS Code does not fit my style. (although, I do use it for C/C++/C#)
After reading this tutorial, I googled remote connection for PyCharm and it seems to have the same features. PyCharm is my IDE of choice for Raspberry Pi for its ease of use in setting up virtual environments and version Control systems (git). I will be investigating how to use PyCharm with a remote SSH.
Thanks.
Unfortunately, the remote connection facility is only in the Pro version of PyCharm. It is available for the Fleet IDE which is in test mode. I’ll try it there, but I imagine Fleet will also follow PyCharm and have a Pro version.
excellent work, thank you.
Thanks 😀
WOW!!! This couldn’t be more timely. I’ve just begun exploring headless development on RPi and Beaglebone Black, and was unhappily resigned to using nano.
VS Code has already simplified working with ESP32 and Arduino. Code may be the best developer tool Microsoft has ever shipped.
Grateful thanks for putting this together!
Great!
I’m glad this is useful.
Regards,
Sara
It’s possible connect VS Code with my PiZero 1.1 ?
Tks.
G.
Hi.
Yes. I think it should be possible as long as SSH is enabled on your Pi.
Regards,
Sara
Hi Sarah,
This guide worked great I had no issues connecting via ssh and programming my rasberry pie 4 with visual studio.
Thanks
You guys have the most useful information, thanks
Thanks 😀
Hi there!
You post is good and it brings very useful info.
By the other side, I’ve used this feature a few years ago, and no problems were reported by myself.
Today ?m connecting my RaspberryPi 3B+ with VSCode SSH Remote plugin, and I’m getting out of memory errors, because the server gets all the pi free memory to run…
Have you noticed it? How can I solve this problem? The host gows down in performance and it is impossoble to develop in this status… 🙁
Thanks!
Hi.
I use this strategy many times and that never happened to me.
Can you see where the Raspberry Pi is using its resources?
Regards,
Sara
Sara,
What version of VS are you running? I have VS 2022 and (1) there is no left side panel and (2) when I click on Extensions and search for ‘Remote’ it finds RemoteDebugLauncher. Everything for SSH seems to come from COZYROC; not Microsoft as shown in the tutorial.
Thank you for a lot of great products which have helped this old dog learn some new tricks.
Hi.
Take a look at the official page of the extension: https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-ssh
See if their instructions help you figure it out.
Regards,
Sara
Another great tutorial!!, yesterday I bought my first Raspberry Pi, a Raspberry Pi 5.
Today I learned how to connect and code with VS, next I will try using as BME280 to develop my VS coding skills.
Thank you both for great, easy to follow tutorials.
Kia Kaha
Stewart
Hi.
That’s great.
You can take a look at all our Raspberry Pi tutorials here: https://randomnerdtutorials.com/projects-raspberry-pi/
Regards,
Sara
Is there a possibility to write a tutorial on how to do this, but with Jupyter Notebook?
Thank you so much Sara. This is best tutorial to this theme I found. Great work Sara.
Thanks 😀
I’m trying to connect to my Raspberry Pi Zero W using the VCode (- a new user).
I can putty into the Pi from windows using pageant to establish an ssh connection using the pub key generated by the Raspberry Image Installer (& corresponding prvt key generated locally) which gets authenticated by providing the same pass code when logging in via putty.
However, when I try logging in from VCode (giving it the ssh@raspsberrypi credentials), it never asks for a password or phrase but asks for the hosts platform type (Linux, Windows or Mac) & then reports that it “Could not establish connection to ‘raspberrypi’: The remote architecture is not supported.’