Last updated
Raspberry Pi setup notes
The holidays are the perfect time for setting up a Raspberry Pi, screwing it up, and having to start over again. I’ve done this enough times in the last two weeks that I decided to write some stuff down.
Of note:
- I’m using a Mac.
- I've run these steps on a Raspberry Pi 4 and they worked.
- I used the Pi Imager to put an image on an SD card. (If you don’t intend to use the desktop GUI, use the Lite image under “other.”)
Set up for headless use #
Before you put the SD card in the Pi, set it up so it will connect to wifi and you can connect to it via ssh. With the SD card still connected to the Mac:
cd /Volumes/boot
touch ssh
Set up wifi by creating the file wpa_supplicant.conf
in /Volumes/boot
, with the following contents:
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=<2 letter ISO 3166-1 country code>
network={
ssid="<network>"
psk="<password>"
}
The country code for the US is US
. (See other country codes.)
Connect & update & install an editor #
Figure out the Pi’s IP address.
nmap -sn 192.168.1.0/24
Then, ssh to it:
ssh [email protected]
Enter raspberry
as the password, and you’ll be logged in.
Change the password for the pi user:
passwd
You’ll hardly ever use this password, and maybe you’ll never use it, but if you do need to use it, you probably won’t remember it, and you’ll end up reading this blog post when you re-image your Pi.
Update package lists, upgrade packages (this could take a while), and install vim (if you don’t want to use nano).
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install vim
Create a new user #
Create a new user with sudo permissions:
sudo adduser rmurphey
sudo adduser rmurphey sudo
You’ll be prompted for a password. You’ll need to use this password every time that you sudo
from the new account, which should make it more likely that you’ll remember it, but if your Pi use is seasonal like mine, good luck with that. I’m not saying you should write it down, but … you do you.
You could use this password to log in to your Pi, but that would be a pain. Instead, copy an ssh key from your Mac to the Pi so you can log in without a password:
ssh-copy-id [email protected]
Oh My ZSH #
Oh My ZSH is nice. Set it up.
sudo apt-get install zsh
sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
I like the fino theme.
Python 3 #
If you want to interact with devices connected to your Pi, you’ll probably end up using Python. Set up Python 3 as the default Python. Why isn’t it already? Who knows.
sudo pip3 install --upgrade setuptools
sudo apt-get install -y python3 git python3-pip
sudo update-alternatives --install /usr/bin/python python $(which python2) 1
sudo update-alternatives --install /usr/bin/python python $(which python3) 2
sudo update-alternatives --config python
Use sudo raspi-config
to enable I2C and SPI.
Install some libraries for interacting with devices:
pip3 install RPI.GPIO
pip3 install adafruit-blinka
It seems like only the pi user has access to the SPI, I2C, and GPIO interfaces by default, so:
sudo adduser rmurphey spi
sudo adduser rmurphey gpio
sudo adduser rmurphey i2c
You might need to log out and log back in for these permissions changes to take effect.
Node #
JavaScript is fun, and johnny-five is a fun way to use JavaScript, so you’ll want Node.
Install with nvm:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash
Read the output; you’ll need to add something like this to .zshrc:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
Finally, install the version you want:
nvm install 14
Maybe #
sudo raspi-config
to change the hostname.sudo apt-get install screen
so you can reconnect to a shell session.sudo apt-get install motion
if you want to set up a camera stream.
Couldn’t I just save a copy of the SD card before I screw it up again? #
Read more posts in the archive.