Install Ubuntu 20.04 + OpenCV + TensorFlow (Lite) on Raspberry Pi 4
Last updated: September 28, 2022
Choose an OS.
There are several 64-bit operating systems available for the Raspberry Pi 4. We've selected four of the most popular for you.
First, our favourite, Raspberry 64-bit OS. The Raspberry Pi has evolved from a 32-bit into a 64-bit operating system. The Raspberry Foundation recently released the Bullseye version. It's lightweight and fast. The instruction manual is on this page.
Second, Ubuntu 22.04. The 22.04 version of Ubuntu has a special release for the Raspberry Pi 4. It is installed easily with the Ubuntu option in the Raspberry Pi imager. It has long life support and, most important, works on boards with only 2 GB of RAM.
Third, Ubuntu 20.04. It ships with a desktop version that requires at least 4 GB of RAM on your Raspberry Pi 4 or Pi 400. It's installed by first set-up the headless server and later the graphical user interface.
Fourth, Ubuntu 18.04. As is known, Ubuntu is a general-purpose operating system. It works well on a Raspberry Pi 4. You can't call it a lightweight system; it uses a fair amount of resources. It is sometimes a bit slower than the Raspberry 64-bit operating system. The same goes for the 20.04 version by the way. This page will guide you through the installation.
This long article guides you through the installation of Ubuntu 22.04 or 20.04 on a Raspberry Pi 4. The Ubuntu version used has been specially developed for a Raspberry Pi 4. At the same time, we will install OpenCV 4.5.0, TensorFlow 2.5.0 / 2.4.1 / 2.4.0 and TensorFlow Lite to see if a 64-bit operating system gives the promised performance boost. At the end of this article the Raspicam is installed.
A word of caution. The installed Ubuntu (aarch64-linux-gnu) is based on an ARM core. Not all Linux software may be running on this version because it may be developed for an Intel processor (x86_64-linux-gnu). Another point is the limited resources on the Raspberry Pi. It's still not a PC with a powerful multi-core processor and fast graphics card.
Flashing Ubuntu 22.04 on a SD card.
By far the easiest option to flash an image to an SD card is to use the Raspberry Pi Imager tool. Download it for this site. It will not only flash your operating system, but also format the card. Even large 64, 128 or even 256 GByte cards, making this tool a much better alternative to balena Etcher.
Once flashed, you can consider overclocking your Raspberry Pi. Follow the instructions on the overclock paragraph or proceed by installing OpenCV 4.5, if needed.
We advise you to use Ubuntu 22.04. It is the latest version, easier to install and requires less RAM.
If you still want to install Ubuntu 20.04, here are the instructions. Please check before you start the size of the RAM. You will need 4 GB or more.
You can download ubuntu-20.04.4-preinstalled-server-arm64+raspi.img.xz form the https://old-releases.ubuntu.com/releases/focal/ page.
If your Raspberry Pi has proper cooling, you might consider overclocking. Ubuntu will run somewhat faster. However, don't push your luck too far. During intensive calculations, for instance, running deep learning models, your system may crash. We use 1925 MHz as an absolute maximum. Above this number, your system will still start up, but there is a chance that it will crash at a certain point. For example, at 1950 MHz, we could not properly execute our TensorFlow Lite example. More information on our overclocking page.
To overclock, you have to add a few lines in the usercfg.txt file in the root folder of your SD-card. Please, use only Notepad++ as it supports Linux line endings of text files (\n) instead of Windows (\r\n).
Add the following lines in your usercfg.txt. If you are not comfortable with the 1925 MHz, or if you have doubts about your cooling, you can lower this number. Your Raspberry Pi 4 runs default at 1500 MHz, so these extra lines are not necessary if you run at that speed. By the way, you can always modify the figures at any time.
The SD card can now be inserted in the Raspberry Pi. Very important; do not connect the RPi to the internet for now. Power up and see many lines scroll across your screen during the installation of Ubuntu for the first time. After about 30 seconds you end up with this screen. If you are missing the prompt, or the login line, give a <enter>.
Log in with ubuntu. Password is also ubuntu. You will now be asked to provide a new password, so let's do that.
After a few lines you see the ubuntu prompt and your Ubuntu 20.04 is installed properly.
Next, connect your Raspberry Pi to the internet. It will immediately start updating the software in the background. It can take a few minutes. If the $ sudo apt-get upgrade command can be executed you know all software packages are present and you can start installing the desktop.
Before the desktop can be installed, there are a few administrative tasks to do. One very important thing is the memory swapping. You must give Ubuntu more breathing space than a Raspbian OS. We use zram-config to take care of the swapping. Please follow the next commands.
The last line installs your Ubuntu desktop. This will take about 20 minutes to complete.
# sure there are upgrades, so get them
ubuntu@ubuntu:$ sudo apt-get update
ubuntu@ubuntu:$ sudo apt-get upgrade
# install zram
ubuntu@ubuntu:$ sudo apt-get install zram-config
# install your desktop
ubuntu@ubuntu:$ sudo apt-get install ubuntu-desktop
The desktop must be start-up once with the command $ startx. Next time it is loaded right away. With the desktop now installed, you can customize it the way you like. Enable WiFi etc.
Before we can fully enjoy Ubuntu, we must increase the standard swap space. Enter the following command in a terminal window.
$ sudo nano /usr/bin/init-zram-swapping
# after saving the modifications, restart
Add a multiplier (* 5) at the end of the mem.... line. This gives you about 4.5 Gbyte of memory swap.
Save and close with <Ctrl+X>, <Y>, <Enter>. Before the changes can take effect, reboot.
Next time, you can check your system with the command htop. You get the following screen, where at the top the actual and maximum swap is given.
Now you have a fully functional and pretty fast 64-bits Ubuntu machine on your Raspberry Pi 4. You can do what you like, LibreOffice, browsing, coding, building apps, you name it. And, you will not easily limited with 64 Gbyte memory onboard. By the way, your clock speed can be checked with the command: $ lscpu | grep MHz.
Install OpenCV 4.5.2.
The next step in our journey is the installation of OpenCV. We will use the newest 4.5.2 version. For those who have done this before, there are not many surprises.
Make sure your swap space is adequate for the task. OpenCV 4.5.0 and 4.5.1 are well served with a total amount of 5 GByte. Depending on the size of the RAM on your Raspberry Pi, the swap space, set in init-zram-swapping, needs to be increased, otherwise the installation will fail.
OpenCV 4.5.2 requires even more RAM. In this case, 6.5 GByte is needed. With small RAM sizes on board, you'd better temporarily install a swap mechanism on the SD card rather than ask zram to process 5 GByte of compressed data on a 1 GByte device. Follow the steps given here to install dphys-swapfile. Don't forget to remove it afterwards so as not to use the SD card unnecessarily.
Please note that the Raspberry Pi 4 ARMv8 core no longer supports VFPV3, but instead has FP16, smaller 16-bits floating point registers on board. To keep in mind when providing the options on the CMake command line. Let start, as usual, with the installation of the dependencies.
# to get your administation in order
$ sudo apt-get update
$ sudo apt-get upgrade
# now we can get the whole lot$ sudo apt-get install build-essential cmake gcc g++ git unzip pkg-config
$ sudo apt-get install libjpeg-dev libpng-dev libtiff-dev
$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev
$ sudo apt-get install libgtk2.0-dev libcanberra-gtk*
$ sudo apt-get install libxvidcore-dev libx264-dev
$ sudo apt-get install python3-dev python3-numpy python3-pip
$ sudo apt-get install libtbb2 libtbb-dev libdc1394-22-dev
$ sudo apt-get install libv4l-dev v4l-utils
$ sudo apt-get install libopenblas-dev libatlas-base-dev libblas-dev
$ sudo apt-get install liblapack-dev gfortran libhdf5-dev
$ sudo apt-get install libprotobuf-dev libgoogle-glog-dev libgflags-dev
$ sudo apt-get install protobuf-compiler
With the dependencies in place, it is time to download OpenCV and unzip the packages, followed by some administartion.
This is a important step. Here you tell CMake what, where and how to make OpenCV on your Ubuntu system.
Note, there are only bare spaces before the -D flags, not tabs. Keep also a single space between the -D and the argument. Otherwise, CMake will misinterpreter the command. The two last dots are not a typo, it tells CMake where the CMakeLists.txt file is located; one directory up. The OPENCV_GENERATE_PKGCONFIG flag must be set because we are going to use the package config when compiling TensorFlow Lite C++ API.
$ cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
-D ENABLE_NEON=ON \
-D BUILD_TIFF=ON \
-D WITH_FFMPEG=ON \
-D WITH_GSTREAMER=ON \
-D WITH_TBB=ON \
-D BUILD_TBB=ON \
-D BUILD_TESTS=OFF \
-D WITH_EIGEN=OFF \
-D WITH_V4L=ON \
-D WITH_LIBV4L=ON \
-D WITH_VTK=OFF \
-D OPENCV_ENABLE_NONFREE=ON \
-D INSTALL_C_EXAMPLES=OFF \
-D INSTALL_PYTHON_EXAMPLES=OFF \
-D BUILD_NEW_PYTHON_SUPPORT=ON \
-D BUILD_opencv_python3=TRUE \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D BUILD_EXAMPLES=OFF ..
Please note the absence of the ENABLE_VFPV3=ON flag used by the building of OpenCV for the Raspbian 32-bits ARMv7 version.
If you had used this flag, cmake would generate erros. See the screendum below.
The error is quite misleading; Regex: "command line option. * Is valid for. * But not for C ++ '. This suggests an incompatibility of compilers instead of an unavailable option. 'Option VFPV3 not applicable' would be a better warning. OpenCV notorious gives these types of warnings, good to keep in mind. You can simply remove all your flags from the cmake command line and see if the build now succeeds. If so, add your flags one by one. This way, your problem will be located soon.
Hopefully, everything went well and CMake comes with a report that looks something like the screenshot below.
With all compilation directives in place, you can start the build with the following command. This will take a while (± 55 min). After the compilation, the libraries can be installed and added to the database.
$ make -j4
# install the libraries
$ sudo make install
$ sudo ldconfig
# cleaning (frees 300 KB)
$ make clean
$ sudo apt-get update
You can now check your installation in Python 3. It all speaks for itself.
If you had to reinstall dphys-swapfile, it's time to uninstall it again. This way you will extend the life of your SD card.
# remove the dphys-swapfile (if installed)
$ sudo /etc/init.d/dphys-swapfile stop
$ sudo apt-get remove --purge dphys-swapfile
Install TensorFlow Lite.
With OpenCV on place, it is now time to install TensorFlow Lite. We only install the C++ API libraries. We need them later to build the app. The procedure is very simple. Just clone the GitHub repository and run two scripts. The commands are listed below. For those who have previously installed TensorFlow Lite on a Raspberry Pi, note the subtle difference. Here we use build_aarch64_lib because Ubuntu is a 64-bit operating system compared to the 32-bit Raspbian versions, build_rpi_lib.
The last step is building the TensorFlow Lite flat buffers. Please use the following commands.
$ cd ~/tensorflow/tensorflow/lite/tools/make/downloads/flatbuffers
$ mkdir build
$ cd build
$ cmake ..
$ make -j4
$ sudo make install
$ sudo ldconfig
If everything went well, you should have the two libraries and two folders with header files as shown in the slide show.
Install TensorFlow 2.5.0 / 2.4.1 / 2.4.0
If you want to install TensorFlow 2.5.0 or a version 2.4 on Ubuntu 20.04, please follow the next instructions. Note, the used python wheel and the tarball are both exclusive builds for Ubuntu 20.04 on a Raspberry Pi 4. You cannot use these packages on other machines, such as Intel or AMD. Ubuntu 20.04 has Python3 release 3.8.2. When complied on a Raspberry Pi 4, it is the linux_aarch64 version. Hence the name of the TensorFlow wheel: tensorflow-2.5.0-cp38-cp38-linux_aarch64.whl. If you like to install another version, please look at our GitHub page here where you find the previous wheels.
Since numpy upgrades to version 1.20, TensorFlow is experiencing issues with this latest version. It became a real struggle to port TensorFlow to numpy 1.20. Now, with TensorFlow version 2.5.0, it seems to have finally succeeded. However, there are still a few reports of problems. To be on the safe side, we are now using numpy version 1.19.5, so you have a guarantee that everything will work fine. With the next release, we will build the wheel with the latest numpy version 1.20.