Page cover image

Using Visual Studio Code with Remote SSH to Program Raspberry Pi

Using the Password Approach

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

One of its most useful features is the ability to use the Remote - SSH extension 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.

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

Prerequisites Before you begin, ensure that you have the following:

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

Step 1: Configuring the Raspberry Pi

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

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:

hostname -I

Step 2: Connecting to the Raspberry Pi from VS Code

  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:

pi@<Raspberry_Pi_IP_Address>

Replace <Raspberry_Pi_IP_Address> 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!

Step 3: Managing Files and Folders

  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.

Step 4: Installing Extensions

  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.

Step 5: Running and Debugging Code

  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.

Step 6: Accessing the Terminal

  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.

Step 7: Port Forwarding

  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.

Step 8: Disconnecting and Reconnecting

  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.

Tips and Tricks

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

Conclusion

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

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.

Last updated

Was this helpful?