Setup your Raspberry Pi 4

I received my first Raspberry Pi yesterday. I have got a Raspberry Pi 4 Model B with 8 GB RAM. As I was doing the setup, I found it difficult to get all the information in one place, so I decided to write this blog post. This post documents the steps to set up a brand-new Raspberry Pi without connecting it to an external monitor.
Setting Up Your Raspberry PI

Here is what we are going to do,

  • install the Raspberry Pi OS (64-bit)]
  • setup internet connection over WiFi
  • SSH and run updates
  • setup VNC Viewer
  • install Docker

We have quite a lot to cover, so let's get started.

Prerequisites

The following are some prerequisites for the setup:

  • Raspberry Pi board (obviously)
  • connector for the power supply
  • microSD card (depends on your Pi model, though) and a card reader
  • another system to do the work (on the same network)
  • SSID (network name)
  • PSK (network password)

Installing Raspberry Pi OS

The very first thing you need for the setup is the Raspberry Pi Imager. Raspberry Pi Imager is a quick and easy way to install Raspberry Pi OS and other operating systems to a microSD card, ready to use with your Raspberry Pi. The imager provides a wide range of stable operating systems. However, they are all 32-bit (I think). You can very well choose either one and move forward. 

Since the Raspberry Pi 4 supports 64-bit OS, I will be installing the 64-bit Raspberry Pi OS. Please note that it's still in beta testing, and a list of known issues can be found on the same forum.

Follow the steps below to install the OS,

  • Connect your microSD card to the system where you have downloaded the OS and imager.
  • In the image, select the Choose OS option
  • Scroll down the pop-up list and select Use Custom at the end
  • Select the downloaded OS (.zip file)
  • Now select Choose Storage and select the connected SD card
  • Click Write and wait for the process to complete.

Note. If you choose an OS from the recommended list, it will first download the OS and then write it to the SD card. This will take a little longer, so be patient.

Setup WiFi

Now that we have installed the OS, it's time to set up the network.

Open the SD card in a file manager of your choosing. If you are using a Windows-based system, the SD card will directly land you in the boot directory. You can double-check it by looking for these files.

bootcode.bin
loader.bin
start.elf
kernel.img
cmdline.txt

Create a file named wpa_supplicant.conf next to the above files, i.e., in the boot directory.

However, if you are on a Linux-based system, you need to create the file as /etc/wpa_supplicant/wpa_supplicant.conf. Now, add the following content to your file.

country=us
update_config=1
ctrl_interface=/var/run/wpa_supplicant

network={
  scan_ssid=1
  ssid="network_SSID_here"
  psk="network_password_here"
}

Save the file and ensure that the file extension is .conf. Insert the SD in the slot of the Pi board, connect the power, and turn it on. As the Pi boots, you should see the blinking green and red lights. Once the boot is complete, the green light will turn off. 

SSH and Run Updates

Now, it's time to find the IP address of your Raspberry Pi. There are multiple ways to scan a network and get connected devices. In order to make it easy, you can use an IP Scanner. 

Scan your network, and you should see a Raspberry Pi device connected. Here is an example,

IP Scanner

Note. The IP address. Let's SSH into the Pi with default credentials.

# username - pi
# password - raspberry

➜ ssh [email protected]
[email protected]'s password:
Linux raspberrypi 5.10.63-v8+ #1488 SMP PREEMPT Thu Nov 18 16:16:16 GMT 2021 aarch64

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Sun Dec  4 00:43:28 2021 from 192.168.0.233
pi@raspberrypi:~ $

Here are a few things you should do as you log in.

# change the password
$ passwd

# get updates
$ sudo apt update

# upgrade the system
$ sudo apt upgrade

The Raspberry Pi OS also provides a configuration manager that you can start using the sudo raspi-config command. It will prompt you with the following or a similar term-UI.

Raspberry Pi

Setup VNC Viewer

Honestly, you don't really need a GUI to work with a Raspberry Pi. But it can be useful at times, especially if you are a beginner. Let's set up a VNC viewer just for that. On your Pi, install the realvnc server.

sudo apt install realvnc-vnc-server

Start the VNC server using the vncserver command.

vncserver
...
Running applications in /etc/vnc/xstartup

VNC Server catchphrase: "Needle baker salute. Price diagram origin."
             signature: e4-20-40-65-48-3d-f7-51

Log file is /home/pi/.vnc/raspberrypi:1.log
New desktop is raspberrypi:1 (192.168.0.232:1)

Notice the last line that provides details about how you can connect to the desktop started by the server.

Now install the [VNC Viewer][5] (also known as VNC client) on the system you want to have the display on. Once installed, start the client and use the desktop address you got from the VNC server (192.168.0.232:1 in my case). When prompted, provide the Raspberry Pi user credentials you used to SSH into the Pi.

Note: With this setup, each time you want to access the GUI, you will need to SSH and start the server manually. You can enable the VNC interface by default from the configuration manager (raspi-config) -> Interface Options -> VNC -> Enabled setting.

Congratulations! You have successfully set up your Raspberry Pi. :)

Installing Docker

Installing Docker on Raspberry Pi 4 is super simple. All you need is to run the following command that gets a script and pipes it to the shell:

curl -sSL https://get.docker.com/ | sh

By default, the Docker daemon runs a privileged service, and a non-privileged user can't connect with it without using sudo. Meaning if you try executing the docker ps command, you will get the following error message.

$ docker ps
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get "http://%2Fvar%2Frun%2Fdocker.sock/v1.24/containers/json": dial unix /var/run/docker.sock: connect: permission denied

In order to fix this, we need to add the pi user to the docker group. Once done, reboot the Pi.

sudo usermod -aG docker $USER
sudo reboot

SSH again and try executing the docker ps command w/o sudo; you should not get any errors.

$ docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

Conclusion

In this blog post, we have set up a Raspberry Pi 4 Model B from scratch without an external monitor connected to the Pi. We have also installed Docker, so the Pi is set to get you rocking on a new journey. In the next post, we will set up Rust and write our first application. Later we will containerize the application and run it on the Raspberry Pi. So, stay tuned.

I hope you found the steps helpful, and I look forward to your valuable feedback. If you run into an issue, please let me know, and I will be happy to help.