There are plethora of instructions on the web to update and upgrade Raspbian and they work too, under normal conditions. However as Raspberry Pi is increasingly being used for Mission Critical tasks, it is important to adopt a procedure which won’t leave you with broken packages or worst, a un-bootable Operating System with your data on it. The following is a simple procedure, with explanations which will make sure that your Pi remains working.
First let’s update and upgrade the Pi with a single line command, run from terminal:
sudo apt-get update -y && sudo apt-get upgrade -y && echo success
This will update and upgrade your Pi and if successful will print success at the end.
Now reboot the Pi with:
Normally Pi would reboot properly 99% of the time. If it refuses to boot then you can select the old kernel while booting to get back an usable version of Raspbian OS.
After this only two more steps remain. First is to dist-upgrade the Raspbian OS:
sudo apt-get dist-upgrade -y && sudo apt-get autoremove -y && echo success
You may wonder why I didn’t do it the very first time. While dist-upgrade is better than plain upgrade as it can intelligently install packages based on dependency, upgrade retains the older kernel and gives you a way to back out safely, unlike dist-upgrade. The procedure outlined above protects your OS from cases where the upgrade is broken for any reason.
Now all that is left is to reboot again to make sure everything is OK.:
You may notice that I didn’t ask you to rpi-update, an instruction you will find at many places online. This is because you do not need rpi-update, the necessary firmware updates are already installed when you upgraded Raspbian. In fact, and I speak from experience, rpi-update can break your running system. Let’s leave that bleeding edge to who really needs it. If you are reading this article, then it can safely be said you don’t.
I used sudo apt-get autoremove to remove any extra packages.
The && before echo success makes sure success is printed only when the previous command succeeds.
Disk space is premium in SD Card. So it may not be out of place to check for available disk space before running update, upgrade or dist-upgrade:
To save some space clean up the downloaded debian archives with:
sudo apt-get clean
Now you understand why automating Raspbian updates / upgrades is a bad idea, unless you want to use the Raspberry pi only for testing purposes.
Advanced Note on why rpi-update was not suggested:
It’s current Raspberry Pi practice not to use this [ rpi-update ] unless you REALLY need it as it can bring in a non-working kernel (i.e. the latest bleeding edge version, which is not guaranteed to work).
My thoughts are that it is not actually required once you have done the apt-get update/upgrade cycle.
— James, Raspberry Pi Foundation (Trading)