0% found this document useful (0 votes)
11 views38 pages

Remote Raspberry Pi Programming with VS Code

Remotely using VS Code (Remote-SSH) _Random Nerd Tutorials

Uploaded by

Catalin Pancescu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views38 pages

Remote Raspberry Pi Programming with VS Code

Remotely using VS Code (Remote-SSH) _Random Nerd Tutorials

Uploaded by

Catalin Pancescu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

6/20/23, 8:06 PM Programming Raspberry Pi Remotely using VS Code (Remote-SSH) | Random Nerd Tutorials

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.

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:

Getting Started with Raspberry Pi


[Link] 1/38
6/20/23, 8:06 PM Programming Raspberry Pi Remotely using VS Code (Remote-SSH) | Random Nerd Tutorials

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.

[Link] 2/38
6/20/23, 8:06 PM Programming Raspberry Pi Remotely using VS Code (Remote-SSH) | Random Nerd Tutorials

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: [Link]
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 pi@[Link] 

[Link] 3/38
6/20/23, 8:06 PM Programming Raspberry Pi Remotely using VS Code (Remote-SSH) | Random Nerd Tutorials

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.

[Link] 4/38
6/20/23, 8:06 PM Programming Raspberry Pi Remotely using VS Code (Remote-SSH) | Random Nerd Tutorials

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

[Link] 5/38
6/20/23, 8:06 PM Programming Raspberry Pi Remotely using VS Code (Remote-SSH) | Random Nerd Tutorials

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.

[Link] 6/38
6/20/23, 8:06 PM Programming Raspberry Pi Remotely using VS Code (Remote-SSH) | Random Nerd Tutorials

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.

[Link] 7/38
6/20/23, 8:06 PM Programming Raspberry Pi Remotely using VS Code (Remote-SSH) | Random Nerd Tutorials

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
[Link].

[Link] 8/38
6/20/23, 8:06 PM Programming Raspberry Pi Remotely using VS Code (Remote-SSH) | Random Nerd Tutorials

It will create and open a new Python file called [Link] 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")

[Link] 9/38
6/20/23, 8:06 PM Programming Raspberry Pi Remotely using VS Code (Remote-SSH) | Random Nerd Tutorials

Save your file. To save your file you can simply press Ctrl+S or Cmd+S.

Now, you have a file called [Link] 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 [Link]

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

[Link] 10/38
6/20/23, 8:06 PM Programming Raspberry Pi Remotely using VS Code (Remote-SSH) | Random Nerd Tutorials

Closing the Remote Connection with Raspberry Pi


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

[Link] 11/38
6/20/23, 8:06 PM Programming Raspberry Pi Remotely using VS Code (Remote-SSH) | Random Nerd Tutorials

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

[Link] 12/38
6/20/23, 8:06 PM Programming Raspberry Pi Remotely using VS Code (Remote-SSH) | Random Nerd Tutorials

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:

Getting Started with VS Code and PlatformIO IDE for ESP32 and ESP8266
(Windows, Mac OS X, Linux Ubuntu)

Thanks for reading.

[eBook] Build Web Servers with ESP32 and


ESP8266 (2nd Edition)

Build Web Server projects with the ESP32 and ESP8266 boards to control outputs and
monitor sensors remotely. Learn HTML, CSS, JavaScript and client-server
communication protocols DOWNLOAD »

Recommended Resources 

[Link] 13/38
6/20/23, 8:06 PM Programming Raspberry Pi Remotely using VS Code (Remote-SSH) | Random Nerd Tutorials

Build a Home Automation System from Scratch » With Raspberry Pi, ESP8266,
Arduino, and Node-RED.

Home Automation using ESP8266 eBook and video course » Build IoT and
home automation projects.

Arduino Step-by-Step Projects » Build 25 Arduino projects with our course, even
with no prior experience! 

[Link] 14/38
6/20/23, 8:06 PM Programming Raspberry Pi Remotely using VS Code (Remote-SSH) | Random Nerd Tutorials

What to Read Next…

ESP8266 NodeMCU: Create a Wi-Fi Manager (AsyncWebServer library)

ESP32 HTTP POST with Arduino IDE (ThingSpeak and [Link])

[Link] 15/38
6/20/23, 8:06 PM Programming Raspberry Pi Remotely using VS Code (Remote-SSH) | Random Nerd Tutorials

Getting Started with VS Code and PlatformIO IDE for ESP32 and ESP8266
(Windows, Mac OS X, Linux Ubuntu)

Enjoyed this project? Stay updated by subscribing our


newsletter!
Your Email Address

SUBSCRIBE

10 thoughts on “Programming Raspberry Pi Remotely


using VS Code (Remote-SSH)”

Dan
February 9, 2023 at 3:05 pm

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 

[Link] 16/38
6/20/23, 8:06 PM Programming Raspberry Pi Remotely using VS Code (Remote-SSH) | Random Nerd Tutorials

Control systems (git). I will be investigating how to use PyCharm with a


remote SSH.
Thanks.

Reply

Dan
February 9, 2023 at 4:29 pm

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

graham
February 9, 2023 at 5:00 pm

excellent work, thank you.

Reply

Sara Santos
February 9, 2023 at 7:54 pm

Thanks 😀

[Link] 17/38
6/20/23, 8:06 PM Programming Raspberry Pi Remotely using VS Code (Remote-SSH) | Random Nerd Tutorials

Reply

Evan Suits
February 9, 2023 at 5:12 pm

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

Sara Santos
February 9, 2023 at 7:54 pm

Great!
I’m glad this is useful.
Regards,
Sara

Reply

[Link] 18/38
6/20/23, 8:06 PM Programming Raspberry Pi Remotely using VS Code (Remote-SSH) | Random Nerd Tutorials

George
February 13, 2023 at 6:43 pm

It’s possible connect VS Code with my PiZero 1.1 ?


Tks.
G.

Reply

Sara Santos
February 18, 2023 at 10:14 am

Hi.
Yes. I think it should be possible as long as SSH is enabled on your Pi.
Regards,
Sara

Reply

Joseph McCarron
March 4, 2023 at 2:41 pm

You guys have the most useful information, thanks

Reply

[Link] 19/38
6/20/23, 8:06 PM Programming Raspberry Pi Remotely using VS Code (Remote-SSH) | Random Nerd Tutorials

Sara Santos
March 5, 2023 at 12:15 am

Thanks 😀

Reply

Leave a Comment

Name *

Email *

Website

Notify me of follow-up comments by email.

Notify me of new posts by email.

Post Comment

[Link] 20/38
6/20/23, 8:06 PM Programming Raspberry Pi Remotely using VS Code (Remote-SSH) | Random Nerd Tutorials

Visit Maker Advisor – Tools and Gear for


makers, hobbyists and DIYers »

Home Automation using ESP8266 eBook


and video course » Build IoT and home
automation projects.

Build Web Servers with ESP32 and


ESP8266 » boards to control outputs and
monitor sensors remotely.

[Link] 21/38
6/20/23, 8:06 PM Programming Raspberry Pi Remotely using VS Code (Remote-SSH) | Random Nerd Tutorials

[Link] 22/38
6/20/23, 8:06 PM Programming Raspberry Pi Remotely using VS Code (Remote-SSH) | Random Nerd Tutorials

[Link] 23/38
6/20/23, 8:06 PM Programming Raspberry Pi Remotely using VS Code (Remote-SSH) | Random Nerd Tutorials

[Link] 24/38
6/20/23, 8:06 PM Programming Raspberry Pi Remotely using VS Code (Remote-SSH) | Random Nerd Tutorials

[Link] 25/38
6/20/23, 8:06 PM Programming Raspberry Pi Remotely using VS Code (Remote-SSH) | Random Nerd Tutorials

[Link] 26/38
6/20/23, 8:06 PM Programming Raspberry Pi Remotely using VS Code (Remote-SSH) | Random Nerd Tutorials

[Link] 27/38
6/20/23, 8:06 PM Programming Raspberry Pi Remotely using VS Code (Remote-SSH) | Random Nerd Tutorials

[Link] 28/38
6/20/23, 8:06 PM Programming Raspberry Pi Remotely using VS Code (Remote-SSH) | Random Nerd Tutorials

[Link] 29/38
6/20/23, 8:06 PM Programming Raspberry Pi Remotely using VS Code (Remote-SSH) | Random Nerd Tutorials

[Link] 30/38
6/20/23, 8:06 PM Programming Raspberry Pi Remotely using VS Code (Remote-SSH) | Random Nerd Tutorials

[Link] 31/38
6/20/23, 8:06 PM Programming Raspberry Pi Remotely using VS Code (Remote-SSH) | Random Nerd Tutorials

[Link] 32/38
6/20/23, 8:06 PM Programming Raspberry Pi Remotely using VS Code (Remote-SSH) | Random Nerd Tutorials

[Link] 33/38
6/20/23, 8:06 PM Programming Raspberry Pi Remotely using VS Code (Remote-SSH) | Random Nerd Tutorials

[Link] 34/38
6/20/23, 8:06 PM Programming Raspberry Pi Remotely using VS Code (Remote-SSH) | Random Nerd Tutorials

[Link] 35/38
6/20/23, 8:06 PM Programming Raspberry Pi Remotely using VS Code (Remote-SSH) | Random Nerd Tutorials

[Link] 36/38
6/20/23, 8:06 PM Programming Raspberry Pi Remotely using VS Code (Remote-SSH) | Random Nerd Tutorials

[Link] 37/38
6/20/23, 8:06 PM Programming Raspberry Pi Remotely using VS Code (Remote-SSH) | Random Nerd Tutorials

About Support Terms and Conditions Privacy Policy Refunds Complaints’ Book

[Link] Join the Lab

Copyright © 2013-2023 · [Link] · All Rights Reserved

Update Privacy Settings

[Link] 38/38

You might also like