Create a Lightweight Raspberry Pi System with Raspbian Lite

Raspbian Lite
In this tutorial, we’re going to build a highly optimized Raspberry Pi system that runs very lean on resources with the help of Raspbian Lite.  Whereas a baseline Raspbian system consumes around 158 MB of RAM, Raspbian Lite runs at a mere 34 MB.  To achieve this lighter weight footprint, we must give up the graphical user interface (GUI); however, we’ll include steps in the build (and tips) to help us work productively without a GUI.

This guide follows the same flow as my popular Beginner’s Guide to Installing Node.js on a Raspberry Pi, and I recommend that you use that guide if you are a beginner or if you simply want the luxury of using a GUI. There’s nothing wrong with that, and a GUI makes sense in many contexts! For those who want to run a lighter weight Pi system, let’s get started!

Table of Contents

Raspberry PiHardware Needed

If you don’t currently own a Raspberry Pi, the easiest way to get started is to buy a starter kit such as this one on Amazon:

We’ll need the following items to get started:

  • Raspberry Pi 4 Model B, Pi 3 Model B+, Pi 3 Model B, or Pi 2 Model B (these are the official names for the Raspberry Pi 4, Pi 3, and Pi 2. Don’t let the “B” confuse you since there is also an older Raspberry Pi Model B which is now effectively the “Raspberry Pi 1 Model B”.)
  • MicroSD card (I recommend a class 10 card that is 16 GB or higher.  I like this Samsung 32GB EVO Plus Class 10 Card.)
  • MicroSD to SD memory card adapter (so you can use the SD card reader on your laptop/desktop to write to the microSD card.  Many microSD card vendors include this SD adapter with the purchase as shown here.)
    SD card adapter
  • Micro USB power supply (2.5A or greater recommended) to power the Pi.
  • (Pi 2 only) USB WiFi adapter (or Ethernet cable if preferred.  I have tested this Edimax USB WiFi adapter on Raspbian and it works great out of the box.  There’s a reason this is a best seller on Amazon.)  This is not needed for the Pi 4/Pi 3 since it includes built-in Wi-Fi.
  • Windows laptop/desktop (Linux or Mac works great too, but this tutorial is geared more toward Windows machines, so some readers will need to adapt)
  • HDMI monitor/USB keyboard/mouse (You can borrow these from another system temporarily and then run your Pi in a headless mode as I will explain later.)

Once you have gathered the needed hardware, we are ready to bring our Pi machine to life!

Write Raspbian Image to SD Card

We will be running Raspbian Lite which is a free operating system based on Debian Linux and optimized for the Pi.  Here we go:

  • Download the latest Raspbian image from https://www.raspberrypi.org/downloads/raspbian/.  Download the lite version rather than one of the desktop versions.  The download is a zip file that is about 425 MB versus the desktop versions which range from 1.1 GB to 1.9 GB.
  • Insert the microSD card into the SD adapter as shown in the Samsung sample picture above.
  • Insert the SD adapter in the SD card reader on your laptop/desktop.  Make sure it is seated well in the connection.
  • Launch Windows Explorer and verify that the SD card registers as a drive on your system.  Make a note of the drive letter as you will need it soon.
  • Download and install Etcher.  Etcher is a cross-platform (Windows, OS X, Linux) tool for flashing images to SD cards.  It has many nice features including a simple user interface, the ability to flash an image without unzipping the zip file, and a validation step to verify the SD card image was written correctly to the SD card.
    • (As an alternative for Windows users, you can use Win32 Disk Imager since it also provides the ability to read an SD card and create an image file which is handy for creating snapshots of Raspberry Pi systems that can flashed back to an SD card later if needed.  For Win32 Disk Imager, you will need to first unzip the downloaded zip file before burning the .img file to the appropriate drive letter containing your SD card.)
  • Launch Etcher.  Be patient as Etcher takes a few seconds to launch.  When launched, the interface will look something like this:
    etcher
  • Click the Select image button and navigate to the Raspbian zip file that you downloaded.  (It does NOT need to be unzipped before flashing.)
  • Click the Select drive button to specify the target device location of the SD card.
    WARNING: Be sure you choose the right target device or you will write the image to another drive on your system and this will not be good.
  • Click the Flash! button to write the image to your SD card.
  • If you are running Windows, you may be presented with a security prompt regarding making changes to your device. If so, click Yes to proceed.
  • After the image is written to the SD card and verified, Etcher will automatically dismount your SD card so it can be safely removed.
  • Remove the SD adapter from your laptop/desktop, and remove the microSD card from the SD adapter.

Prep the Hardware

See the Raspberry Pi 4 Tech Specs page for a diagram showing the various parts of the Pi 4.  Also, you will find a diagram of the Pi 2 here (the Pi 3 looks very similar).  These diagrams may help you in this section.  Let’s get this little SBC (single board computer) ready to boot! Here are the steps:

  • Insert the microSD card into the Pi.  (Turn the Pi upside down.  The microSD card slot is located on the side opposite of the USB ports.  This short video explains the process well.)
  • Connect the USB keyboard and mouse
  • Connect the HDMI cable
  • Connect the USB Wi-Fi adapter (or Ethernet connection if you are not using Wi-Fi) This step is not necessary if you are using a Raspberry Pi 4/Pi 3 model which has on-board Wi-Fi.
  • Connect the power supply (You will see the Pi lights come on and power up.)
  • You should now see Raspbian Lite booting on the screen.  Hooray!

Option for advanced users – Complete initial setup without a monitor and keyboard

We are ultimately creating a Raspberry Pi system that can run in headless mode (i.e. without a monitor and keyboard).  It is possible to even complete the initial setup without a monitor and keyboard. To accomplish this goal, you will need to create the following files in the root of the microSD card on your PC/Mac after writing the Raspbian image.

  • wpa_supplicant.conf – Add your WiFi network and credentials in this file. For Raspbian Stretch, the contents will be:
    ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
    network={
        ssid="YOUR_NETWORK_NAME"
        psk="YOUR_PASSWORD"
        key_mgmt=WPA-PSK
    }
    

    See this article for more information.

  • ssh – By creating this empty file, the ssh server will be enabled on boot so you can remote into the Pi with Putty (explained later in this article) or another ssh client to continue the configuration. See here for more details.

Thanks to Graham Smith who pointed out this option in the comments!

After connecting the power cord and booting, you will need to find the Raspberry Pi on your network so you can remote in using ssh.  Use the ping command to locate your system on the network.  The following may work:

C:\> ping raspberrypi.local

If this does not work, you can use either the Adafruit Pi finder or nmap to find the Pi’s IP address so you can remote in.  Later in this tutorial, we install Samba so finding your Pi on the network will no longer be a concern.


Configure the Pi

Log in to your new system when you are prompted using the following credentials:

  • username: pi
  • password: raspberry

We first need to make a few changes to the Pi for optimization, and to personalize it for our use.  To get started, run the following command:

$ sudo raspi-config

This will launch the following screen:

raspi-config

At this screen, we will configure our Pi. To navigate, there are a few keyboard shortcuts we need to know:

  • Arrow keys – used to go up and down to select menu items
  • Enter key – used to select and drill further into the menu
  • Tab key – used to break out of the menu. In the screen above, the tab key will take you to the bottom two menu options so you can, for example, arrow to the right and select <Finish>.
  • Spacebar – used to toggle radio button selections on and off.

Who needs a mouse when you have a keyboard? 🙂 Let’s start configuring!

Change User Password (Optional, but recommended)

From a security perspective, it’s a good idea to change the default password. Just make sure you don’t forget it. 😉

To change your password:

  • Hit the enter key to navigate into the Change User Password option.
  • Enter your new password when prompted.  (You will be prompted to enter your password twice.)

Network Options

Arrow down to Network Options and hit the Enter key.

Hostname

If you prefer less typing, you can change the host name from “raspberrypi” to something shorter such as “raspi”.

  • Hit the enter key to select Hostname.
  • Hit the enter key after reading the message.
  • Enter a new host name.
  • Hit the Enter key to save your selection and return to the main menu.
Wi-fi

You definitely want to configure your wi-fi now so you can remote into your Raspberry Pi over the network when it is rebooted, and you no longer have the keyboard/mouse/and monitor connected to it.

Arrow back down to Network Options and hit Enter.

Arrow down to Wi-fi and hit Enter.
config wifi

Follow the prompts to enter the following information:

  • SSID (the name of your wireless home network)
  • Passphrase (your wi-fi password)

Localisation Options

The Raspberry Pi is the brilliant brainchild from our friends in the UK. If you are not from the UK, some of the keys on your keyboard may not work as expected. Here are the changes I made to make my Pi feel at home in San Diego:

First, arrow down to Localisation Options and hit Enter.

Change locale – hit Enter (the screen will momentarily blip to black before returning to raspi-config.)

  • Arrow down to “en_US.UTF-8 UTF-8” (or your locale of choice)
  • Press the spacebar to put a * in the brackets [*].  (Note: don’t remove the “en_GB.UTF-8 UTF-8” locale while adding your locale if you want the system to function properly.)
  • Hit the tab key and then hit the Enter key to select <Ok>.
  • (Another screen appears.) Select “en_US.UTF-8” (or your locale of choice) as the default locale.
  • Hit the Enter key to save your selection and exit.
  • (The system will generate the new locales and then bring you back to the main menu.)

Arrow back down to Localisation Options and hit Enter.

Change Timezone – hit Enter

  • Select your geographic area. (America, in my case)
  • Select your specific time zone. (Los Angeles, in my case)
  • Hit the Enter key to save your selection and return to the main menu.

Arrow back down to Localisation Options and hit Enter.

Change Keyboard Layout – hit Enter

  • Change keyboard if needed. Here’s the changes I made:
    • Select Generic 104-key PC and hit enter
    • Select Other
    • Select English (US)
    • Key to function as AltGr (I chose the default for the keyboard layout.)
    • Compose key – No compose key
    • Use control-alt-backspace to terminate an X Server: no

Arrow back down to Localisation Options and hit Enter.

Change Wi-fi Country – hit Enter

  • Arrow down and select the country in which the Pi will be used.
  • Hit the tab key and then hit the enter key to select <Ok>.

Interfacing Options

We want to enable the SSH server so we can remote into our system without dedicating a monitor, keyboard, and mouse to it. Here are the steps:

  • Arrow down to SSH and hit the Enter key.
    ssh config
  • Hit the Enter key to select Yes and enable the SSH server.
  • Hit the Enter key to select <OK>.

Advanced Options

Back at the raspi-config main menu, arrow down to Advanced Options and hit the Enter key.

We will be running our Raspbian Lite system without a graphical user interface so we can reduce the amount of memory made available for the GPU and consequently make more memory available for the CPU since the CPU/GPU memory is shared.

Arrow down to Memory Split and hit the Enter key.

Enter a value of 16 for the amount of memory in MB that the GPU should have and hit the Enter key.

Once you are back to the main menu (as shown in our raspi-config screenshot above), you can hit the Tab key, arrow right to select the <Finish> option, and hit the Enter key. The Pi will then let you know that it needs to reboot to activate the changes you made. Reboot it now and log back in.

We’re now ready to move on and ensure our system is up to date with security patches.

Apply Raspbian Updates

First, let’s verify that our Wi-Fi has been configured correctly and that we have Internet access. Issue the following command from the terminal:

$ ping -c 5 google.com

You should receive responses back from google.com if you have Internet connectivity.  If not, you will need to repeat the Wi-Fi configuration steps outlined earlier in the tutorial and test again.

Assuming you have Wi-Fi connectivity, you are ready for the next steps.

We will first run the apt “update” command.  This command will not actually update any software on the system but will download the latest package lists from the software repositories so that Raspbian will be aware of all new software available along with dependencies.  Issue the following command at the “$” prompt:

$ sudo apt update

Next, run the following command to upgrade any packages installed on your system that need upgrades:

$ sudo apt full-upgrade -y

This is important to keep your Pi system synchronized with security updates, etc. These two commands should be issued together and run periodically.

Prepare for Remote Connection

We want to run our Pi remotely without requiring a dedicated HDMI monitor and USB keyboard/mouse.  This is known as headless mode.

In the steps above, we enabled the SSH server, and now we need to install the samba package so we will be able to access the Pi by its host name from Windows machines on the network rather than by its IP address which can change since the Pi receives its IP address via DHCP.  (For OS X users, you can install Bonjour and then access your Raspberry Pi by host name.  For example, if your host name is “raspi”, you can access it on OS X as “raspi.local”.)  OK, let’s install Samba:

$ sudo apt install -y samba

After this installation completes, you should be able to ping the Pi Hostname (configured in Raspberry Pi Configuration program above) from a Windows machine:

C:\> ping raspi

You are now ready to launch a remote connection!

  • From your Windows machine, download and install Putty if you do not already have an ssh client installed.
  • Launch Putty and enter the host name of your Pi (“raspi” in my case) as I have highlighted in yellow in the screenshot:
    putty
  • Select the Open button to initiate a remote SSH session with your Pi.
  • Enter your credentials and log into the Pi.
    • username: pi
    • password: raspberry (unless you changed it in the Raspberry Pi Configuration program earlier in the tutorial.)

Amazing!  We are connected remotely to the Pi and we no longer need the dedicated HDMI monitor and USB keyboard/mouse. Let’s go ahead and shut down the Pi for a minute so we can free up our monitor and keyboard/mouse:

  • Launch a terminal session and enter the following command to cleanly shut down your system:
    $ sudo poweroff
  • Wait a minute for the Pi to fully power down.
  • Unplug the power cable, HDMI cable, and the USB keyboard and mouse.
  • Plug the power cable back in and the Pi should be on its way back up without all those extra cables!
  • After giving the Pi a minute or so to boot, connect to it once again using Putty or other ssh client of your choice.

Create Windows File Share on the Pi (Optional)

We can create a Windows file share on the Pi so that we can copy files from our Windows machine directly onto the Pi.  This can come in handy in many contexts.  This is also useful for OS X users since you will be able to connect to your Raspberry Pi file share from Finder under “Shared”.  Here’s how we do it:

  • From the terminal, issue the following command to edit the Samba configuration file.
    $ sudo nano /etc/samba/smb.conf
  •  Scroll down to the very bottom of the smb.conf configuration file and add the text shown below.  If you are using Putty, you can copy the text from this tutorial and right click in the Putty window to paste the text right into Nano.  I saved you a few keystrokes.🙂
    [PiShare]
     comment=Pi Share
     path=/home/pi
     browseable=yes
     writeable=yes
     only guest=no
     create mask=0740
     directory mask=0750
     public=no
  •  Save the changes to the smb.conf file in the nano editor and exit:
    • select Ctrl+X
    • press “Y” to save changes
    • hit the Enter key
  • After saving the changes to the configuration file, Raspbian will automagically restart the SMB (Samba) services based on the changes you made.
  • Back in the terminal session, we need to create an SMB (Samba) user so we can connect securely from Windows.  We will create a user named “pi”; however, this will be an SMB (Windows user) rather than the Raspbian “pi” user. Here’s how we do it:
    $ sudo smbpasswd -a pi

    You will be prompted to enter and re-enter a password.  Document this password somewhere for future reference.

We are ready to connect to our newly created Pi file share from Windows!

  • Back on your Windows machine, type the Windows key and “R” (Win-R) to launch a run box for typing in commands.
  • Enter two backslashes followed by the name of your Pi machine (i.e. your Hostname) and hit enter.  For example:
    \\RASPI
  • (Please note that when connecting to the file shares through Windows 10, the host name here can be entered in lower case or upper case.)
  • When prompted for credentials, enter the following:
    • Username: The format to enter here in Windows parlance is the domain\username.  For us, our domain will be our Pi hostname and our user name will be “pi”.  Therefore, we will enter: raspi\pi
      If your Hostname is not raspi, you will obviously enter that instead.
    • Password: (enter the password you created with the smbpasswd command above)
  • Voilà!  We should now be connected.  Double click the PiShare folder to launch the file share you just created.
  • You can test your newly created file share by creating a text file on the Windows side and verifying that it shows up in the /home/pi directory on the Pi side.
  • In Windows, you can also map a drive to the file share you just created as follows:
    • Launch Windows Explorer.
    • Click on the Map network drive button in the ribbon menu at the top and select Map network drive.
    • Enter the drive letter (for example “P” for “pi” drive), the share folder path, and select Reconnect at sign-in:
      map drive
    • Enter the password you created with the smbpasswd command above (if prompted) and select Remember my credentials:
      map drive
    • You should now see a “P” drive (or other drive letter you selected) when viewing through Windows Explorer!

Install Node.js (Optional)

You now have an amazing general purpose Raspberry Pi system that can be used for a variety of tasks and inter-operates well in the Windows world (it even looks like a Windows machine to the other Windows machines!) – and can play nicely in the Mac and Linux world too.  Let’s go ahead and install Node.js so we will be ready to do some fun projects in the future.  Here are the steps:

Our friends at NodeSource host and maintain some excellent Node.js binary distributions.  We will leverage a command they have written to add another package repository to our Pi so that we will be able to “apt install” a modern version of Node.js from their repository.  This is beneficial since the Debian/Raspbian versions may not always be up to date.  By adding the NodeSource repository, we will also be able to receive updates rather than just installing a standalone (.deb) file version of Node that cannot be updated easily.

Note: As described at the beginning of this article, this final section of the tutorial related to the installation of Node.js requires a Pi system based on the newer ARMv7 or ARMv8 chip such as the Pi 4/Pi 3/Pi 2.  NodeSource provides Node.js binaries for these newer ARMv7+ architectures, but not for Raspberry Pi systems based on the older ARMv6 architecture such as the Raspberry Pi Model B/B+ or the Raspberry Pi Zero.

Read the writing carefully on your Raspberry Pi circuit board to confirm it indicates something like “Raspberry Pi 4 Model B” or “Raspberry Pi 2 Model B”. If in doubt, run the following command in the terminal:

$ uname -m

If the result returned starts with “armv6”, you are running a Raspberry Pi based on the older ARMv6 chipset and the next Node.js installation step will not work; otherwise, you are ready for the next step.

Here we go in the final stretch!  Let’s proceed with an installation of the latest version of Node at the moment which is Node 20.5.1.

$ curl -sL https://deb.nodesource.com/setup_20.x | sudo -E bash -

The previous command updates our Debian apt package repository to include the NodeSource packages.

Note: It’s generally a good idea from a security perspective to know what commands you are invoking on your system, especially since the command above invokes the script as the root user.  If you want the technical details behind this script, you can launch the URL (https://deb.nodesource.com/setup_20.x) in your browser and review the code.  The script carries out some ceremony to determine the Linux distribution you are running and instructs the Debian apt package system to add the NodeSource package repository as a trusted source for obtaining Debian packages.  This enables us to install Node.js now and upgrade to more recent versions of Node.js when they become available.

Now that we have added the NodeSource package repository, we can move on and install Node.js!

$ sudo apt install -y nodejs

We can then test and see what version of Node we are running and launch the Node REPL as we discussed in the previous article as a quick test to confirm the installation was successful.

$ node -v
v20.5.1
$ node
> 1 + 3
4
> # We can hit Ctrl-C twice to exit the REPL and get back to the bash (shell) prompt.

Raspbian Lite Workflow

We have created a lean Raspberry Pi system without a GUI, but we don’t want to hinder our productivity when we need more than the command-line and text-based editors such as nano can provide us. If you have followed the steps above and created the Samba file share and mapped a drive, you are good to go!

Here’s a typical workflow that will improve your productivity.  In this example, we assume you have mapped the “P drive” to your Raspberry Pi.

  • On your Windows machine, create a folder on the P drive for your project.
  • Open this P drive folder using Visual Studio Code or text editor of your choice.
  • Build your project using the full glory of your Windows text editor/IDE (instead of just the nano editor).
  • Launch Putty or other ssh client from your Windows machine and log into your Raspberry Pi.
  • Run your newly created software from the terminal.  (For example, Node.js web server, Python console application, etc.)

To improve your workflow in the terminal, I recommend installing and learning about tmux.  The tmux application allows you to multiplex and access multiple separate terminal sessions inside a single terminal window or remote terminal session.  The lower tech alternative is to simply launch multiple Putty sessions, but this is less elegant.  See this tmux tutorial if you want to learn more.

Additional Optimizations (Optional)

We can save approximately 25 mA of power (according to Jeff Geerling) by powering down the HDMI display circuitry—assuming you are planning to remote in and will not connecting an external monitor to the Raspberry Pi HDMI port.  We will run a command when the Pi boots to accomplish this goal.

  • From the terminal, issue the following command to edit the rc.local file which runs on startup after other Linux services have started:
    $ sudo nano /etc/rc.local
  •  Just above the “exit 0” line at the bottom of the file, enter the following:
    tvservice --off
  • Save the changes to the rc.local file in the nano editor and exit:
    • select Ctrl+X
    • press “Y” to save changes
    • hit the Enter key

Conclusion

Congratulations!  You have built a lean Raspberry Pi system that is well suited for creating and implementing a wide variety of fun projects.   You are equipped and ready to make something awesome!

Follow @thisDaveJ (Dave Johnson) on Twitter to stay up to date on the latest tutorials and tech articles.

Related Articles

Beginner’s Guide to Installing Node.js on a Raspberry Pi
Using Visual Studio Code with a Raspberry Pi (Raspbian)
Upgrading to more recent versions of Node.js on the Raspberry Pi
Debian apt command cheat sheet – summary of new apt

Share

10 thoughts on “Create a Lightweight Raspberry Pi System with Raspbian Lite

  1. Great write-up! Just to note that you can avoid having to hook the Pi up to any hardware (apart from power for course) and skip configuring WiFi by copying an empty file named ssh and a properly configured wpa_supplicant.con file (Google search will elaborate) to the boot partition of a newly burned SD card. On first boot ssh is enabled and WiFi gets configured automatically. You can then connect using PuTTY. If you have Apple’s Bonjour installed (free download) you don’t even need to go in to your router to find the IP address as you can ssh in using the default hostname. I have a profile I use in PuTTY saved for connecting to newly minted Pis created in this way.

  2. I have book marked this site years ago and still come back here when setting up a new Pi.
    One update though, with the latest version of Rasbian, username “pi” and password “raspberry” no longer works when setting up a headless pi and connecting SSH for the first time. You need to now set the password when writing the image to the SD card using the Raspberry Pi Imager. Reference: https://www.raspberrypi.com/news/raspberry-pi-bullseye-update-april-2022/

Leave a Reply

Your email address will not be published. Required fields are marked *