> For the complete documentation index, see [llms.txt](https://education.raspberrypiaustralia.online/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://education.raspberrypiaustralia.online/using-visual-studio-code-with-remote-ssh-to-program-raspberry-pi.md).

# Using Visual Studio Code with Remote SSH to Program Raspberry Pi

Visual Studio Code (VS Code) is a powerful, lightweight, and cross-platform code editor that provides a rich set of features for development.&#x20;

One of its most useful features is the ability to use the <mark style="color:blue;">Remote - SSH extension</mark> to connect to a remote machine, such as a Raspberry Pi, and work with files and folders on that machine as if they were local.&#x20;

This allows you to take full advantage of VS Code's development capabilities while working on a Raspberry Pi.

<figure><img src="/files/Z3kzC2JV5T6uILXzxLTY" alt=""><figcaption></figcaption></figure>

<mark style="color:green;">Prerequisites Before you begin, ensure that you have the following:</mark>

* A Raspberry Pi with a compatible operating system (e.g., Raspberry Pi OS) and SSH server installed and running.
* A computer with Visual Studio Code installed.
* The Remote - SSH extension installed in VS Code.
* An SSH client installed on your computer (e.g., OpenSSH).

#### <mark style="color:green;">Step 1: Configuring the Raspberry Pi</mark>

Enable SSH on your Raspberry Pi if it's not already enabled. You can do this by running the following command in the terminal:

```bash
sudo raspi-config
```

Navigate to "Interfacing Options" and select "SSH" to enable it.

1. Ensure that your Raspberry Pi is connected to the same network as your computer.
2. Note down the IP address of your Raspberry Pi. You can find it by running the following command in the terminal:

```bash
hostname -I
```

#### <mark style="color:green;">Step 2: Connecting to the Raspberry Pi from VS Code</mark>

1. Open Visual Studio Code on your computer.
2. Click on the Remote Explorer icon in the left sidebar or press `F1` and select "Remote-SSH: Connect to Host...".
3. Enter the SSH connection details in the following format:

```bash
pi@<Raspberry_Pi_IP_Address>
```

Replace <mark style="color:yellow;">**`<Raspberry_Pi_IP_Address>`**</mark> with the IP address of your Raspberry Pi.

1. VS Code will establish an SSH connection to your Raspberry Pi. You may be prompted to enter the password for the 'pi' user (default password is 'raspberry').
2. Once connected, VS Code will set up the SSH host and open a new window. You are now connected to your Raspberry Pi via SSH!

#### <mark style="color:green;">Step 3: Managing Files and Folders</mark>

1. In the new VS Code window, you can open any folder on the Raspberry Pi by selecting "File" > "Open Folder" from the menu or by pressing `Ctrl+K Ctrl+O` (Windows/Linux) or `Cmd+K Cmd+O` (macOS).
2. Navigate to the desired folder on your Raspberry Pi and click "OK".  VS Code will open the selected folder, and you can start working with the files and folders as if they were local.
3. You can create new files and folders, edit existing ones, and perform various operations just like you would in a local VS Code environment.

#### <mark style="color:green;">Step 4: Installing Extensions</mark>

1. While connected to your Raspberry Pi, you can install VS Code extensions that will run remotely on the Pi.
2. Open the Extensions view by clicking on the Extensions icon in the left sidebar or by pressing `Ctrl+Shift+X` (Windows/Linux) or `Cmd+Shift+X` (macOS).
3. Search for the desired extension and click on the "Install" button. The extension will be installed on the Raspberry Pi.
4. Some extensions may require additional setup or configuration. Refer to the extension's documentation for specific instructions.

#### <mark style="color:green;">Step 5: Running and Debugging Code</mark>

1. With the Remote - SSH connection established, you can run and debug your code directly on the Raspberry Pi.
2. Open the file you want to run or debug.
3. Set breakpoints (if needed) by clicking on the gutter (the space next to the line numbers) or by using the "Run" view in the left sidebar.
4. Press `F5` or select "Run" > "Start Debugging" from the menu to start debugging. VS Code will use the appropriate debugger based on the file type and launch configuration.
5. You can step through your code, inspect variables, and perform other debugging tasks just as you would when running the code locally.

#### <mark style="color:green;">Step 6: Accessing the Terminal</mark>

1. VS Code provides an integrated terminal that allows you to run commands on your Raspberry Pi.
2. Open the terminal by selecting "Terminal" > "New Terminal" from the menu or by pressing `` Ctrl+` `` (backtick).
3. The terminal will open, and you can run commands on your Raspberry Pi as if you were connected via SSH in a regular terminal.

#### <mark style="color:green;">Step 7: Port Forwarding</mark>

1. If your Raspberry Pi application uses specific ports, you can forward those ports to your local machine using the Remote - SSH extension.
2. Open the "Ports" view in the bottom panel of VS Code or run the command "Ports: Focus on Ports View".
3. Click on the "Forward a Port" button.
4. Enter the desired port number and a name for the forwarded port.
5. VS Code will establish the port forwarding, and you can access the Raspberry Pi application using the forwarded port on your local machine.

#### <mark style="color:green;">Step 8: Disconnecting and Reconnecting</mark>

1. To disconnect from the Raspberry Pi, simply close the VS Code window that is connected via SSH.
2. To reconnect, follow the steps in "Step 2: Connecting to the Raspberry Pi from VS Code" again.

#### <mark style="color:green;">Tips and Tricks</mark>

* You can set up SSH key-based authentication to avoid entering the password every time you connect.
* If you have multiple Raspberry Pis or frequently connect to the same Pi, you can set up an SSH config file to store the connection details and easily connect using a custom hostname.
* VS Code's settings are synced between your local machine and the remote Raspberry Pi, ensuring a consistent development experience.
* You can use the integrated version control features of VS Code to manage your code repositories on the Raspberry Pi.

### <mark style="color:blue;">Conclusion</mark>

Using Visual Studio Code with Remote SSH provides a powerful and convenient way to develop and debug applications on a Raspberry Pi.&#x20;

With the ability to access files, install extensions, run and debug code, and use the integrated terminal, VS Code offers a seamless development experience as if you were working locally on the Raspberry Pi.

By following the steps outlined in this guide, you can easily set up and use VS Code with Remote SSH to program your Raspberry Pi efficiently and effectively.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://education.raspberrypiaustralia.online/using-visual-studio-code-with-remote-ssh-to-program-raspberry-pi.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
