Install 64 bit OS on Raspberry Pi 4 + USB boot
This article will help you install the new Raspberry 64-bit operating system on Raspberry Pi 4. Although written for the Raspberry Pi 4, the guide can also be used without any modification for the Raspberry 3. The Raspberry 2 and 1 have 32- bit CPUs and therefore are unable to run a 64-bit OS. The 64-bit operating system is, of course, the natural next step in the ongoing Raspberry Pi evolution. The operating system will be further developed over time. For now, the Raspberry Foundation has released a more than functional beta version to get started.
Why not use Ubuntu?
Well, there is nothing wrong about Ubuntu. The Raspberry Imager has even options to install different Ubuntu versions.
However, Ubuntu being a mature operating system and suitable for a wide range of processors, is not what you can say very lightweight. The new 64-bit Raspberry operating system outperforms Ubuntu systematically when it comes to speed. Especially in our deep learning applications. That's the reason why we choose to run the 64-bit Raspberry OS, even while it is still in development.
The first step in installing a 64-bit OS on your Raspberry Pi 4 is to download the new Raspberry Pi Image Tool from this site. The Image Tool can write an operating system of your choice on an SD card. At the same time, it will format the card into the correct ext4 for the Raspberry Pi, even a 64, 128 or 256 GByte card. Different image formats are supported, making this tool a better alternative to balena Etcher.
The second step is downloading the correct 64-bit OS for your Raspberry Pi 4. At the moment, the latest version is the 2020-08-20-raspios-buster-arm64 version which can be downloaded here.
The slideshow below shows how easy the whole image writing process is.
- Select the operating system dialogue.
- In the drop-down list select the custom option
- Find the 2020-08-20-raspios-buster-arm64.zip you just downloaded.
- Select the SD card.
- Here, we used a 64 Gbyte SD card.
- Start the erase, format and writing activities in one go.
- You can follow the progress.
After writing your 64-bit image, you can insert the SD card into the Raspberry and boot. Follow all instructions and make sure that the startup procedure is checked for software updates. As the 64-bit operating system is under development, there will inevitably be some new ones.
Time for a version check now. Please give the command uname -a and check your version.
You also need to check your C++ compiler version with the command gcc -v. It must also be an aarch64-linux-gnu version, as shown in the screenshot. Earlier 64-bit versions of the Raspberry OS still had an old 32-bit C++ compiler (arm-linux-gnueabihf). You must have a 64-bit C++ compiler as we are going to build software from scratch. Otherwise, there is no point in building a 32-bit version on a 64 machine.
The last action before using the 64-bit operating system on the Raspberry Pi 4 is changing the memory swapping. Memory swapping increases your working memory by temporarily placing infrequently used parts in a file on your SD card. In this way, memory is released. Once the tasks are completed, the original content is replaced from the file into memory, hence the name 'swapping'.
There are two minor issues with switching memory to the SD card. First, it is not a very fast mechanism because transferring data to flash memory is slow. Second, it can quickly wear out your SD card due to the limited number of write actions a flash memory can endure.
When you occasionally switch between web pages in Chromium, data is only transferred once. This has no impact on the lifespan of the flash memory whatsoever. On the other hand, when you compile a large software package, such as TensorFlow, you may have huge amounts of data constantly transferring between flash and memory, causing your SD card to deteriorate very quickly. See the Protect the Raspberry Pi 4 SD flashcard page for more inside information on this topic.
To solve the problems with memory swapping, we use zram. Instead of writing parts of the memory to the flashcard, zram compresses these parts to a zip file and saves the result back to RAM. The difference in the size of the compressed data and the original size is the amount of memory that is released. That may sound cumbersome, but in practice, it is a much faster mechanism than writing to flash memory. The only limitation is ultimately the size of your RAM when there is no more space to store even larger compressed files. On the other hand, the original Raspberry Pi dphys-swap file is limited to 2 Gbyte flash memory.
Another discussion is whether we need memory exchange in the first place. If you have 8 GB of RAM onboard, there is rarely any reason to swap memory. You can try to work without. You probably never have a problem. If you have 2 or 4 Gbyte, you better install zram.
Follow the next steps to install zram on your 64-bit operating system. Note the ampersand on the last line. Do not forget this character, otherwise the bootup will not continue.
# remove the old dphys version
$ sudo /etc/init.d/dphys-swapfile stop
$ sudo apt-get remove --purge dphys-swapfile
# install zram
$ sudo wget -O /usr/bin/zram.sh https://raw.githubusercontent.com/novaspirit/rpi_zram/master/zram.sh
$ sudo chmod +x /usr/bin/zram.sh
# set autoload
$ sudo nano /etc/rc.local
# add the next line before exit 0
# save with <Ctrl+X>, <Y> and <Enter>
Last action will be setting the upper limit zram will use. This can be done with the next commands.
$ sudo nano /usr/bin/zram.sh
# alter the limit with * 2
mem=$(( ($totalmem / $cores)* 1024 * 2))
# save with <Ctrl+X>, <Y> and <Enter>
$ sudo reboot
After a reboot, all actions have now been taken and the 64-bit operating system is up and running on Raspberry Pi 4.
Overclocking the 64-bit operating system can be done in the same way as with the 32-bit version. For more information see: Safe overclocking of the Raspberry Pi 4 to 2 GHz. Keep in mind that you don't have NOOBS now with its easy to use recovery menu. If the system freezes or crashes due to overclocking, the only way to change the frequency is to use another computer that can modify the /boot/config.txt file on the SD card.
The Raspberry 64-bit operating system is still under development. There will undoubtedly be many improvements planned.
It is good practice to check for updates every week with known commands.
# refresh your OS
$ sudo apt-get update
$ sudo apt-get upgrade
USB boot on Raspberry Pi 4.
Why do Raspberry Pi enthusiasts get excited when they talk about USB booting? Because it makes the Raspberry Pi significantly faster. Or, to be more precise, the memory transfers with the flash (ROM). The RPi boots faster, especially with a 64-bit operating system because it is an uncompressed kernel. Or load pages from the cache into Chromium. Ergo, anything, where a lot of data is transferred with the flash memory, will be faster. However, deep learning applications, for example, will not run faster because they mainly use RAM. You won't stream YouTube better either; this has everything to do with your internet bandwidth, not with your flashcard.
USB booting requires dedicated hardware. A simple USB 3.0 stick can do the job, but the transfer speed is the same as that of your SD card, around 80-100MB/s. Net, it will be a little worse as there is about 0.5 second overhead when initializing the USB controller on the RPi board.
Only with a fast SSD drive, with 400 MB/s or more, will you fully benefit from the USB boot. These SSD drives don't come as USB devices. An extra USB 3.0 adapter is needed to connect it to your Raspberry. Make sure to use an adapter suitable for UASP transfers. It is the new and fastest protocol support by USB 3.0. The last point to consider is power consumption. A relatively small SSD drive can be powered via the USB port. It is preferred because otherwise, you need also an additional power supply. A relatively inexpensive combination is the Kingston 240GB A400 SATA 3 (€ 35) with an Inateck 2.5 hard drive enclosure (€ 15).
Prepare the Raspberry Pi 4.
First, you have to alter some settings in the Raspberry Pi to boot from USB. Please give the following commands. Most important is the full-upgrade.
# refresh your OS with the latest firmware
$ sudo apt-get update
$ sudo apt-get full-upgrade
The next action is to change the status of FIRMWARE_RELEASE_STATUS from critical to stable in the /etc/default/rpi-eeprom-update file. Open nano with the given command, alter the status and close with <Ctrl>+<X>, <Y>, <ENTER>. Once modified, you can download the new bootloader. Activate the new loader with a reboot. You may want to verify your new bootloader. The given date must be June 15th 2020, being the first USB release, or later. The slide show below will guide you.
$ sudo nano /etc/default/rpi-eeprom-update
$ sudo rpi-eeprom-update -d -a
$ sudo reboot
# after the reboot
$ vcgencmd bootloader_version
Prepare the SSD drive.
By far, the easiest way to prepare your SSD drive is using the SD card copy utility that comes with the Raspberry Pi operating system. It copies all the files and settings on your SSD drive so that, once you boot from the drive, you will have the same contents, as left before migrating to the USB boot drive.
Another nice feature of the SD Card Copier is that it not only copies all files, but also takes care of the partitions with their different file systems. The 500 MByte FAT32 boot partition and the Ext4 rootfs will be created automatically, alongside any other found partition.
The usage is simple. First, select your SD card. Usually, the name contains something like /dev/mmcblk0. Then pick your SSD drive from the second drop-down list. Start copying with the Start button. Connect your SSD drive to the blue USB ports as only these will support USB 3.0. The other blacks ones have USB 2.0, which will severely limit the speed.
The whole process will take some time, depending on the size of your SD card. It can take several hours, worse case.
There are other and faster ways to load the software on your SSD card. However, these procedures involve manually copying all kind of files from the latest GitHub repo to the root section of your SSD drive. It can be an adventure, especially when you have to find the latest 64-bit versions.
Better to be patient and be sure you have everything working at the end of the day.
Once the SD Card Copier has done its work, you can choose how you want to boot your Raspberry Pi in the future. Most people remove the SD card from their RPi, connect the SSD drive to one of the USB 3.0 ports and apply power. Since no other bootable medium is available apart from the SSD drive, you will boot from this device.
For advanced users, there is the bootloader configuration file dictating how the Raspberry Pi will boot. In the rpi-eeprom-config file, you can define two media (SD, USB, UART, Network) trying to boot your RPi. If the first, for instance, the SD card, fails, it will try to boot from the second, which can be your USB. This way, your SD card can function as a sort of backup when the SSD drive fails.
Note, however, once booted, you will only work with that device. In other words, if, by misfunction, you boot from the 'old' SD card instead of the 'new' SSD drive, you will be missing all the work on the SSD drive since the 'old' SD card file structure is loaded. This situation can occur if your SSD drive is still booting internally or if its power is not available yet. You have to take precautions to fix this. A certain number of boot retries can be the solution.
Please read the instructions on this page very carefully before you begin to modify your rpi-eeprom-config file.
$ sudo -E rpi-eeprom-config --edit
# modify BOOT_ORDER=0xf41# first SSD (USB), second SD card$ BOOT_ORDER=0xf14# save <Ctrl>+<X>, <Y>, <Enter>$ sudo reboot
Again, this is for advanced users. Most people will just remove the SD card from their Pi and enjoy the improved performance.