Install 64 bit OS on Raspberry Pi 4 + USB boot
Last updated: June 22, 2021
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 2021-05-07-raspios-buster-arm64 version which can be downloaded here.
The slideshow below shows the ease of the entire image writing process.
- Select the operating system dialogue.
- In the drop-down list select the custom option
- Find the 2021-05-07-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
# 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 chmod +x /usr/bin/zram.sh
$ 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.
The other reason why USB booting is so popular is that the used SSD cards wear out much less than their smaller siblings the SD cards.
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 (€ 32) with an Inateck 2.5 hard drive enclosure (€ 15).
Raspberry Pi 4 USB boot.
The Raspberry Pi boots up smartly. Immediately after startup, a small program, located in the EEPROM, loads only the drivers for the I/O. That's why, for example, you still see a diagnostic screen even if no SD card inserted. This program then tries to load the operating software in the Raspberry Pi. Once done, it transfers the control to the Raspberry Pi and stays idle for the rest of the time.
By far, the easiest way to enable the USB boot is by using the Raspberry Pi Imager. Since version 1.6, you can find dedicated EEPROM settings for your USB boot. Take a look at the slide show below.
Insert an (small) SD card in your PC and flash the EEPROM image of your choice. Either the boot order first the SD-card, next USB, or the one selected in the slideshow, USB first, SD card next can be used. Boot with the newly flashed SD card in your Raspberry Pi 4 and the EEPROM updates. Once the screen turns green, your EEPROM is ready. You can always change your boot order by simply repeating the above procedure with different settings.
The remaining procedure is identical to the standard SD card installation, described in the first paragraph. Flash the Raspberry Pi 64 OS on the USB device using the Imager, remove the SD card from your Raspberry Pi 4 and plug the USB device into a (blue) USB 3.0 connector. Now, follow the usual installation sequence.
Keep an eye on the power consumption of the USB device. One SSD card is no problem but more requires a separate power hub.
The most common cause of failure is the USB to SATA (SSD card) adapter not supporting the UASP protocol. UASP stands for USB Attached SCSI Protocol, an extremely fast data transfer protocol used by the Raspberry Pi. Several adapters like the popular JMicron having trouble with some of the UASP commands. You see slow performances, frequent disconnections, or error reports. Even attached to USB 2.0 will not solve the issues, because the Linux kernel still uses UAS commands when it sees an USB 3.0 device.
The only solution is to disable the UASP transfers with command right after boot. In the command.txt file, you need to provide the hardware ID of your USB drive. You can get it by connecting the SSD drive to the USB plug of a Raspberry Pi that is still working from the SD card.
# Get the verdor and product ID
The found number is given as parameter in the command usb-storage.quirks=xxxx:yyyy:u
This command must set as the very first argument in the long line found in /boot/cmdline.txt.
Find the file in the boot section of the USB folders. See the screen dump below from an RPi booted from a regular SD card. Connects the USB device after booting.
Naturelly, you can also use the nano editor.
After all the effort, most people only want to know one thing: was it worth it? The answer is a simple yes.
Not only do you have a lot more memory space, but the SSD card with its distributed wear also lasts much longer than SD cards.
And SSD cards are much faster. You can find a benchmark tool on the Raspberry menu. Once executed, you can request a report.
We investigate three different options. First, the standard SD card, which is, of course, the slowest. No surprises here.
The second option is an SSD card with a SATA to USB converter that does not support UASP; much improvement.
The third option is the same SSD card, but now with a UASP supported converter. Obvious being the fastest.
It has to be said, without UASP, you still have a lot of speed gains.
Two final remarks.
In the Raspberry diagnostic report, you see IOPS. It stands for I/O operations per second. As you can see, one read or write transfer is 4 KByte. You may multiply these figures to get the transfer rate in (Mega)bytes per second.
Don't fool yourself. These impressive transfer rates only reflect reading or writing data from ROM, not the working memory (RAM). Ultimately, your Raspberry Pi will run snappier, but, for instance, deep learning apps with their models in RAM won't be any faster using an SSD card.
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.