Skip to main content

Raspberry Pi OS

Installation

Ubuntu 22.04

sudo apt install rpi-imager

Create User

NOTE: 新版 Raspberry Pi OS 已經移除內建帳號 pi。使用前需要手動先建立帳號。

Headless Setup

SD Card > Boot partition > File: userconf

userconf:

alang:<encrypted-password>

Generate encrypted password

echo 'mypassword' | openssl passwd -6 -stdin
CLI
# Add user
sudo adduser <username>
sudo usermod -a -G adm,dialout,cdrom,sudo,audio,video,plugdev,games,users,input,netdev,gpio,i2c,spi <username>

# Delete user
sudo deluser -remove-home <username>

Enable SSH

Default credential is pi / raspberry
Headless setup

SD Card > Boot partition > File: ssh (an empty file)

SSH can be enabled by placing a file named ssh, without any extension, onto the boot partition of the SD Card.

Desktop
  1. Launch Raspberry Pi Configuration from the Preferences menu
  2. Navigate to the Interfaces tab
  3. Select Enabled next to SSH
  4. Click OK

Option #3: Using the raspi-config

  1. Enter sudo raspi-config in a terminal window
  2. Select Interfacing Options
  3. Navigate to and select SSH
  4. Choose Yes
  5. Select Ok
  6. Choose Finish

Wireless LAN

Headless setup

SD Card > Boot partition > File: wpa_supplicant.conf

wpa_supplicant.conf :

country=TW # Your 2-digit country code
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
network={
    ssid="YOUR_NETWORK_NAME"
    psk="YOUR_PASSWORD"
    key_mgmt=WPA-PSK
}
CLI
# Usage: sudo raspi-config nonint do_wifi_ssid_passphrase <ssid> <passphrase> [hidden] [plain]
sudo raspi-config nonint do_wifi_ssid_passphrase myssid 'mypassphrase' 0 0 # Visible SSID, passphrase quoted

 

Python

Install pip

sudo apt install python3-pip
pip --version
sudo pip install --upgrade pip

雖然 pip 可以成功安裝,不過要繼續安裝 3rd-party 套件時,會遇到以下錯誤。

error: externally-managed-environment

× This environment is externally managed
╰─> To install Python packages system-wide, try apt install
    python3-xyz, where xyz is the package you are trying to
    install.
    
    If you wish to install a non-Debian-packaged Python package,
    create a virtual environment using python3 -m venv path/to/venv.
    Then use path/to/venv/bin/python and path/to/venv/bin/pip. Make
    sure you have python3-full installed.
    
    For more information visit http://rptl.io/venv

新版 Raspberry Pi OS 已經不允許直接安裝 3rd-party 套件;而是必須在 python 虛擬環境才能安裝套件。

建立 python 虛擬環境

# 以下指令在 user 帳號下執行,例如 pi
mkdir myproject
cd myproject
python -m venv env
source env/bin/activate
which python
# 記得先升級 pip 至最新版
pip install --upgrade pip
# 開始安裝套件
pip install paho-mqtt
# 檢視已安裝套件
pip list

要執行 python 程式時,開頭宣告指向虛擬環境的 python,可以避免需要手動進入 python venv。

#!<path-to-venv>/bin/python