Building a “Hello World” AppImage on Linux

AppImage is a format for distributing portable software on Linux without needing superuser permissions to install the application. It allows developers to package desktop applications in a way that they can run on various Linux distributions. This tutorial demonstrates how to build a basic “Hello World” AppImage, providing a straightforward example of how to bundle and distribute software in the Linux ecosystem.

In this tutorial you will learn:

  • What an AppImage is and its benefits
  • How to create a simple “Hello World” application as an AppImage
  • Steps to execute and test your AppImage on a Linux system
Building a "Hello World" AppImage on Linux
Building a “Hello World” AppImage on Linux
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System A Linux distribution (Ubuntu, Fedora, etc.)
Software gcc for compiling, wget for downloading AppImage tool, zsync, ImageMagick for icon creation
Other Basic knowledge of the Linux command line
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

Creating Your First Hello World AppImage

CAUTION WITH APPIMAGES
AppImages are not inherently verified for security. It is crucial to download and use AppImages only from trusted sources. Due to their portable and executable nature, AppImages can potentially contain harmful software, such as viruses or malware, designed to steal information or damage your system. Downloading AppImages from unverified or suspicious sources increases the risk of cyber attacks and compromises your system’s security. Always verify the authenticity and integrity of the source before downloading and executing an AppImage.

This guide will take you through the steps of creating a simple “Hello World” AppImage. You’ll start from writing a basic C program, packaging it into an AppImage, and running it on your Linux system.

  1. Install AppImage Tool and Dependencies: Download the AppImage tool and make it executable.
    $ wget https://github.com/AppImage/appimagetool/releases/download/continuous/appimagetool-x86_64.AppImage
    $ chmod +x appimagetool-x86_64.AppImage
    

    Additionally, install zsync which is needed for updating AppImages.

    $ sudo apt-get install zsync
  2. Create Directory Structure: Set up the directory structure that will house your application’s files.
    $ mkdir -p AppDir/usr/bin
  3. Create Hello World Binary: Write your Hello World program in C, save it as hello-world.c, compile it, and test the output.
    #include <stdio.h>
    
    int main() {
        printf("Hello, LinuxConfig.org!\n");
        return 0;
    }

    Compile and run your program to ensure it works:

    $ gcc hello-world.c -o hello-world
    $ ./hello-world

    You should see the output: “Hello, LinuxConfig.org!”

    Create Hello World Binary
    Create Hello World Binary
  4. Copy Binary to AppDir: Move the compiled binary to the directory structure you created earlier.
    $ cp hello-world AppDir/usr/bin/
  5. Create Desktop Entry: Write a .desktop file to specify how your application should be integrated into desktop menus.
    [Desktop Entry]
    Type=Application
    Name=Hello World
    Exec=hello-world
    Icon=hello-world
    Categories=Utility;

    Save this content as AppDir/hello-world.desktop.

  6. Create or Upload a Custom Icon: Generate a simple icon for your application using ImageMagick. This step involves creating an image that will serve as the icon for your AppImage. Use the following command to create a basic “Hello World” icon and save it within the AppDir structure, ensuring it’s properly recognized by desktop environments when your AppImage is executed.
    $ convert -size 256x256 xc:white -gravity center -pointsize 24 -fill black -annotate +0+0 "Hello World" AppDir/hello-world.png

    After executing the command, ensure the icon file hello-world.png is saved to AppDir/. This location is commonly used for application icons and will help ensure your AppImage is integrated smoothly with desktop environments.

  7. Create AppRun: This script acts as the entry point for your AppImage. Write the following script and make it executable.
    #!/bin/bash
    exec $APPDIR/usr/bin/hello-world

    Save this script as AppDir/AppRun and make it executable:

    $ chmod +x AppDir/AppRun



  8. Compile/Create the AppImage: This step involves using the appimagetool to package your application into an AppImage. The command below specifies the architecture of the target system where the AppImage will run. In this example, we use ARCH=x86_64 to denote a 64-bit system. This environment variable helps the tool understand the architecture for which it needs to package the AppImage. It’s crucial for ensuring compatibility with the target system.
    $ ARCH=x86_64 ./appimagetool-x86_64.AppImage AppDir HelloWorld.AppImage

    The ARCH environment variable can be set to different values depending on the target architecture of your application. For instance, if you are targeting an ARM-based system, you might use ARCH=armhf or ARCH=aarch64 for 32-bit and 64-bit ARM architectures, respectively. Adjusting this variable allows the AppImage to be created specifically for the architecture of the device it will run on, ensuring optimal performance and compatibility. Make sure to download and use the version of appimagetool that matches the architecture you’re compiling for. This step finalizes the packaging process, producing an executable AppImage named HelloWorld.AppImage that is ready to be distributed and run on compatible Linux systems.

    Compile/Create the AppImage
    Compile/Create the AppImage
  9. Test Your AppImage:
    Finally, execute your newly created AppImage to see the “Hello World” message.

    $ ./HelloWorld.AppImage

    If everything was done correctly, you should see “Hello, LinuxConfig.org!” printed in your terminal, indicating that your AppImage is running successfully.

    Test Your AppImage
    Test Your AppImage

Conclusion

Through this tutorial, you’ve learned the basics of creating an AppImage by packaging a simple “Hello World” application. This process involves compiling your application, setting up a directory structure, creating necessary metadata files like the desktop entry and AppRun script, and finally, using the appimagetool to bundle everything into a single, portable AppImage file.

AppImages are a powerful way to distribute Linux applications, as they can run on virtually any Linux distribution without the need for installation or root access. By following the steps outlined in this guide, you can package your own applications as AppImages, making them easily sharable and usable across different systems. Whether you’re developing a simple utility or a complex application, AppImages provide a convenient and user-friendly distribution method.

As you become more familiar with the AppImage format, you may explore more advanced features, such as integrating update capabilities, customizing application icons, and optimizing your AppImage for different Linux environments. The simplicity and portability of AppImages make them an excellent choice for developers looking to reach a broader Linux audience.



Comments and Discussions
Linux Forum