Runar Ovesen Hjerpbakk

Programmer. Software Architect. Technical Manager.

Shrinking a Raspbian installation to save space

Yet another Pi post!

When I put Raspbian on the SD card used by my Raspberry Pi, I used the Raspbian Stretch with desktop version. That was perhaps a poor choice on my part, as I use the machine as a headless server. However, I’m not that skilled with Desktop Linux, so the mere fact that there were buttons to push during the installation and stuff actually worked, made me feel like a pro.

All those packages that I’ll never use, like LibreOffice, still take up precious space, so I wrote a simple script to remove some of them.

You can find the script on GitHub and I’ll explain it below:

#!/bin/bash

# Must be run as root or sudo
if [ $(id -u) != 0 ]
then
  echo "This script must be run as root or with sudo."
  exit
fi

# delete package
delete_package(){
	# delete package
	apt autoremove -y wolfram-engine
	apt autoremove -y oracle-java8-jdk
	apt autoremove -y sonic-pi
	apt autoremove -y libreoffice*
	# 100MB-----------
	apt autoremove -y scratch* nuscratch squeak-plugins-scratch
	apt autoremove -y nuscratch
	apt autoremove -y gnome-user-guide
	apt autoremove -y freepats
	apt autoremove -y libraspberrypi-doc
	apt autoremove -y pypy
	apt autoremove -y greenfoot
	# 10MB-----------
	apt autoremove -y epiphany-browser*
	apt autoremove -y bluej
	apt autoremove -y netsurf*
	apt autoremove -y supercollider-server
	apt autoremove -y debian-reference*
	apt autoremove -y minecraft-pi
	apt autoremove -y claws-mail
	apt autoremove -y galculator
	apt autoremove -y geany*
	apt autoremove -y idle*
	apt autoremove -y lxterminal
	apt autoremove -y
	apt clean -y
}

START_TIME=date +%s

# change directory here
cd dirname $0

delete_package

END_TIME=date +%s

SS=expr ${END_TIME} - ${START_TIME}
HH=expr ${SS} / 3600
SS=expr ${SS} % 3600
MM=expr ${SS} / 60
SS=expr ${SS} % 60

echo "Total Time: ${HH}:${MM}:${SS} (h:m:s)"
  • First I check that the user id is equal to 0. Since this is root by default, I assume the user is root if this is the case. This check is only for convenience, the script will fail if the user is not root or superuser.
  • Then I list the packages that I want to remove. You can alter this list to suit your needs, but keep apt autoremove -y and apt -y clean at the end. autoremove removes packages that were installed by other packages and are no longer needed. clean clears out the local repository of retrieved package files.
  • Lastly, the script is timed and actually run.

As an aside, I know that Raspbian Stretch Lite is a thing and that it has a much smaller footprint than the Desktop version since it foregoes all the GUI-bits. But this script can easily be tweaked so that even that one contains only the bits you actually need. Remember you, can always add it again later if needed.

Free space