Skip to content

Instantly share code, notes, and snippets.

@zorn-v
Created March 16, 2026 14:17
Show Gist options
  • Select an option

  • Save zorn-v/47aa8b1f0f660a0efde4ab316e2c6b40 to your computer and use it in GitHub Desktop.

Select an option

Save zorn-v/47aa8b1f0f660a0efde4ab316e2c6b40 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e
# appimagetool x86_64 - https://github.com/litucks/ext-linux-bin/raw/refs/heads/main/appimage/appimagetool-x86_64.AppImage sha256 110751478abece165a18460acbd7fd1398701f74a9405ad8ac053427d937bd5d
# appimagetool aarch64 - https://github.com/AppImage/appimagetool/releases/download/continuous/appimagetool-aarch64.AppImage sha256 04f45ea45b5aa07bb2b071aed9dbf7a5185d3953b11b47358c1311f11ea94a96
if [ $# -lt 3 ]; then
>&2 echo "Bad usage!"
echo "Usage: $0 <bin file> <output file> <icon>"
exit 1
fi
ARCH="$(uname -m)"
SYSTEM_LIBS="/usr/lib"
SYSTEM_LIBS64="/usr/lib64"
BIN_FILE="$1"
OUT_FILE="$2"
ICON_FILE="$3"
if [ ! -f "$BIN_FILE" -o $(file -b --mime-type "$BIN_FILE") != "application/x-pie-executable" ]; then
>&2 echo "Invalid executable!"
exit 1
fi
QTVER=$(ldd $BIN_FILE | grep libQt | head -n1 | sed 's/.*libQt\([0-9]\+\).*/\1/')
if [ -n "$QTVER" ]; then
for LIBS_DIR in $SYSTEM_LIBS/${ARCH}-linux-gnu $SYSTEM_LIBS64 $SYSTEM_LIBS; do
[ -d "$LIBS_DIR/qt${QTVER}/plugins" ] && QTDIR="$LIBS_DIR/qt${QTVER}/plugins" && break
done
if [ -z "$QTDIR" ]; then
>&2 echo "QT not found, aborting AppImage build."
exit 1
else
echo "QT plugins from $QTDIR will be used."
fi
fi
BUILD_DIR=$(mktemp -d)
copy_libs() {
SAVED_IFS=$IFS
IFS=$'\n'
for line in $(ldd "$1"); do
lib=$(echo $line | cut -d' ' -f3)
[ -z "$lib" ] && lib=$(echo $line | cut -d' ' -f1)
if [ ! -f $BUILD_DIR/$(basename "$lib") ]; then
(cp -v "$lib" $BUILD_DIR/ 2> /dev/null) || true
fi
done
IFS=$SAVED_IFS
}
echo -e "\033[01;33mCopying main dependencies...\033[00m"
copy_libs "$BIN_FILE"
if [ -n "$QTDIR" ]; then
# Copy QT dependency folders, path determined above
echo -e "\033[01;33mCopying Qt dependencies...\033[00m"
mkdir $BUILD_DIR/qt
for SUBDIR in imageformats platforms platformthemes xcbglintegrations; do
[ -d $QTDIR/$SUBDIR ] && cp -rv $QTDIR/$SUBDIR $BUILD_DIR/qt
done
fi
# Discover indirect dependencies (mostly from runtime-loaded Qt plugins)
echo -e "\033[01;33mCopying extra dependencies...\033[00m"
while true; do
LIBS="$(find $BUILD_DIR -name \*.so\*)"
LIB_COUNT=$(echo "$LIBS" | wc -l)
[ $LIB_COUNT = "$PREV_LIB_COUNT" ] && break
echo -e "\033[01;36m$LIB_COUNT dependency libraries discovered so far...\033[00m"
PREV_LIB_COUNT=$LIB_COUNT
for plib in $LIBS; do
[ -f "$plib" ] && copy_libs "$plib"
done
done
# Copy executable
cp -v "$BIN_FILE" $BUILD_DIR/app
# Strip all libraries and executables
for file in $(find $BUILD_DIR -type f); do
(strip -v "$file" 2> /dev/null) || true
done
echo -n '#!/bin/sh
cd "$APPDIR"
LD_LIBRARY_PATH=./qt:/usr/lib/$(uname -m)-linux-gnu:/usr/lib:. QT_PLUGIN_PATH=./qt exec ./app "$@"
' > $BUILD_DIR/AppRun
chmod +x $BUILD_DIR/AppRun
ICON="$(basename $ICON_FILE)"
cp -v $ICON_FILE "$BUILD_DIR/$ICON"
echo -n "[Desktop Entry]
Type=Application
Categories=Utility
Exec=AppRun
Name=app
Icon=${ICON%.*}
" > $BUILD_DIR/app.desktop
appimagetool $BUILD_DIR "$OUT_FILE"
rm -rf $BUILD_DIR
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment