Install Ubuntu 20.04 on Raspberry Pi 4 - Q-engineering
Go to content
Ubuntu on Raspberry Pi 4

Install Ubuntu 20.04 + OpenCV + TensorFlow (Lite) on Raspberry Pi 4

Raspberry 64-bit OS
Ubuntu 18.04

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 is evolving into a 64-bit operating system. In about a year, the 32-bit operating system will be replaced by the faster 64-bit version. The Raspberry Foundation recently released a more than functional beta version. It's lightweight and fast. The instruction manual is on this page.

Second, Ubuntu 20.04. The 20.04 version of Ubuntu has a special release for the Raspberry Pi 4. It can be installed easily with the Ubuntu option in the Raspberry Pi imager. It has long life support in contrast to the newest 20.10 version, released on 22th of October 2020.

Third, already mentioned Ubuntu 20.10. It comes with a desktop version that requires at least 4 GB of RAM on your Raspberry Pi 4 or Pi 400. If you have the 1 or 2 GB RAM version, install the Ubuntu 20.10 server. There are some minor improvements in this version compared to 20.04. On the other hand, you only have nine months of support on 20.10. Follow the instructions on this site if you want to install 20.10 instead of 20.04. Once Ubuntu is installed, you can continue to the overclocking part.

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 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.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. Let's begin with flashing Ubuntu 20.04 on an SD card.
Flashing Ubuntu 20.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.


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).

Exploder SD-card

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.


Install Ubuntu.

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.
Memory swapping.
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
ubuntu@ubuntu:$ startx
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
$ reboot
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.0.

The next step in our journey to the fastest deep learning app on a bare Raspberry Pi 4 is the installation of OpenCV. We will use the newest 4.5.0 version. For those who have done this before, there are not many surprises. Be sure your swap space, set in init-zram-swapping, is more then 3 Gbyte, otherwise the installation will fail. 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.
# download OpenCV 4.5.0
$ cd ~
$ wget -O
$ wget -O
# upzip both packages
$ unzip
$ unzip
# rename the folders
$ mv opencv-4.5.0 opencv
$ mv opencv_contrib-4.5.0 opencv_contrib
# finally, make the build directory
$ cd opencv
$ mkdir build
$ cd build
Build Make.
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.
        -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_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 BUILD_opencv_python3=TRUE \
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.

Regex error

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.

Cmake succes

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.

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.
$ cd ~
# dependencies
$ sudo apt-get install wget curl
# download TensorFlow 2.4.0
$ wget -O
# unpack and give the folder a convenient name
$ unzip
$ mv tensorflow-2.4.0 tensorflow
$ cd tensorflow
# get the dependencies
$ ./tensorflow/lite/tools/make/
# build TensorFlow Lite
$ ./tensorflow/lite/tools/make/
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.4.0.

If you want to install TensorFlow 2.4.0 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.4.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.
# get a fresh start
$ sudo apt-get update
$ sudo apt-get upgrade
# install the dependencies (if not already onboard)
$ sudo apt-get install python3-pip gfortran
$ sudo apt-get install libhdf5-dev libc-ares-dev libeigen3-dev
$ sudo apt-get install libatlas-base-dev libopenblas-dev libblas-dev
$ sudo apt-get install liblapack-dev
$ sudo pip3 install pybind11
$ sudo pip3 install Cython==0.29.21
# install h5py with Cython version 0.29.21
$ sudo pip3 install h5py
# upgrade setuptools -> 51.0.0
$ sudo pip3 install --upgrade setuptools
# install gdown to download from Google drive
$ pip3 install gdown
# copy binairy
$ sudo cp /home/ubuntu/.local/bin/gdown /usr/local/bin/gdown
# download the wheel
$ gdown
# install TensorFlow (± 63 min @1950 MHz)
$ sudo -H pip3 install tensorflow-2.4.0-cp38-cp38-linux_aarch64.whl
Depending on the clock frequency and available RAM, it may take several hours to compile. Once done, you can check your installation, as shown below.
If you like to build fast C ++ applications like us, you might want to install the TensorFlow C ++ API.
# get a fresh start
$ sudo apt-get update
$ sudo apt-get upgrade
# the dependencies
$ sudo apt-get install wget libhdf5-dev libc-ares-dev libeigen3-dev
$ sudo apt-get install libatomic1 libatlas-base-dev zip unzip
# install gdown to download from Google drive (if not already done)
$ pip install gdown
# copy binairy
$ sudo cp /home/ubuntu/.local/bin/gdown /usr/local/bin/gdown
# download the tarball
$ gdown
# unpack the ball
$ sudo tar -C /usr/local -xzf libtensorflow_cp38_64OS_2_4_0.tar.gz
You should end up having your TensorFlow library installed at the /usr/local/lib location and the header files in the folder usr/local/include/tensorflow/c.

TensorFlow C++ headers

Final comments.
If you plan to build TensorFlow from scratch on a Raspberry Pi running Ubuntu 20.04, note the absence of Python2, a requirement of Bazel. Without Python2 Bazel cannot be installed. The only solution we found effective seems to be a symbolic link to Python3 as shown below. Once the symlink is established, the procedure is identical to the one we used for the Raspberry Pi 64-bit OS. Please pay attention to your memory size. Memory and swap together need to be more than 6 Gbyte. See the remarks regarding this topic above.
# symlink
$ sudo ln -s /usr/bin/python3 /usr/bin/python


Lately, the Raspberry Pi BM2711 userland video drivers have been ported to Ubuntu. This makes the use of the popular Raspicam on an Ubuntu platform possible. To install the Raspicam in Ubuntu, the first this action is adding a line in the config.txt on the SD-card. The procedure is identical to the one used for the overclocking. Insert the SD card in your PC and open config.txt with Notepad++. Use no other editor because Notepad++ keeps the Linux line endings intact.

Edit config.txt

In config.txt add the line start_x=1 in the end section and save the file


Insert the SD card now in the Raspberry Pi 4. After booting, all you have to do is giving the well-known update and upgrade commands to install all the drivers.
Keep in mind that only the Raspicam drivers will be installed, not the popular diagnostic tools like raspistill or raspivid. You can capture images or videos by other third-party software like vokoscreen, or write your OpenCV application. Please note, the new Raspberry 64 OS have a working raspistill and raspivid.
$ sudo apt-get install vokoscreen
Deep learning examples for Raspberry Pi
Install 64 OS
Install 32 OS
Raspberry and alt
Raspberry Pi 4
Jetson Nano
Back to content