Raspberry Pi Setup

Raspberry Pi Setup Guide

This guide provides instructions for setting up a Raspberry Pi and optimizing its performance by adding swap memory.

Getting Started with Raspberry Pi

Requirements

  • Raspberry Pi board (Pi 4 or Pi 5 recommended for best performance)
  • MicroSD card (16GB or larger)
  • Power supply (USB-C for Pi 4/5)
  • HDMI cable
  • USB keyboard and mouse (for initial setup)
  • Monitor/display
  • Ethernet cable or Wi-Fi connectivity

Basic Setup

  1. Download Raspberry Pi OS

  2. Flash the OS

    # You can also use the official Raspberry Pi Imager with a GUI
    # Alternatively, from the command line (macOS/Linux):
    diskutil list  # Find your SD card (e.g., /dev/disk2)
    diskutil unmountDisk /dev/disk2
    sudo dd bs=1m if=path/to/raspios.img of=/dev/rdisk2
  3. First Boot

    • Insert the microSD card into your Raspberry Pi
    • Connect the display, keyboard, mouse, and power supply
    • Follow the on-screen setup wizard to configure:
      • Language and region
      • Username and password
      • Wi-Fi connection
      • System updates
  4. Enable SSH for Remote Access (Optional)

    # On the Raspberry Pi
    sudo systemctl enable ssh
    sudo systemctl start ssh
     
    # Get your IP address
    ip addr show
     
    # Connect from another computer
    ssh username@raspberry_pi_ip

Adding Swap Memory

Swap space provides virtual memory that can be used when the physical RAM is full. This is especially helpful for Raspberry Pi devices with limited RAM.

Check Current Swap Configuration

# Check if swap is enabled and its size
free -h
 
# View swap configuration details
sudo swapon --show

Method 1: Using dphys-swapfile

This is the simplest method as it's managed by the Raspberry Pi OS.

  1. Edit the swap file configuration

    sudo nano /etc/dphys-swapfile
  2. Modify the CONF_SWAPSIZE parameter

    # Default is 100MB, increase to desired size (in MB)
    # For example, to set 1GB swap:
    CONF_SWAPSIZE=1024
  3. Restart the swap service

    sudo systemctl restart dphys-swapfile

Method 2: Create Custom Swap File

If you need more control over your swap file:

  1. Turn off existing swap

    sudo dphys-swapfile swapoff
    sudo systemctl disable dphys-swapfile
  2. Create a new swap file

    # Create a 2GB swap file
    sudo fallocate -l 2G /swapfile
    # Set permissions
    sudo chmod 600 /swapfile
    # Format as swap
    sudo mkswap /swapfile
  3. Enable the swap file

    sudo swapon /swapfile
  4. Make the swap persistent across reboots

    echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

Optimize Swap Settings

You can tune your swap performance with the swappiness parameter:

# Check current swappiness (0-100, lower means less swap usage)
cat /proc/sys/vm/swappiness
 
# Set temporarily
sudo sysctl vm.swappiness=10
 
# Set permanently
echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf

Performance Considerations

  • microSD Card Wear: Excessive swap usage can reduce the lifespan of your microSD card
  • Speed: Swap is significantly slower than RAM
  • Recommended Swap Size:
    • 1GB RAM: 2GB swap
    • 2GB RAM: 1GB swap
    • 4GB RAM: 1GB swap
    • 8GB RAM or more: 512MB swap (or none for many use cases)

For high-performance applications, consider using an external SSD connected via USB 3.0 for both the OS and swap file.

Monitoring Resource Usage

# View real-time resource usage
htop
 
# Install if not available
sudo apt update
sudo apt install htop

The htop utility provides a colorful, interactive view of your system's CPU, memory, and swap usage.

Troubleshooting

If you encounter issues with swap:

  1. Check swap status

    sudo swapon --show
  2. View system logs

    dmesg | grep -i swap
  3. Re-create swap if necessary

    sudo swapoff -a
    sudo swapon -a

Additional Resources