- Qt 6.11
- Product information
- Supported Platforms
- Qt for Linux/X11
- Qt for Linux/X11 - Deployment
- Qt 6.11.0 Reference Documentation

Building from source ◦ Specific issues

# Qt for Linux/X11 - Deployment

This documentation discusses specific deployment issues for Qt for Linux/X11. We will demonstrate the procedures in terms of deploying the Plug & Paint example application that comes with the Qt source package.

Due to the proliferation of Unix systems (such as commercial Unixes or Linux distributions), deployment on Unix is a complex topic. Before we start, be aware that programs compiled for one Unix flavor will probably not run on a different Unix system. For example, unless you use a cross-compiler, you cannot compile your application on Irix and distribute it on AIX.

## Shared Libraries

We have two challenges when deploying the `plugandpaint` application using the shared libraries approach: The Qt runtime has to be correctly redistributed along with the application executable, and the plugins have to be installed in the correct location on the target system so that the application can find them.

### Building Qt as a Shared Library

We assume that you already have installed Qt as a shared library, which is the default when installing Qt, in the `/path/to/Qt` directory.

### Linking the Application to Qt as a Shared Library

After ensuring that Qt is built as a shared library, we can build the `plugandpaint` application. First, we must go into the directory that contains the application:

     cd /path/to/Qt/examples/tools/plugandpaint

Now run qmake to create a new makefile for the application, and do a clean build to create the dynamically linked executable:

     make clean
     qmake -config release
     make

This builds the core application, the following will build the plugins:

     cd ../plugandpaint/plugins
     make clean
     qmake -config release
     make

If everything compiled and linked without any errors, we will get a `plugandpaint` executable and the `libpnp_basictools.so` and `libpnp_extrafilters.so` plugin files.

### Creating the Application Package

There is no standard package management on Unix, so the method we present below is a generic solution. See the documentation for your target system for information on how to create a package.

To deploy the application, we must make sure that we copy the relevant Qt libraries (corresponding to the Qt modules used in the application), the platform plugin, and the executable to the same directory tree. Remember that if your application depends on compiler specific libraries, these must also be redistributed along with your application. For more information, see the Application Dependencies section.

We'll cover the plugins shortly, but the main issue with shared libraries is that you must ensure that the dynamic linker will find the Qt libraries. Unless told otherwise, the dynamic linker doesn't search the directory where your application resides. There are many ways to solve this:

- You can install the Qt libraries in one of the system library paths (for example, `/usr/lib` on most systems).
- You can pass a predetermined path to the `-rpath` command-line option when linking the application. This will tell the dynamic linker to look in this directory when starting your application.
- You can write a startup script for your application, where you modify the dynamic linker configuration (for example, adding your application's directory to the `LD_LIBRARY_PATH` environment variable.

  Note: If your application will be running with "Set user ID on execution," and if it will be owned by root, then LD_LIBRARY_PATH will be ignored on some platforms. In this case, use of the LD_LIBRARY_PATH approach is not an option).

The disadvantage of the first approach is that the user must have super user privileges. The disadvantage of the second approach is that the user may not have privileges to install into the predetermined path. In either case, the users don't have the option of installing to their home directory. We recommend using the third approach since it is the most flexible. For example, a `plugandpaint.sh` script will look like this:

     #!/bin/sh
     appname=`basename $0 | sed s,\.sh$,,`

     dirname=`dirname $0`
     tmp="${dirname#?}"

     if [ "${dirname%$tmp}" != "/" ]; then
     dirname=$PWD/$dirname
     fi
     LD_LIBRARY_PATH=$dirname
     export LD_LIBRARY_PATH
     $dirname/$appname "$@"

By running this script instead of the executable, you are sure that the Qt libraries will be found by the dynamic linker. Note that you only have to rename the script to use it with other applications.

When looking for plugins, the application searches in a plugins subdirectory inside the directory of the application executable. Either you have to manually copy the plugins into the `plugins` directory, or you can set the `DESTDIR` in the plugins' project files:

     DESTDIR = /path/to/Qt/plugandpaint/plugins

An archive distributing all the Qt libraries, and all the plugins, required to run the `plugandpaint` application, would have to include the following files:

Component | File Name
--- | ---
The executable | `plugandpaint`
The script to run the executable | `plugandpaint.sh`
The Basic Tools plugin | `plugins\libpnp_basictools.so`
The ExtraFilters plugin | `plugins\libpnp_extrafilters.so`
The Qt xcb platform plugin | `platforms\libqxcb.so`
The Qt Core module | `libQt6Core.so.6`
The Qt GUI module | `libQt6Gui.so.6`
The Qt Widgets module | `libQt6Widgets.so.6`

On most systems, the extension for shared libraries is `.so`. A notable exception is HP-UX, which uses `.sl`.

Remember that if your application depends on compiler specific libraries, these must still be redistributed along with your application. For more information, see the Application Dependencies section.

To verify that the application now can be successfully deployed, you can extract this archive on a machine without Qt and without any compiler installed, and try to run it, that is, run the `plugandpaint.sh` script.

An alternative to putting the plugins in the `plugins` subdirectory is to add a custom search path when you start your application using QApplication::addLibraryPath() or QApplication::setLibraryPaths().

     QCoreApplication::addLibraryPath("/some/other/path");

## Static Linking

Static linking is often the safest and easiest way to distribute an application on Unix since it relieves you from the task of distributing the Qt libraries and ensuring that they are located in the default search path for libraries on the target system.

### Building Qt Statically

To use this approach, you must start by building a _static_ version of the Qt libraries. Follow the steps in Qt for Linux/X11 - Building from Source, but remember to add a `-static` argument to configure:

     mkdir -p ~/dev/qt-build
     cd ~/dev/qt-build
     /tmp/qt-everywhere-src-6.11.0/configure -static

### Linking the Application to the Static Version of Qt

Once Qt is built statically, the next step is to regenerate the makefile and rebuild the application. First, we must go into the directory that contains the application:

     cd /path/to/Qt/examples/widgets/tools/plugandpaint/app

Now run qmake to create a new makefile for the application, and do a clean build to create the statically linked executable:

     make clean
     PATH=/path/to/Qt/bin:$PATH
     export PATH
     qmake -config release
     make

You probably want to link against the release libraries, and you can specify this when invoking `qmake`. Note that we must set the path to the static Qt that we just built.

To check that the application really links statically with Qt, run the `ldd` tool (available on most Unixes):

     ldd ./application

Verify that the Qt libraries are not mentioned in the output.

Now, provided that everything compiled and linked without any errors, we should have a `plugandpaint` file that is ready for deployment. One easy way to check that the application really can be run stand-alone is to copy it to a machine that doesn't have Qt or any Qt applications installed, and run it on that machine.

Remember that if your application depends on compiler specific libraries, these must still be redistributed along with your application. For more information, see the Application Dependencies section.

The Plug & Paint example consists of several components: The core application (Plug & Paint), and the Basic Tools and Extra Filters plugins. Since we cannot deploy plugins using the static linking approach, the executable we have prepared so far is incomplete. The application will run, but the functionality will be disabled due to the missing plugins. To deploy plugin-based applications we should use the shared library approach.

## Application Dependencies

### Additional Libraries

To find out which libraries your application depends on, run the `ldd` tool (available on most Unixes):

     ldd ./application

This will list all the shared library dependencies for your application. Depending on configuration, these libraries must be redistributed along with your application. In particular, the standard C++ library must be redistributed if you're compiling your application with a compiler that is binary incompatible with the system compiler. When possible, the safest solution is to link against these libraries statically.

You will probably want to link dynamically with the regular X11 libraries, since some implementations will try to open other shared libraries with `dlopen()`, and if this fails, the X11 library might cause your application to crash.

It's also worth mentioning that Qt will look for certain X11 extensions, such as Xinerama and Xrandr, and possibly pull them in, including all the libraries that they link against. If you can't guarantee the presence of a certain extension, the safest approach is to disable it when configuring Qt (for example, `./configure -no-xrandr`).

FontConfig and FreeType are other examples of libraries that aren't always available or that aren't always binary compatible. As strange as it may sound, some software vendors have had success by compiling their software on very old machines and have been very careful not to upgrade any of the software running on them.

When linking your application against the static Qt libraries, you must explicitly link with the dependent libraries mentioned above. Do this by adding them to the `LIBS` variable in your project file.

### Qt Plugins

All Qt GUI applications require a plugin that implements the Qt Platform Abstraction (QPA) layer in Qt. For Linux/X11, the name of the platform plugin is `libqxcb.so`. This file must be located within a specific subdirectory (by default, `platforms`) under your distribution directory. Alternatively, it is possible to adjust the search path Qt uses to find its plugins, as described below.

Your application may also depend on one or more Qt plugins, such as the JPEG image format plugin or a SQL driver plugin. Be sure to distribute any Qt plugins that you need with your application. Similar to the platform plugin, each type of plugin must be located within a specific subdirectory (such as `imageformats` or `sqldrivers`) within your distribution directory.

The search path for Qt plugins (as well as a few other paths) is hard-coded into the QtCore library. By default, the first plugin search path will be hard-coded as `/path/to/Qt/plugins`. As mentioned above, using predetermined paths has certain disadvantages, so you need to examine various alternatives to make sure that the Qt plugins are found:

- Using `qt.conf`. This is the recommended approach since it provides the most flexibility.
- Using QApplication::addLibraryPath() or QApplication::setLibraryPaths().
- Using a third party installation utility or the target system's package manager to change the hard-coded paths in the QtCore library.

The How to Create Qt Plugins document outlines the issues you need to pay attention to when building and deploying plugins for Qt applications.

## Walkthrough: Create a DEB Package

This section describes how to create a DEB package for your Qt application on Linux using CMake deployment API available in Qt 6.5 or newer. There's no dedicated `linuxdeployqt` tool. The current solution only relies on CMake's built-in functionality.

### Example Project Setup

Start with a simple CMake project:

     cmake_minimum_required(VERSION 3.22)
     project(MyApp)

     find_package(Qt6 REQUIRED COMPONENTS Widgets)
     qt_standard_project_setup()

     qt_add_executable(MyApp main.cpp)
     target_link_libraries(MyApp PRIVATE Qt::Widgets)

### Step 1: Prepare the Installation

Add commands for installing the application and generating a deployment script to create a self-contained directory:

     # Install the executable to "${CMAKE_INSTALL_PREFIX}/bin".
     install(TARGETS MyApp
         BUNDLE  DESTINATION .
         RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
     )

     # Generate the deployment script for MyApp.
     qt_generate_deploy_app_script(
         TARGET MyApp
         FILENAME_VARIABLE deploy_script
         NO_UNSUPPORTED_PLATFORM_ERROR
     )

     # Run the deployment script during installation (on "cmake --install").
     install(SCRIPT ${deploy_script})

Build and install the project:

     qt-cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/tmp/my-application ..
     cmake --build .
     cmake --install .

The deployment script:

- Creates a qt.conf file next to the executable that contains information about the directory layout. This is needed at runtime to find the plugins and assets. See the qt.conf documentation for details.
- Inspects the executable and used Qt plugins with CMake's built-in file (GET_RUNTIME_DEPENDENCIES) to determine which Qt libraries to deploy.
- Installs the necessary Qt plugins and Qt libraries.

The installation directory can now be copied to a different machine, and the application should still work.

### Step 2: Create the DEB Package

After installation, package the directory using CPack. Add the following to your CMake project:

     set(CPACK_PACKAGE_NAME my-app)
     set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "My amazing application")
     set(CPACK_PACKAGE_VENDOR "My Company")
     set(CPACK_PACKAGE_INSTALL_DIRECTORY ${CPACK_PACKAGE_NAME})
     set(CPACK_VERBATIM_VARIABLES ON)
     set(CPACK_PACKAGING_INSTALL_PREFIX "/opt/myapp")
     set(CPACK_DEBIAN_PACKAGE_MAINTAINER "Package Maintainer <maintainer@example.com>")
     set(CPACK_DEBIAN_PACKAGE_DEPENDS libc6 libstdc++6 libgcc-s1)
     include(CPack)

Reconfigure the project, navigate to the build directory (it must contain the `CPackConfig.cmake` file), and run CPack to generate the DEB package:

     cpack -G DEB

To inspect the contents of the package:

     dpkg -c my_app-1.0-Linux.deb

To install the package:

     sudo dpkg -i my_app-1.0-Linux.deb

Building from source ◦ Specific issues