This guide demonstrates how to install Debian 12 (bookworm), also known as "bookworm," on a Raspberry Pi 3B. The installation process focuses on using the command-line interface (CLI) for a lightweight and server-oriented setup.
Debian is a popular and versatile operating system known for its stability and extensive package ecosystem. Debian 12 (bookworm) is the latest stable release, offering numerous improvements and updated software versions.
This guide specifically targets Raspberry Pi 3B users who prefer a command-line interface over a graphical desktop environment. A desktop environment consumes valuable resources and might not be necessary for server-like tasks or lightweight projects.
Hardware and Software Requirements
- Raspberry Pi 3B with USB boot enabled (refer to to this article)
- USB stick with at least 8GB capacity (128GB used in this example)
- Internet connection
Since this guide focuses on a command-line interface (CLI) setup, we ll omit desktop environment installation. If you desire a graphical user interface later, read this guide.
1. Downloading the Debian Image
Visit the official Raspberry Pi Debian images page. Locate the image file for Debian 12 (bookworm) compatible with Raspberry Pi 3B. In this example, we will use: 20231109_raspi_3_bookworm.img.xz.
Download the image file using the wget command in your terminal on another computer:
$ wget https://raspi.debian.net/tested/20231109_raspi_3_bookworm.img.xz
2. Preparing the USB Stick
Decompress the downloaded image file using xz:
$ xz -d -v 20231109_raspi_3_bookworm.img.xz
The dd command we will use to write the image to the USB stick can potentially overwrite data on your drives. Double-check that you are targeting the correct device with the following command (replace /dev/sda if your device name is different):
$ lsblk
This command lists all block devices. Identify your USB drive by its size. Write the image file to the USB stick with sudo dd. Use extreme caution with this command as it can cause data loss.
$ sudo dd if=20231109_raspi_3_bookworm.img of=/dev/sda
3. Installing Debian
Boot your Raspberry Pi 3B with the prepared USB stick connected. The first boot may take some time as the system performs initial setup tasks. Eventually, you'll reach a basic command-line prompt.
After the system boots up, you'll be prompted to type the username "root". No password is set initially.
As an initial step, let's define variables for Wi-Fi, user, and hostname configuration. You can adjust their values according to your requirements:
root@pi3:~# ssid="Redmi8"
root@pi3:~# psk="my_secret_psk_pass"
root@pi3:~# user="pi"
root@pi3:~# hstname="pi3"
4. Setting Hostname
A hostname is a unique identifier for your Raspberry Pi on a network. It's recommended to set a meaningful hostname for easier identification and network administration.
root@pi3:~# echo "$hstname" > /etc/hostname
The hosts file is a local lookup table that maps hostnames to IP addresses. It is useful for associating your hostname (pi3 in this example) with the Raspberry Pi's loopback address (127.0.0.1).
root@pi3:~# echo "127.0.0.1 $pi3" >> /etc/hosts
You might need to reboot your Raspberry Pi for the hostname changes to take effect.
root@pi3:~# reboot
5. Wi-Fi Configuration
To connect Raspberry to your Wi-Fi network and enable internet access for further installations, edit the wlan0 interface settings in the file /etc/network/interfaces.d/wlan0:
root@pi3:~# echo "allow-hotplug wlan0" >> /etc/network/interfaces.d/wlan0
root@pi3:~# echo "iface wlan0 inet dhcp" >> /etc/network/interfaces.d/wlan0
root@pi3:~# echo "wpa-ssid $ssid" >> /etc/network/interfaces.d/wlan0
root@pi3:~# echo "wpa-psk $psk" >> /etc/network/interfaces.d/wlan0
To verify the content of the wlan0 configuration file, use the cat command (Figure 1):
root@pi3:~# cat /etc/network/interfaces.d/wlan0
Figure 1 - Checking Wlan0 Interface Configuration
Once you have configured the network interface file activate the wlan0 interface to connect to your Wi-Fi network:
root@pi3:~# ifup wlan0
6. Updating and Upgrading Debian
To update and upgrade your system packages, execute the following command:
root@pi3:~# apt update && apt upgrade
This command fetches the latest package information from the repositories and upgrades installed packages to their latest versions. It ensures that Debian is up-to-date with the latest security patches and feature enhancements.
7. Adding a New User and Enabling sudo
For everyday use and security best practices, it is recommended to create a new user account instead of using the root account directly. This new user will have its own password and won't have full administrative privileges by default.
We'll use a variable, $user, to store your chosen username. The adduser command creates a new user account and prompts you to set a password for the user.
root@pi3:~# adduser $user
Next, we will install the sudo package and configure the user account to use sudo. Sudo allows users to execute commands with root privileges when needed.
root@pi3:~# apt install sudo
Add the user to the sudo group:
root@pi3:~# usermod -aG sudo $user
While setting a root password allows you to access the most privileged account, it is generally discouraged for security reasons. Anyway, we need to change root password for root:
root@pi3:~# passwd
Logout from the root account:
root@pi3:~# exit
At the login prompt, type your new username and password you created earlier.
8. Setting the PATH for Non-Root Users
By setting the path appropriately, a non-root user will have easier access to essential tools and commands without needing to specify the full path every time.
pi@pi3:~$ echo 'export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin' >> ~/.bash_profile
9. Installing Network Utilities
This step equips your Raspberry Pi with essential network diagnostic tools.
pi@pi3:~$ sudo apt install net-tools traceroute -y
10. Installing Python3 and pip
This step equips your Raspberry Pi with Python3, a versatile programming language widely used for various tasks like scripting, web development, and scientific computing. We will also install pip, the package manager for Python, which allows you to install additional Python libraries and tools.
The following command installs both python3-full (including development tools) and python3-pip:
pi@pi3:~$ sudo apt install python3-full python3-pip -y
10.1 Using Virtual Environments
Virtual environments are a powerful concept in Python development. They isolate project dependencies, preventing conflicts between different projects that might require different Python libraries or versions.
In this example, we'll create a virtual environment named test:
pi@pi3:~$ python3 -m venv test
This creates a directory named test containing the necessary files for a virtual environment. Activate the virtual environment:
pi@pi3:~$ source ./test/bin/activate
This activates the virtual environment, and your terminal prompt will change to reflect the active environment (e.g., (test) pi@pi3:). Now, commands like pip3 will install packages specifically within this virtual environment.
Install a package with pip3 (within the virtual environment):
(test) pi@pi3:~$ pip3 install numpy
Here, we re installing the numpy library, a popular package for numerical computing, within the test virtual environment.
(test) pi@pi3:~$ python3
>>> quit()
Python can be used to interact with installed libraries. To view a list of installed packages within the virtual environment, you can use pip3 list.
Check installed packages (within the virtual environment):
(test) pi@pi3:~$ pip3 list
Figure 2 - Checking Installed Python packages
Deactivate the virtual environment:
(test) pi@pi3:~$ deactivate
This deactivates the virtual environment, and your terminal prompt will return to the normal state (pi@pi3:).
11. Installing Additional Packages
There are many other useful packages available for Raspberry Pi Debian. You can explore the available packages using the apt search command followed by a keyword related to the desired functionality. For example, apt search webserver would search for web server packages.
By leveraging the vast package ecosystem of Debian, you can customize your Raspberry Pi to suit your specific needs and projects.
Here, we will demonstrate installing the powerful text editor vim.
pi@pi3:~$ sudo apt install vim
Conclusion
This guide has comprehensively demonstrated the installation process for Debian 12 (bookworm) on a Raspberry Pi 3B. By following these steps, you'll have a robust and functional Raspberry Pi 3B running Debian 12 (bookworm), ready for various command-line-driven tasks and projects.