Although the Raspberry Pi is a powerful computing device, it performs best when it focuses on one task at a time. Running a browser, code editor, and other programs quickly consumes the limited RAM on the device. A better workflow is to develop on another computer and run the project on the RPi.
In this tutorial, we’ll set up the RPi so it can be accessed and programmed remotely from another device on the same network.
What You Will Learn
In this tutorial you will learn how to:
- Enable SSH on your Raspberry Pi
- Connect to your Pi from another computer
- Transfer files using SFTP
- Automatically upload code using VS Code
- Run your first Python program remotely
By the end, you will have a development workflow that lets you program your Raspberry Pi from anywhere on your network.
Disclaimer
I use a MacBook Air as my primary home device. While I will focus on agnostic methods (Universal methods no matter the Operating System of your home device) I am sure I will elicit some bias and forget to point out differences. I will do my best to not forget, but our community can help.
Requirements
- A RPi Zero W2 or higher that is connected to your home network.
- Just about any other computer, even another RPi on the same network.
- The SSID and password for your home network.
- Bonus: a phone on the same network
NOTE: It is possible to connect to a RPi on another network. This tutorial will focus on all devices on the same network.
Step 1 Prepare the RPi
Enable SSH
SSH stands for secure shell. It is a way to securely connect to and control another computer over a network using a terminal. It is absolutely required to work remotely.
- Open a terminal on your Pi
- Run sudo raspi-config
- Navigate to Interface Options → SSH → Enable
To Change the Name (Hostname) of Your RPi
💡 Tip
If you are like me and have several RPis, do yourself a favor and change the name (hostname) of the device so that it is identifiable by task, or Pi model. For example, I typically use this naming convention RPiX-Task, where X is Pi model (2, 3, 4, or 5) and Task is the single use task I am developing for (Calendar, Clock, Weather, etc.).
- Opn a terminal on your Pi
- Run sudo raspi-config
- Navigate to System Options → Hostname
- Enter your new hostname
- Select OK, exit the menu, and reboot when prompted.
We also recommend changing your username and password on all RPis. Usernames like Pi and admin are predictable. Choose something you can remember easily but moves you away from predictable patterns. Knowing your username or password is half the battle for someone with nefarious intent. In fact, your username is relatively easy to find, so be thoughtful about changing your password.
To Change the Username
- Open a terminal on your Pi
- Run sudo adduser newUserName, replace newUserName with your custom name
- Run sudo usermod -aG sudo newUserName to add this user to the sudo group
- During this you should also be prompted to create a password, if you do this you do not need to change the password below.
To Change the Password
- Open a terminal on your Pi
- Run passwd
- You will be prompted for your Current Password, New Password, and New Password Confirmation
Dynamic versus Static IP Address for your RPi
Most home networks assign dynamic IP addresses, which means the address may change each time the device boots.
While it is possible to configure a static IP, this can create conflicts if another device uses the same address.
For most Raspberry Pi projects, connecting using the hostname (hostname.local) is easier than worrying about the IP address.
Identify your Current IP Address
From the RPi
- Open a terminal on your Pi
- Run hostname -I
- Write down the IP Address
From a Mobile Device
I’ve found it very useful to be able to use my phone to identify the IP address of any given device. There are lots of apps that will do this, but I prefer Network Analyzer, you can find it on Android and Apple App Stores. It is free and does one thing super well. It will scan the same network your phone is on (make sure it’s on the same network as your Pi) and provide the IP address of all connected devices. If you use this method, scan the network and write down the IP address. The RPi will be identifiable by the hostname you chose earlier.
Step 1 - Closing
These steps prepare your RPi (and you) to connect remotely. Next we’ll jump onto your home device and connect.
Step 2 Connect with SSH
Now you are set. You can connect to your RPi from literally any other device that can ssh, including phones. Using a mobile device can be useful at times, but we will focus on using a desktop or laptop.
It is important to remind you again that both the RPi and your home device need to be on the same network.
From your home device:
- Open a terminal
- Run ssh username@hostname.local, where username is your username and hostname is the name we set up for the Pi. This keeps you from having to remember (or check) for the dynamic IP address. The .local address works because Raspberry Pi OS uses mDNS (multicast DNS), which allows devices on the same network to discover each other by hostname. However, this may not resolve or you may need to be specific with your IP address. If that is the case, use:
- Run ssh username@ipaddress, where ipaddress is the address you recorded above. The IP address should look something like 192.168.1.42, four numbers separated by 3 periods.
- You will be prompted to enter your password
- You should now see a prompt like this: username@hostname:~ $
Step 2 Closing
So now we’re connected to the RPi from your home device. But, how do we get files from your home device to the RPi? That’s where we will go next.
Step 3 Transfer Files
There are several ways to transfer files to your Raspberry Pi:
- Edit files directly on the Pi using Nano (a built in, command line editor)
- Copy files using the command line with scp
- Use an SFTP (Secure File Transfer Protocol) client like FileZilla
- Automatically upload files from a code editor like VS Code
Option 4 is the most useful workflow in my personal opinion. If it keeps me from opening my FTP app after every edit, then why not save a few moments? Those seconds add up to minutes when developing a medium to large project. Visual Studio Code (VS Code) is the current popular editor, but not the only tool that can SFTP on save. Similar steps could be followed to set it up. And lastly, following these steps will also allow you to SFTP to the device.
Why this Matters
I prefer to have copies of my files on my home device to preserve as masters, then copy them to the RPi. This allows me to keep a clean copy of all my RPis projects on my home device and/or set up repositories from my home device. This is a much more convenient and clean workflow.
Option 1 - Set Up an SFTP Client
- Choose your S/FTP App.
- Create a new connection.
- Enter the following information:
- Protocol: SFTP
- Hostname: your_hostname (name of Pi OR IP address)
- Port: 22
- Username: your_username
- Password: your_password
- Home directory: file path on home device
- Remote directory: file path on RPi, I recommend creating a folder specific to the project. This keeps your file system clean.
- Test the connection.
If it fails, try the actual IP address. If it still fails, double check your username and password. And lastly, check your directory paths (go to the folder and copy the path).
Try uploading a file. Confirm it is there, by logging into the Pi, navigating to the folder and running ls
Once connected with SFTP, you can manage files on your RPi just like you would on your own computer: uploading, downloading, renaming, and deleting files through the interface.
Option 2 - Set Up VS Code to Upload on Save (Preferred Method)
- Open VS Code and install the extension called SFTP by Natizyskunk
- Open VS Code
- Click the Extensions icon on the left sidebar
- Search for SFTP
- Click Install
- In VS Code open the folder on your home device where you will store the RPi project
- File >> Open Folder
- Navigate to the Folder
- Click Open
- Open SFTP Config
- Press F1
- Type SFTP: Config
- VS Code will create a file called .vscode/sftp.json VS Code stores project-specific settings in a hidden folder called .vscode. The SFTP extension will create the configuration file inside this folder.
- Overwrite the contents with the following:
{
"name": "RaspberryPi",
"host": "hostname OR IP address",
"protocol": "sftp",
"port": 22,
"username": "your_username",
"remotePath": "file_path_on_pi",
"uploadOnSave": true
}
Create a simple test file in VS Code. Save it. In the bottom left of VS Code, you should see a loading status message. From your command prompt, navigate to the folder on your Pi and run ls you should see the file in the folder.
Step 4 Run Your First Program
- In VS Code create a new file called hello.py Copy and paste the contents below into the file.
#!/usr/bin/env python3
def main():
print("Hello from your Raspberry Pi!")
if __name__ == "__main__":
main()
- Save the file.
- From your SSH terminal run python3 hello.py
- Expected output: Hello from your Raspberry Pi!
That’s it. If you see the message printed in the terminal, your development environment is working correctly and you’re ready to start writing programs for your RPi.
You have now successfully prepared your Pi, connected to it from a home device, uploaded home device code to it, and run it successfully.
Developing remotely means you can sit on the couch with my laptop while the Raspberry Pi and display are running across the room. You never have to get up just to edit a line of code.
If you want to go one step further, you can run the Raspberry Pi completely headless.
BONUS - Run the Pi Headless
So, you made it through the tutorial and like this so much you’re ready to commit to this workflow? Going headless might scare you because you like the fail safe of being able to navigate visually using the GUI if all else fails. However, it doesn’t have to be permanent, you can always bring it back temporarily or permanently again. But most importantly, YOU DON’T NEED IT! You can do anything from the command line! Anything! So don’t worry, buckle in, and let’s give it a try.
- Open a terminal on the Pi
- Run sudo raspi-config
- Navigate to System Options → Boot / Auto Login
- Select Console or Console Autologin
- Reboot the Pi
This will boot to command-line mode only.
Why this Matters
Running headless has several advantages for project builds:
- Uses less memory
- Faster startup
- More CPU resources available for programs
- Ideal for embedded projects like LED displays
Many RPi projects run this way because the Pi is often mounted inside a device where a monitor is not connected.
If You Ever Need the Desktop Again
- If the desktop environment is installed, you can temporarily start it from the command-line run startx
To Change the Boot Behavior Back Permanently
- Open a terminal on the Pi
- Run sudo raspi-config
- Navigate to System Options → Boot / Auto Login
- Select Desktop or Desktop Autologin
- Reboot the Pi