Install Ubuntu 18.04 on Raspberry Pi 4 - Q-engineering
Q-engineering
Q-engineering
Go to content
images/empty-GT_imagea-1-.png
Ubuntu on Raspberry Pi 4

Install Ubuntu 18.04 + OpenCV + TensorFlow Lite on Raspberry Pi 4

Introduction.

This long article guides you through the installation of Ubuntu 18.04 on a Raspberry Pi 4. The Ubuntu version used has been specially developed for a Raspberry Pi 4. Now, for the first time, your Pi is running on a 64-bit operating system, while the well-known Raspbian operating system is still 32 bits.
At the same time, we will install OpenCV 4.2.0 and TensorFlow Lite 2.1.0 to see if a 64-bit operating system gives the promised performance boost.
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 hardware interface on the Raspberry Pi. It can be a bit of a headache to make everything work as we know it. We have chosen for the 18.04 (Bionic Beaver) version instead of the 19.10 (Eoan Ermine) because the support of the later will be dropped half 2020. The first step installing Ubuntu 18.04 is flashing it on a SD card.
64 Gbyte SD-micro.
We use Ubuntu on a 64 Gbyte SD card, so it will not become exhausted immediately. Flashing an image on an SD-micro card up to 32 Gbyte this is nothing special. However, above 32 Gbyte, Windows will always format the card with the exFAT file system, a conversion unsupported by your Raspberry Pi. The Raspberry Pi only works with a FAT32 system. Special software is needed to format your 64 or 128 Gbyte SD-micro card. We use MiniTool Partition wizard.
The slide show gives you an impression of how to format an SD card that has already been used for a Linux distribution. If you have a new card, you have probably one large section instead of the two we have. Before using the tool, be sure your working on the SD card, not on a Windows disk in your machine.
With the SD-card formatted, the Ubuntu image can now be flashed. Download the ubuntu-18.04.4-preinstalled-server-arm64+raspi3.img.xz image from the Ubuntu site. We use the 64-bit version for the Raspberry Pi 4. The image is best be flashed by Etcher.

Etcher

Overclocking.

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. Some issues with the Raspberry Pi 4 Gbyte RAM version have been reported. We assume that the Ubuntu Foundation will solve this problem as quickly as possible. In the meantime, limit the RAM usage to 3 Gbytes with the line total_mem=3072.

User config

Install Ubuntu.

The SD card can now be inserted in the Raspberry Pi. Very important; you must have a working internet connection from the very start. 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 18.04 is installed properly.
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.

Swap

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.

htop

The final step is, of course, the installation of the beautiful Raspberry desktop screen we have created for you. You can download it here.

Rubuntu

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.

Ubuntu desktop

Install OpenCV 4.2.1.

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.2.1 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 python-dev python-numpy
$ 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.2.0
$ cd ~
$ wget -O opencv.zip https://github.com/opencv/opencv/archive/4.2.0.zip
$ wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.2.0.zip
# upzip both packages
$ unzip opencv.zip
$ unzip opencv_contrib.zip
# rename the folders
$ mv opencv-4.2.0 opencv
$ mv opencv_contrib-4.2.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.
$ 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.

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
$ sudo apt-get update

You can now check your installation in Python 3. It all speaks for itself.

OpenCV succes

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 ~
$ git clone https://github.com/tensorflow/tensorflow
$ cd tensorflow
$ ./tensorflow/lite/tools/make/download_dependencies.sh
$ ./tensorflow/lite/tools/make/build_aarch64_lib.sh
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.

Raspicam.

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

Start_x

The SD card can now be reinstalled in the Raspberry Pi 4. After booting up, 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 will have a working raspistill and raspivid soon.
$ sudo apt-get install vokoscreen
Deep learning examples for Raspberry Pi
Raspberry and alt
Install 32 OS
Raspberry Pi 4
Install 64 OS
Back to content