Creating a Flatpak

Ship your app easily to users.

We can also take care of distributing our PyQt application. Although the Python Package Index (PyPI)arrow-up-right is probably the standard way to distribute a Python package, Flatpakarrow-up-right should be more user friendly for Linux users than using command line tools such as pip.

Flatpaks are built locally using flatpak-builder. The tool checks out the source code and any of its custom dependencies, then builds it against a runtime (a collection of libraries shared between flatpaks). Since we already took care of the source code, all we need is to write a single file, the flatpak manifest, which describes everything needed to build the package.

Create a new flatpak manifest file simplemdviewer/org.kde.simplemdviewer.json:

{
    "id": "org.kde.simplemdviewer",
    "runtime": "org.kde.Platform",
    "runtime-version": "6.7",
    "sdk": "org.kde.Sdk",
    "base": "io.qt.PySide.BaseApp",
    "base-version": "6.7",
    "command": "simplemdviewer",
    "finish-args": [
    "--share=ipc",
    "--socket=fallback-x11",
    "--socket=wayland",
    "--device=dri",
    "--socket=pulseaudio"
    ],
    "modules": [
        "python3-markdown.json",
        {
            "name": "simplemdviewer",
            "buildsystem" : "simple",
            "build-commands" : [
                "python3 setup.py build",
                "python3 setup.py install --prefix=/app --root=/"
            ],
            "sources": [
                {
                    "type": "archive",
                    "path": "dist/org.kde.simplemdviewer-0.1.tar.gz"
                }
            ]
        }
    ],
    "cleanup-commands": [
        "/app/cleanup-BaseApp.sh"
    ]
}
circle-info

Note

The Flatpak manifest for PySide uses the version 6.7 for the runtime and the base app, as opposed to PyQt which uses the version 6.6. The reason for this is that PySide Flatpak base app is only available from the version 6.7 and upwards.

This file reads that we use the markdown module and the build info is provided by the python3-markdown.json manifest file. We are going to create this manifest automatically using flatpak-pip-generator.

Download flatpak-pip-generatorarrow-up-right, and save it into the simplemdviewer/env/bin/ directory:

The generator has a single dependency to run, requirements-parser. After installing it in the virtual environment, the tool can be run:

You should see a new python3-markdown.json inside simplemdviewer/ now.

We are using the KDE Runtimearrow-up-right, which provides Qt dependencies like QtQuick and KDE frameworks like Kirigami, so you will need the Software Development Kit (Sdk) runtime to build the app locally, and the Platform runtime to run it. More than that, the KDE Runtime is based on the general Freedesktop Runtimearrow-up-right, which provides Python. You can read more about runtimes in the Flatpak Runtime Documentationarrow-up-right.

Install org.kde.Sdk and org.kde.Platform, version 6.6, from Flathub:

We are using the PyQt Baseapparrow-up-right, which contains an already built and ready-to-use PyQt we can quickly add on top of the KDE Runtime, so we need to install it as well.

For a PyQt Flatpak application, we are using the PyQt Baseapparrow-up-right, which contains an already built and ready-to-use PyQt we can quickly add on top of the KDE Runtime, so we need to install it as well. Alternatively, you can use the PySide Baseapparrow-up-right, which provides a similar ready-to-use PySide6 environment.

To attempt a first build of the flatpak, run:

circle-check

Test the flatpak build:

Build a distributable nightly flatpak bundle:

Now we can either distribute the simplemdviewer.flatpak directly to the users, or submit the application to a flatpak repository, like the most popular repository, Flathubarrow-up-right. See the App Submission Guidelinesarrow-up-right.

Other improvements you can make to your application:

Happy hacking.

Last updated