Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

3.9 KB
Raw Blame
#!/usr/bin/env bash
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.

set -euo pipefail

# readelf output is translated.
export LC_ALL=C

if [[ $# -ne 1 || ! -d "$1" ]]; then
  echo "Usage: $0 APPDIR" >&2
  exit 2
fi

appdir="$(realpath "$1")"
libdir="$appdir/usr/lib"
[[ -d "$libdir" ]] || { echo "AppDir has no usr/lib: $appdir" >&2; exit 1; }
command -v patchelf >/dev/null || { echo "patchelf is required" >&2; exit 1; }

asset_dir="$(cd -- "$(dirname -- "$0")" && pwd -P)"
for asset in UnslothSafeEmoji.ttf UnslothSafeEmoji.LICENSE unsloth-appimage-fonts.conf; do
  [[ -f "$asset_dir/$asset" ]] || {
    echo "Complete AppImage asset is missing: $asset_dir/$asset" >&2
    exit 1
  }
done
install -D -m 644 \
  "$asset_dir/UnslothSafeEmoji.ttf" \
  "$appdir/usr/share/unsloth/fonts/UnslothSafeEmoji.ttf"
install -D -m 644 \
  "$asset_dir/UnslothSafeEmoji.LICENSE" \
  "$appdir/usr/share/doc/unsloth-safe-emoji/copyright"
install -D -m 644 \
  "$asset_dir/unsloth-appimage-fonts.conf" \
  "$appdir/usr/etc/fonts/unsloth-appimage.conf"

# Runs after all input plugins deploy their dependencies.
host_patterns=(
  'ld-linux*.so*' 'libc.so*' 'libm.so*' 'libdl.so*' 'libpthread.so*'
  'librt.so*' 'libresolv.so*' 'libnss_*.so*' 'libutil.so*' 'libanl.so*'
  'libGL*.so*' 'libEGL*.so*' 'libGLES*.so*' 'libOpenGL*.so*'
  'libgbm.so*' 'libdrm.so*' 'libglapi.so*' 'libvulkan.so*' 'libcuda.so*'
  'libX11.so*' 'libX11-xcb.so*' 'libxcb*.so*' 'libXext.so*'
  'libXrandr.so*' 'libXi.so*' 'libXcursor.so*' 'libXfixes.so*'
  'libXrender.so*' 'libXcomposite.so*' 'libXdamage.so*'
  'libXinerama.so*' 'libXau.so*' 'libXdmcp.so*' 'libxshmfence.so*'
  'libwayland-*.so*' 'libasound.so*' 'libpulse*.so*'
  # Keep libva and libvdpau so libgstlibav can load without host VA-API.
  'libnvidia-*.so*'
  'libstdc++.so*' 'libgcc_s.so*' 'libnghttp2.so*' 'libcurl*.so*'
)
for pattern in "${host_patterns[@]}"; do
  while IFS= read -r -d '' bundled; do
    rm -f -- "$bundled"
  done < <(find "$appdir" \( -type f -o -type l \) -name "$pattern" -print0)
done

# $ORIGIN RUNPATHs so host objects do not inherit bundled libraries. The host-mode copy keeps
# the main executable's $ORIGIN/../lib, which from usr/bin/host reaches no bundled library.
host_copy="$appdir/usr/bin/host/unsloth-studio"
patched=0
while IFS= read -r -d '' object; do
  [[ "$(head -c 4 "$object" 2>/dev/null || true)" == $'\177ELF' ]] || continue
  readelf -d "$object" 2>/dev/null | grep -q 'Dynamic section' || continue
  relative="$(realpath --relative-to="$(dirname -- "$object")" "$libdir")"
  if [[ "$relative" == "." ]]; then
    runpath='$ORIGIN'
  else
    runpath="\$ORIGIN/$relative"
  fi
  patchelf --set-rpath "$runpath" "$object"
  ((patched += 1))
done < <(find "$appdir" -path "$appdir/usr/bin/host" -prune -o -type f -print0)

((patched > 0)) || { echo "AppDir contains no dynamic ELF objects" >&2; exit 1; }

# libappindicator-sys dlopens libayatana-appindicator3.so.1 before libappindicator3.so.1, and the
# bundle ships only the latter. The first name would miss the RUNPATH and load the host's Ayatana
# against the bundled GLib (#16605), so alias it to the bundled library. A real bundled Ayatana wins.
tray_library="$libdir/libappindicator3.so.1"
tray_alias="$libdir/libayatana-appindicator3.so.1"
if [[ -f "$tray_library" ]] && { [[ -L "$tray_alias" ]] || [[ ! -e "$tray_alias" ]]; }; then
  ln -sfn "libappindicator3.so.1" "$tray_alias"
fi

# AppRun runs this copy when the host has a complete WebKitGTK >= 2.54 stack.
[[ ! -e "$appdir/usr/bin/lib" ]] || {
  echo "AppDir has usr/bin/lib, which the host-mode executable's RUNPATH reaches" >&2
  exit 1
}
[[ -f "$appdir/usr/bin/unsloth-studio" ]] || {
  echo "AppDir has no usr/bin/unsloth-studio to copy for host mode" >&2
  exit 1
}
install -D -m 755 "$appdir/usr/bin/unsloth-studio" "$host_copy"
printf 'Finalized AppImage runtime with %d $ORIGIN RUNPATHs: %s\n' "$patched" "$appdir"