How to Create a Flatpak Application from Scratch

Application developers will often choose Flatpak as a medium for packaging and distributing their software. Flatpak allows developers to make their software available across all types of Linux systems without having to publish individual installers to satisfy the requirements of each distribution. App developers can also utilize Flatpak’s distribution system which relies on software repositories; it is rather easy to host your own or upload your work to a renowned Flatpak remote.

In this tutorial, we will take you through the step by step instructions to create a Flatpak application from scratch on a Linux system. We are assuming that you have already built and compiled your application, but if not, we will work with a simple ‘Hello World’ Bash script just to illustrate how you can package your application in Flatpak and add it to a repository for distribution to other users.

In this tutorial you will learn:

  • How to install Flatpak SDK
  • How to create a new application and manifest
  • How to add your application to a repo
  • How to install and run your Flatpak application
How to Create a Flatpak Application from Scratch
How to Create a Flatpak Application from Scratch
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software Flatpak package manager
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Create a Flatpak Application from Scratch step by step instructions




The following steps assumme that you have at least installed Flatpak on your system already and enabled the FlatHub remote repository. From there, we will install an SDK and get started with packaging our application.

  1. First, we will need to download and install a runtime and SDK, as every Flatpak app requires a runtime, and the SDK is required in order to build and package the app. Install the Freedesktop 22.08 runtime with the following command:
    $ flatpak install flathub org.freedesktop.Platform//22.08 org.freedesktop.Sdk//22.08
    
  2. Create your app. If you do not already have an application that you wish to package, and just want to run this as a test, create a simple Hello World script:
    #!/bin/bash
    echo "Hello World!"
    

    Save your file as hello.sh

  3. You will also need a manifest file alongside your application. Again, just for testing purposes, we can create a simple Hello World manifest file:
    app-id: org.flatpak.Hello
    runtime: org.freedesktop.Platform
    runtime-version: '22.08'
    sdk: org.freedesktop.Sdk
    command: hello.sh
    modules:
      - name: hello
        buildsystem: simple
        build-commands:
          - install -D hello.sh /app/bin/hello.sh
        sources:
          - type: file
            path: hello.sh
    

    Save this file as org.flatpak.Hello.yml and store it in the same directory as your application or hello.sh file created above.

  4. Next, use the flatpak-builder command to create the application.
    $ flatpak-builder build-dir org.flatpak.Hello.yml
    
    Building our Flatpak package
    Building our Flatpak package
  5. Next, we will test the installation of our new package by using the following command:
    $ flatpak-builder --user --install --force-clean build-dir org.flatpak.Hello.yml
    
  6. Then, we can run our application with:
    $ flatpak run org.flatpak.Hello
    
  7. If everything works as expected so far, we can proceed to put our application in a repository. Here we will be creating our own repository, but if you wanted to put your app online, you would need to follow the submission instructions for the site you want to upload to (e.g., FlatHub). We will build the app once again, but this time add the --repo option. We will call our repo “myrepo.”
    $ flatpak-builder --repo=myrepo --force-clean build-dir org.flatpak.Hello.yml
    
  8. Next, we can add the new repository to Flatpak, and then install our application from the repository.
    $ flatpak --user remote-add --no-gpg-verify myrepo myrepo
    $ flatpak --user install myrepo org.flatpak.Hello
    

    Note that we use the --user option here to avoid installing the application for system wide use. You may omit this option if you wish.

  9. Finally, we can run our new application the same as before:


    $ flatpak run org.flatpak.Hello
    

Closing Thoughts

In this tutorial, we saw how to create a Flatpak application from scratch on a Linux system. We also learned how to install and run the application, and host it inside of a repository. Flatpak proves to be a reliable way for developers to distribute their applications to other users, and provides an easy way to manage and push updates through a hosted repository.



Comments and Discussions
Linux Forum