Programming Raspberry Pi Remotely using VS Code (Remote-SSH)

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.

Raspberry Pi: Create, Code, and Execute Python Files Remotely using VS Code Remote SSH

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.

Installing Remote - SSH Extension on VS Code

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.

Create Remote Connection with the Raspberry Pi on VS Code

Then, you’ll be asked to run the SSH command to establish a communication with your host.

SSH command raspberry Pi VS Code

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.

SSH configuration file VS Code Raspberry Pi

You’ll get a message saying the host was added.

New host added to VS Code Remote

On the top left corner, click on the Refresh icon next to Remote.

Refresh remote connections Vs Code

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.

New Remote connection on new window VS Code

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.

insert password for remote connection

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.

Open new folder on remote machine SSH VS Code

Choose a folder on your Raspberry Pi where you want to start creating your files.

Select Remote folder Raspberry Pi on VS Code SSH

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.

Select folder for Remote connection SSH Raspberry Pi

Insert your Raspberry Pi password again.

Enter SSH host password on VS Code

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.

Trust authors of the Pi folder Vs Code remote connection with Raspberry Pi

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.

VS Code Workspace on Remote Raspberry Pi

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.

Create a New Python File in the Raspberry Pi Remotely using VS Code

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.

New file create on remote connection Raspberry Pi SSH

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")
Python File on VS Code Raspberry Pi Remote connection

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

Run file on Raspberry Pi from VS Code Remote SSH connection

Closing the Remote Connection with Raspberry Pi on VS Code

To close the remote session, go to File > Close Remote Connection.

Close Remote Connection on VS Code

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.



Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »
Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »

Enjoyed this project? Stay updated by subscribing our newsletter!

17 thoughts on “Programming Raspberry Pi Remotely using VS Code (Remote-SSH)”

  1. 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.

    Reply
    • 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.

      Reply
  2. 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!

    Reply
  3. 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!

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

      Reply
  4. 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.

    Reply
  5. 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

    Reply

Leave a Comment

Download Our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.