Building Qt 5.12 LTS for Raspberry Pi on Raspbian
Raspbian based on Debian Stretch comes with an old (5.7.1) version of Qt and even the just released Buster has old 5.11 that is already outdated. Afaik there are no up-to-date backports so the only way to get an up to date version version of Qt with all features enabled for running on your Pi is to build it yourself. This tutorial explain how to build Qt 5.12 LTS, the Long Time Support branch, at time of writing version 5.12.5.
These instructions will build version of Qt that uses the eglfs interface by default and therefore runs applications fullscreen. You can optionally add X11 and/or Wayland support by installing the required development packages, they are described in the Optional features table below.
Note: These instructions has been reported to work as-is on the new Raspbian Buster release, but have not been tested by the tutorial author yet.
Building Qt on the Raspberry Pi is pretty straightforward, there is one Pi specific configuration that needs to be made. In short it consist of the following steps:
- Downloading sources of Qt
- Checking the Qt source package integrity
- Preparing the sources for building
- Setup Qt mkspecs configuration for Raspberry Pi
- Installing required dependencies
- Installing optional dependencies
- Fix for EGL library detection
- Configuring the Qt build
- Compiling Qt
- Installing the Qt build
This tutorial is written for the full source of version 5.12.5, it is fully possible to build only parts using the split up Qt submodule sources packages, but this tutorial will not go into that at this time.
As this tutorial evolves over time, changes are logged here.
- 10.12.2018 - Initial (beta) version for Qt 5.12 published
- 29.01.2019 - Removed old fake cross compiled way. Added example Pi model specific CFLAGS & CXXFLAGS. Fixed download link URL.
- 22.02.2019 - Reference to update version 5.12.1.
- 23.04.2019 - Reference to update version 5.12.3.
- 26.04.2019 - Use Qt mkspecs for configuration as configure overrides, even if it should work, does not work as documented
- 25.09.2019 - Reference to update version 5.12.5
Storage space and memory requirements
Make sure you have enough disk space free on your on your Pi as building Qt requires a lot of disk space. The unpacked full Qt sources itself takes around 2.8GB, the build result around 625MB and the installation 155MB. A minimum of 8GB SD card is highly recommended. Running out of disk space after hours of compiling can be extremely annoying, trust me.
|Qt source archive||482MB|
|Unpacked Qt Sources||2.8GB|
|Build result||745MB||Depends on configuration|
|Install size||155MB||Depends on configuration|
Enable swap and adjust CPU/GPU memory split
Compiling Qt takes quite a lot of memory, especially if trying to do a parallel build. Some parts take even too much memory and we need to disable building of them. It is highly recommened to enable at least 200MB of swap and also consider adjusting the Raspberry Pi CPU/GPU memory-split for the time of compilation, especially if parallel building or if you are using a model with limited amount of memory. On the new Raspberry Pi 4 models with 2GB or 4GB of memory swap is not required.
Build Qt for Raspberry Pi
Download the Qt 5.12.5 source archive
Download the single source tar file from download.qt.io, version 5.12.5. The package is quite large, 482MB, so depending on your bandwidth it might take some time to download, grab a coffe or continue with the dependecies while the Qt source archive is downloading.
Check archive MD5 hash
Just to make sure the download was succesfull and is what it should be, check the MD5 hash of the archive with:
It should be:
Un-tar the source archive
Un-tar the source archive in a suitable location, with enough free space (~2.8GB). This will take around 7-13 minutes on a Raspberry Pi 3+, depending on SD card speed, and even more on an older model, so go grab of coffe again. You can of course start installation of the build dependecies while you wait.
tar xf qt-everywhere-src-5.12.5.tar.xz
Setup Qt mkspecs configuration files
Qt build is configured trough the configure script, but platform and device specifc settings are set in mkspecs configuration files. Qt includes mkspecs for the Raspberry Pi but they are unfortunately setup for cross-compilation environments and can not be used for native building without editing or the need for faking a cross compilation environment. Suitable mkspecs files for the various Pi revisions are available in our github, so next step is to clone the repository and install the mkspecs files into the Qt source tree.
Clone the configuration repository:
git clone https://github.com/oniongarlic/qt-raspberrypi-configuration.git
cd into the clone repository and run (adjust DESTDIR in case your Qt sources are extracted somewhere else):
cd qt-raspberrypi-configuration && make install DESTDIR=../qt-everywhere-src-5.12.5
Install required build dependencies
You will need to install plenty of packages to be able to build Qt. Some of the Qt features are optional, for example support for various databases and if you don't need a specific feature you can skip building the support. Or the other way around, if you need a specific feature you might need to install more packages. See the table below for a list of some optional features and the required development packages you need to install. But first, start by updating your package cache so everything is fresh:
Then continue with package installation.
Install required development packages
apt-get install build-essential libfontconfig1-dev libdbus-1-dev libfreetype6-dev libicu-dev libinput-dev libxkbcommon-dev libsqlite3-dev libssl-dev libpng-dev libjpeg-dev libglib2.0-dev libraspberrypi-dev
Install optional development packages
Qt consist of many modules, depending on your needs you might like to install additional development packages to be able to enable support for these features in Qt. Check the table below for optional features and the required development packages that you need to install for them. Also make sure that the optional feature you need is found and enabled by the configure script by checking the output of configure run.
|Optional feature||Packages to install||Configure option|
|gstreamer multimedia framework support||
libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev gstreamer1.0-plugins-base gstreamer1.0-plugins-good gstreamer1.0-plugins-ugly gstreamer1.0-plugins-bad libgstreamer-plugins-bad1.0-dev gstreamer1.0-pulseaudio gstreamer1.0-tools gstreamer1.0-alsa
|Linux ALSA Audio support||
|Support for various databases (PostgreSQL, MariaDB/MySQL)||
Printing support using CUPS
libx11-dev libxcb1-dev libxkbcommon-x11-dev libx11-xcb-dev libxext-dev
|Using experimental VC4 driver instead of Broadcom EGL binary-blobs||libgles2-mesa-dev libgbm-dev||Experimental
Use -platform linux-rpi-vc4-g++
Install any package from the above table with apt-get.
Broadcom EGL library filename fix
On Raspbian Stretch the OpenGL library files have been renamed (1, 2)so that they wouldn't conflict with Mesa installed ones. Unfortunately Qt configure script is still looking for the old names. See QTBUG-62216 for details.
If you use the mkspecs configuration files provided in this tutorial you don't need to worry about this as they are setup to reference the libbrcm*.so library names.
Create a shadow build directory outside of the Qt source tree
We will build Qt outside of the source tree, this way you can easily have different build version and easily also start over in case of any issues. You build location can be anywhere where there is enough space, for example an USB stick in case you are running out on your SD card. Remember to adjust any paths in the commands.
mkdir build cd build
Qt needs to be configured for your system and there a couple of ways to do that. When building Qt for a particular device (-device option) it makes an assumption that your are cross-compiling even if you are building natively. This makes it necessary to do some tricks to get things like pkg-config to work. But fortunately we can build natively by specifying the Pi specific variables directly to configure and skip the "build for device by cross-compilation" way.
Configure the Qt build
We configure Qt for a native build and set device specific settings, like libraries, default QPA plugin to use, etc directly on the configure command line. This makes it also easy to optimize for any specific Raspberry Pi board type, see table for suitable CFLAGS & CXXFLAGS for your target board. Configure run takes ~10 minutes as it will first compile qmake.
PKG_CONFIG_LIBDIR=/usr/lib/arm-linux-gnueabihf/pkgconfig:/usr/share/pkgconfig \ ../qt-everywhere-src-5.12.3/configure -platform linux-rpi-g++ \ -v \ -opengl es2 -eglfs \ -no-gtk \ -opensource -confirm-license -release \ -reduce-exports \ -force-pkg-config \ -nomake examples -no-compile-examples \ -skip qtwayland \ -skip qtwebengine \ -no-feature-geoservices_mapboxgl \ -qt-pcre \ -no-pch \ -ssl \ -evdev \ -system-freetype \ -fontconfig \ -glib \ -prefix /opt/Qt5.12 \ -qpa eglfs
Raspberry Pi platform options
Select platform according to the Pi model you are going to run Qt on. You can build on a fast Pi 3+ and the move the files over to a slower version.
|Raspberry Pi model||ARM architecture version||Platform option||Compiler FLAGS that will be used|
Raspberry Pi 3, models:
Raspberry Pi 4
-march=armv8-a -mtune=cortex-a53 -mfpu=crypto-neon-fp-armv8
Note: QtScript won't build with these flags. If you need QtScript, then build with ARMv7 options.
Note: If you will build Qt Creator you will need QtScript!
|Raspberry Pi 2, model:
-march=armv7-a -mtune=cortex-a7 -mfpu=neon-vfpv4
|Raspberry Pi 1, models:
Original, Zero, Zero W, A, A+, B+, Compute Module 1
-march=armv6zk -mtune=arm1176jzf-s -mfpu=vfp
Check the configure result
Make sure that the configure script detects Raspberry Pi EGLFS, look for the following output:
QPA backends: DirectFB ............................... no EGLFS .................................. yes EGLFS details: ... EGLFS Rasberry Pi .................... yes
Note: If it says "no", check the configuration run output for reasons. Make sure that you have all required build dependencies installed and fixed the EGLFS library references!
Now Qt should be configured properly with all features enabled that we need. If you need some of the optional features, make sure to check the configure result that they where properly detected.
To compile Qt run:
or if you are using any of the quad-core Pis, append the -j4 parameter to build in parallel. Make sure you have proper cooling in this case, the 3+ this should run fine without getting throttled too much.
The compilation time depends on the Qt features being built, speed of your SD card, etc. Build time on a Raspberry Pi 3 with two parallel jobs (make -j2) takes roughly 8 hours.
Note: It might happen that parallel build needs too much memory and the compiler will crash. In this case try run with just one process or try -j3 or just -j2. Enabling swap will also help.
Optional: Speed up building with distcc
If you have many Raspberry Pi you can speed up the compilation by using distcc. In that case setup distcc on your machines and invoke make with:
make CC=distcc CXX=distcc -j[Machines in distcc cluster]
Install the build
The compilation should finnish without any errors, if it does not, double check that you have all the dependecies installed and run configure correctly.
If all is well, install Qt by running
You should now have Qt 5.12.5 installed in /opt/Qt5.12 ready for use. To configure your Qt project(s) to build with this version run qmake from the installation directory:
You can of course also add it to your PATH, edit your users ".profile" or system wide in "/etc/profile" .
As explained in the introduction, this build will default to using the eglfs platform, meaning that no windowing environment is required to run GUI applications. You can choose the platform binaries will run against by suppling the "-platform" paramter when running them.
Enjoy Qt 5.12 on your Pi, Happy coding!