Skip to content

Instantly share code, notes, and snippets.

What would you like to do?

Raspberry Pi - Clean Install

A great set of video resources can be found here. Most of this setup process comes from these videos. This setup process should work fine for either a Pi 3 or a Pi Zero, but the peripherals needed will change depending on which you are setting up. If you would like to skip this entire setup process, and instead flash a pre-built image to your card that includes all of the setup detailed here, as well as a copy of OpenCV 3.3 and python virtualenvironments, check out this walkthrough instead.

Installing the OS

  1. Download the newest version of Raspbian from The Raspberry Pi Org.
  2. Download a copy of The Unarchiver to extract the Raspbian img file.
  3. Download a copy of Pi Filler from IvanX to easily flash the OS to the card.
  4. Right click on the OS zip file and open with The Unarchiver. Extracting the file to the same folder is fine.
  5. Once extracted, open up Pi Filler. Select your img file, insert your SD card, and continue through the prompts.
  6. Once your card is flashed, Pi Filler will automatically eject it. Remove your SD card and immediately plug it back in.
  7. Your SD card should mount as boot. Open up terminal and type nano /Volumes/boot/ssh. This will open an empty file in the nano text editor. Type anything in the file (such as "enabling ssh") and save the file by typing ctrl + x, y, ENTER. This will add the ssh file to the SD card, allowing you to SSH into your Pi on the first boot.
  8. Eject your card, remove it from the computer, and plug it into your Pi.

Note: If you are having issues with Pi Filler working properly, try using Apple Pi Baker instead. This will allow you to: 1. Prep for NOOBS (format your SD card for a Raspbian installation) 1. Restore Backup (Flash an .img file to your card) 1. If you want to at a later date, you can make a backup of your Pi image and even flash it to another SD card to use on a different Pi!

Basic Setup - Powering Up the Pi

Depending on your setup, you may follow one of two paths.

  • If your pi is equipped with an ethernet cable and you can plug it in directly, do that.
  • If you cannot plug in directly, you'll need a keyboard and external display.

Without an Ethernet Cable

Requirements: USB keyboard, external display, HDMI cable, (Micro USB to USB-A, HDMI to Micro HDMI for Pi Zero).

  1. Plug in your peripherals and power on the Pi. Once it boots, it will take you to the GUI Desktop and start a setup process.
    1. Setup your localization preferences.
    2. Change your password.
    3. Connect to wifi.
    4. Update Raspbian (this will take a while).
    5. Reboot the Pi when prompted.
    6. From the start menu (you can open this from a keyboard with the right command key or the windows key) navigate to Preferences > Raspberry Pi Configuration.
      1. Change the hostname to something distinct. This can help avoid conflict when running multiple Pis on the same network.
      2. Go to Interfaces and enable the Camera (if you'll be using it), SSH (should already be enabled), and VNC.
      3. In Performance, go ahead and bump up the GPU Memory as well.
      4. Finally, click OK and reboot your Pi.

At this point, since your Raspberry Pi is connected to the internet, you no longer need to use the attached display and keyboard. Feel free to disconnect your peripherals and follow the relevant steps below to continue setting up through ssh.

With an Ethernet Cable

Requirements: Ethernet cable, another computer (preferably a Mac)

  1. Plug your Pi into an ethernet cable and power it on. Give it about 30 seconds to boot.
  2. From another computer on the network, connect to your Pi via ssh.
    ssh pi@raspberrypi.local
    • Note: If you've already changed your Pi's hostname, use that here in place of raspberrypi
  3. You may be asked to confirm by typing yes and hitting Enter. You will then be asked to type your password. If you have not changed your password, use the default password raspberry.
  4. You may encounter a "known hosts" error. If so, rm ~/.ssh/known_hosts and try the ssh again.
  5. Once you are in the Pi, type sudo raspi-config to set it up.
  6. Change your default password to increase security.
  7. Go to Network Options:
    1. Change your Hostname to something distinct. This will come in handy if you have multiple Pi's running on the same network.
    2. Connect to wifi. For SSID, provide your wifi's name.
  8. Go to Localisation Preferences and choose your settings.
    1. For Locales, disable en_GB.UTF-8 and enable en_US.UTF-8
    2. Set your preferred timezone.
  9. Go to Interfacing.
    1. If you plan on using a camera, enable it now.
    2. SSH is already enabled, because you're using it...
    3. Enable VNC.
  10. Go to Advanced Options.
    1. Expand the filesystem.
    2. Set your resolution. I prefer mode 85.
  11. We want to update our Pi, but before we do so we should reboot for our changes to take effect. Select Finish and confirm that you would like to reboot. Your ssh session will be terminated, so wait a moment and then reconnect as before. Remember to replace raspberrypi with the new hostname that you set.

Fine Tuning the Setup


  1. Once back in, let's go ahead and make sure that our Pi is up to date. This may take a while if you haven't updated in a while (or ever).
    1. Run sudo apt-get update
    2. Run sudo apt-get dist-upgrade -y
    3. Run sudo apt-get upgrade -y
    • If either of the upgrades fail, try running update again and then retrying.
    • It is a good idea to periodically update your pi.

Setting Up VNC

  1. From your computer, download and install VNC Viewer from RealVNC. If so desired, you can also download VNC Viewer for phones and tablets.
  2. In the top bar, type your hostname appended with ".local". For example, raspberrypi.local.
  3. Click OK, then type the username "pi" and your password. Remember the password, then click OK to connect. If everything has been setup correctly, you should now see your Pi's desktop.
  4. Let's fix some resolution issues by executing sudo nano /boot/config.txt on the pi.
    1. If you already set the resolution via the raspi-config, you should see that you have hdmi_group=2 and hdmi_mode=85. We want to add hdmi_ignore_edid=0xa5000080 (this helps to optimize for computer screens rather than a TV).
    2. Exit and save, then reboot with sudo reboot. Once booted, reconnect via ssh. The VNC should reconnect automatically.
  5. By creating a RealVNC account you can connect to your Pi via a computer on a different network. This can be super useful and I encourage you to do it. It is free for individual use for up to 5 computers. From your computer, click "Sign in..." from the main VNC Viewer screen and click "Sign up online". Once you are signed up, you should be able to sign in.
  6. Next, connect to your Pi via the VNC. Click on the VNC button to the left of the bluetooth button in the upper right corner of the task bar.
  7. From the left of the window that pops up, click "Sign In" and enter your credentials. Choose "Direct and Cloud Connectivity" to enable remote access from external networks. You should now be able to access your Pi remotely via VNC from any computer where you are signed into VNC Viewer.

Setting Up File Sharing

  1. Install Netatalk on your pi with sudo apt-get install netatalk -y
  2. On your Mac, open Finder and type cmd + k to bring open the "Connect to Server" dialog.
  3. Type afp://raspberrypi.local, replacing raspberrypi with your hostname. Click the "+" to save the address and then click "Connect".
  4. Enter the username "pi" and your password. Select to remember the password in keychain. You can now browse files on your Pi, drag files from your Pi to your computer and vice versa. Another benefit is that you can open python files in Pycharm when connected.

Connecting to your Pi over the Internet

In order to SSH into your Pi from an external network, you'll need to enable a static ip for your Pi and enable port forwarding. To do this, you will need to access the network settings for your router. Unfortunately, this will vary depending on the type of router you have, and you will need to determine how to do this part on your own. I will give a rough outline of the process here:

Static IP

  1. Access the network interface to your router via a web browser (the address may be written on the bottom of your router, but it may just be with usename/pw: admin).
  2. Identify the section that lists connected devices on your network (mine is listed under DHCP > DHCP Clients List).
  3. Identify your Pi via it's hostname. Mine lists the hostname, its MAC address, and its currently assigned ip address.
    • Note: You may see your device listed twice if you are connected to both wifi and ethernet. Since I know I'll always be connected to WIFI, but may not be connected via ethernet, I'll choose that one to set. To identify which is which, try disconnecting your device from ethernet and then refreshing the list (this may require rebooting your router).
  4. I can enable a static ip address for my Pi by going to DHCP > Address Reservation, entering the MAC address I identified on the previous page, and providing a desired ip address (the current dynamic ip address is fine to choose).

Port forwarding

Before we forward our port, we want to change the port that our Pi will accept connections on. This makes it slightly more difficult for someone else to connect to your device.

Change your SSH Port
  1. ssh into your pi and type sudo nano /etc/ssh/sshd_config
  2. Near the top of the file you will see "#PORT 22". Remove the "#" and change 22 to another number. For example, 9001.
  3. Exit and save the file and reboot your pi.
  4. From now on when you connect you will have to provide the flag -p <yourPort>. For example, ssh pi@raspberrypi.local -p 9001.
  5. I would recommend setting up a bash alias for this on your computer.
    1. nano ~/.bash_profile
    2. Add alias pi-ssh='ssh pi@raspberrypi.local -p 9001' replacing with your information where necessary. Exit, save, and close/reopen terminal for the changes to take effect.
Forward the Port
  1. Go back into your router's network settings interface.
  2. Find the section for Port Forwarding.
  3. Identify the Service/Internal Port to use (this is the port number you set a moment ago).
  4. Identify the Static IP address that you enabled previously.
  5. Set it to forward for both TCP and UDP.
  6. Reboot your router if needed.
Testing that it worked

To test that this works, you can try to ssh into your Pi from your phone over your cellular network.

  1. Download Shelly from the app store.

  2. Open Shelly and ensure you are not connected to WIFI.

  3. Identify your computer's public ip address by Googling "what is my ip" or going to

  4. Add a new connection from Shelly and use the public ip address and port that you have set.

  5. You will be prompted to type your username which should be "pi". Press "return".

  6. Enter your password. Press return.

  7. Voila! You should see a familiar prompt. Type exit to end the session.

    • Notes:
      • If you have multiple Pis that you want to connect to on the same network just setup a static ip for each and forward each to a different port.
      • Your public ip address is dynamic and can change at any time. This could be problematic if you want to connect to your Pi but don't know what the current ip is. One way to get around this is by first connecting via VNC. If you connect via VNC you can go to a web browser on the Pi and find your ip address, as before, then ssh in. Alternatively, Alexander Baran-Harper has a nice video that walks you through setting up a cron-job on your Pi to automatically email you your ip address. Take a look at that here. Note that Google has changed their process slightly, and his walkthrough won't work as is. You will need to go into Google Security settings and enable less secure apps. Note that you cannot do this if you have 2FA enabled, so consider creating a throwaway account if you set this up. Additionally, you may need to review recent security events after sending the email via your app and mark that the unknown account was actually you. Finally, note that in addition to a cron-job to run your python script every X minutes, you may want to run your python file on boot. To do this, in your crontab add a rule for @reboot /usr/bin/python3 /home/pi/absolute/path/to/your/file, but be warned that this script will fail on boot unless you enable "wait for network on boot" in the raspberry pi configuration settings. Or, maybe just go the VNC approach :-)

A Little More Security

We will add one more layer of security by installing a package called fail2ban. This will help prevent against someone attempting to brute-force their way into your machine.

  1. Install with sudo apt-get install fail2ban -y
  2. Edit the settings with sudo nano /etc/fail2ban/jail.local
  3. Add the following (replacing yourPortNumber with your actual port number):
    bantime = 600
    maxretry = 2
    backend = polling
    port = yourPortNumber
    maxretry = 2
  4. Exit, save, and reboot your Pi.


  1. Let's update the python header files with sudo apt-get install python2.7-dev python3-dev -y. Most likely it is already up-to-date, but it doesn't hurt to check.
  2. Let's do the same with pip.
    1. wget
    2. sudo python
    3. sudo python3
  3. Next let's set up virtualenv and virtualenvwrapper.
    1. sudo pip install virtualenv virtualenvwrapper
    2. sudo rm -rf ~/.cache/pip
    3. Update our .profile with defaults
      1. nano ~/.profile
      export WORKON_HOME=$HOME/.virtualenvs
      export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
      source /usr/local/bin/
    4. Either exit the ssh session with exit and log back in, or set the source with source ~/.profile. You're now setup to use virtualenvs as normal!

This portion of the material comes from Adrian Rosebrock's guide to installing OpenCV on a Pi. Thanks, Adrian!

A Few More Prefs

You're pretty much set at this point from a Pi point of view. Feel free to set up any other normal configurations you're used to. For example, I may add the following to my bashrc:

sudo nano ~/.bashrc

alias ls='ls -lhA'
alias openbash='nano ~/.bashrc'
alias re='source ~/.bashrc'

mkcd () {
  mkdir -p $1
  cd $1

As always, exit, save, and reopen your terminal for the changes to take effect!

Huge thanks to Alexander Baran-Harper for providing his awesome videos. He has a bunch of other good videos, so take a look if you're interested in learning how to interact with a camera and other sensors using your Pi. Also thanks to Adrian Rosenbrok who has tons of great blogs that get you up and running with OpenCV.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.