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

Raspberry 64-bit OS
Ubuntu 20.04

Choose an OS.

There are several 64-bit operating systems available for the Raspberry Pi 4. We've selected three 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. This page will guide you through the installation.

Third, 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.
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. At the same time, we will install OpenCV 4.5.0, TensorFlow 2.4.0 / 2.4.1 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. We have chosen for the 18.04 (Bionic Beaver) version instead of the 19.10 (Eoan Ermine) because the support of the later is dropped half 2020. The first step installing Ubuntu 18.04 is flashing it on a SD card.
Flashing Ubuntu 18.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.
Now, download the ubuntu-18.04.5-preinstalled-server-arm64+raspi3.img.xz image from the Ubuntu site and follow the slideshow below.

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

none

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.
Next, connect your Raspberry Pi to the internet. It will immediately start catching up two and a half years of updates in the background. It can take more than half an hour. So wait patiently. 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.

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.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 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.
# check your memory first
$ free -m
# you need at least a total of 6.5 GB!
# if not, enlarge your swap space as explained earlier
# download the latest version
$ cd ~
$ wget -O opencv.zip https://github.com/opencv/opencv/archive/4.5.2.zip
$ wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.5.2.zip
# unpack
$ unzip opencv.zip
$ unzip opencv_contrib.zip
# some administration to make live easier later on
$ mv opencv-4.5.2 opencv
$ mv opencv_contrib-4.5.2 opencv_contrib
# clean up the zip files
$ rm opencv.zip
$ rm opencv_contrib.zip
# download the latest version
$ cd ~
$ wget -O opencv.zip https://github.com/opencv/opencv/archive/4.5.1.zip
$ wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.5.1.zip
# unpack
$ unzip opencv.zip
$ unzip opencv_contrib.zip
# some administration to make live easier later on
$ mv opencv-4.5.1 opencv
$ mv opencv_contrib-4.5.1 opencv_contrib
# clean up the zip files
$ rm opencv.zip
$ rm opencv_contrib.zip
# download the latest version
$ cd ~
$ wget -O opencv.zip https://github.com/opencv/opencv/archive/4.5.0.zip
$ wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.5.0.zip
# unpack
$ unzip opencv.zip
$ unzip opencv_contrib.zip
# some administration to make live easier later on
$ mv opencv-4.5.0 opencv
$ mv opencv_contrib-4.5.0 opencv_contrib
# clean up the zip files
$ rm opencv.zip
$ rm opencv_contrib.zip
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
# cleaning (frees 300 KB)
$ make clean
$ sudo apt-get update
You can now check your installation in Python 3. It all speaks for itself.

OpenCV_4_5_0_Ubuntu_18_04

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.
$ cd ~
# the dependencies
$ sudo apt-get install wget curl
# download TensorFlow 2.5.0
$ wget -O tensorflow.zip https://github.com/tensorflow/tensorflow/archive/v2.5.0.zip
# unpack and give the folder a convenient name
$ unzip tensorflow.zip
$ mv tensorflow-2.5.0 tensorflow
$ cd tensorflow
# get the dependencies
$ ./tensorflow/lite/tools/make/download_dependencies.sh
# build TensorFlow Lite
$ ./tensorflow/lite/tools/make/build_aarch64_lib.sh
$ cd ~
# the dependencies
$ sudo apt-get install wget curl
# download TensorFlow 2.4.1
$ wget -O tensorflow.zip https://github.com/tensorflow/tensorflow/archive/v2.4.1.zip
# unpack and give the folder a convenient name
$ unzip tensorflow.zip
$ mv tensorflow-2.4.1 tensorflow
$ cd tensorflow
# get the dependencies
$ ./tensorflow/lite/tools/make/download_dependencies.sh
# build TensorFlow Lite
$ ./tensorflow/lite/tools/make/build_aarch64_lib.sh
$ cd ~
# the dependencies
$ sudo apt-get install wget curl
# download TensorFlow 2.4.0
$ wget -O tensorflow.zip https://github.com/tensorflow/tensorflow/archive/v2.4.0.zip
# unpack and give the folder a convenient name
$ unzip tensorflow.zip
$ mv tensorflow-2.4.0 tensorflow
$ cd tensorflow
# get the dependencies
$ ./tensorflow/lite/tools/make/download_dependencies.sh
# build TensorFlow Lite
$ ./tensorflow/lite/tools/make/build_aarch64_lib.sh
FT_Lite_Rdy_Ubuntu_18_04

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.1 / 2.4.0.

If you want to install TensorFlow 2.4.0 or version 2.4.1 on Ubuntu 18.04, please follow the next instructions. Note, the used python wheel and the tarball are both exclusive builds for Ubuntu 18.04 on a Raspberry Pi 4. You cannot use these packages on other machines, such as Intel or AMD. Ubuntu 18.04 has Python3 release 3.6.9. When complied on a Raspberry Pi 4, it is the linux_aarch64 version. Hence the name of the TensorFlow wheel: tensorflow-2.4.0-cp36-cp36m-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. Finally, with TensorFlow version 2.5.0, it worked. It means that all older versions of TensorFlow still require version 1.19.
So start by setting the numpy version to 1.19.5. Then install h5py, which will use this numpy version. If you go the other way around, chances are you will get runtime errors.

TensorFlow 2.5.0 depends on h5py version 3.1.0. Unfortunately, the h5py version 3.1.0 cannot be easily installed on Ubuntu 18.04, or to be more precise, on an aarch64 with Python 3.6. See #1760. That's why we don't have a wheel for Ubuntu 18.04. Use TensorFlow 2.4.1 or switch to Ubuntu 20.04.
# get a fresh start
$ sudo apt-get update
$ sudo apt-get upgrade
# install pip and pip3
$ sudo apt-get install python-pip python3-pip
# remove old versions, if not placed in a virtual environment (let pip search for them)
$ sudo pip uninstall tensorflow
$ sudo pip3 uninstall tensorflow
# install the dependencies (if not already onboard)
$ sudo apt-get install 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
# upgrade setuptools 47.1.1 -> 57.0.0
$ sudo -H pip3 install --upgrade setuptools
# utmost important: use only numpy version 1.19.5
$ sudo -H pip3 install numpy==1.19.5
$ sudo -H pip3 install pybind11
$ sudo -H pip3 install Cython==0.29.21
# install h5py with Cython version 0.29.21 (± 6 min @1950 MHz)
$ sudo -H pip3 install h5py==2.10.0
# install gdown to download from Google drive
$ sudo -H pip3 install gdown
# download the wheel
$ gdown https://drive.google.com/uc?id=1qbjNluql9jA2HgEQnq5MYqb9XjLBMI0I
# install TensorFlow 2.4.1 (± 68 min @1950 MHz)
$ sudo -H pip3 install tensorflow-2.4.1-cp36-cp36m-linux_aarch64.whl
# get a fresh start
$ sudo apt-get update
$ sudo apt-get upgrade
# install pip and pip3
$ sudo apt-get install python-pip python3-pip
# remove old versions, if not placed in a virtual environment (let pip search for them)
$ sudo pip uninstall tensorflow
$ sudo pip3 uninstall tensorflow
# install the dependencies (if not already onboard)
$ sudo apt-get install 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
# upgrade setuptools 47.1.1 -> 57.0.0
$ sudo -H pip3 install --upgrade setuptools
# utmost important: use only numpy version 1.19.5
$ sudo -H pip3 install numpy==1.19.5
$ sudo -H pip3 install pybind11
$ sudo -H pip3 install Cython==0.29.21
# install h5py with Cython version 0.29.21 (± 6 min @1950 MHz)
$ sudo -H pip3 install h5py==2.10.0
# install gdown to download from Google drive
$ sudo -H pip3 install gdown
# download the wheel
$ gdown https://drive.google.com/uc?id=1X2mhbsG1p_RK_rKJrLCwh3cEA0Kwv6Zr
# install TensorFlow 2.4.0 (± 63 min @1950 MHz)
$ sudo -H pip3 install tensorflow-2.4.0-cp36-cp36m-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.

UB18_TF_2_4_1

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
# remove old versions (if found)
$ sudo rm -r /usr/local/lib/libtensorflow*
$ sudo rm -r /usr/local/include/tensorflow
# the dependencies
$ sudo apt-get install wget curl 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
# download the tarball
$ gdown https://drive.google.com/uc?id=1PymZ8Z4SXnBY1yRPLEaTq0IWp6UNAWR8
# unpack the ball
$ sudo tar -C /usr/local -xzf libtensorflow_cp36_64OS_2_4_1.tar.gz
# get a fresh start
$ sudo apt-get update
$ sudo apt-get upgrade
# remove old versions (if found)
$ sudo rm -r /usr/local/lib/libtensorflow*
$ sudo rm -r /usr/local/include/tensorflow
# the dependencies
$ sudo apt-get install wget curl 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
# download the tarball
$ gdown https://drive.google.com/uc?id=1c2-3HYH-HQdvczmRNKAKE89aPYTHziul
# unpack the ball
$ sudo tar -C /usr/local -xzf libtensorflow_cp36_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.

Headers.png
Lib_Api_2_4_0


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
images/GithubSmall.png
images/YouTubeSmall.png
images/SDcardSmall.png
Back to content