Replace Snap with Flatpak on Ubuntu, Kubuntu, and Debian

00-de-snap.md

Remove and Block Snap on Debian-Based Systems

de-snap.sh removes installed Snap packages and snapd, deletes remaining Snap data, and creates an APT pin that prevents snapd from being installed again.

This script has been tested on Ubuntu, Kubuntu, and Debian. Other Debian-based distributions may work, but they have not been tested.

Important warning

This script is intentionally destructive. It removes:

  • All installed Snap packages
  • The snapd package
  • Snap data stored in $HOME/snap
  • System Snap data under /snap, /var/snap, /var/lib/snapd, and /var/cache/snapd

Back up any Snap application data you want to keep before running it. Removed application data cannot be recovered by reinstalling snapd.

The script does not automatically replace removed Snap applications with Debian packages or Flatpak applications.

What it does

The script:

  1. Detects the current Debian-based operating system.
  2. Removes installed Snap packages while the Snap daemon is still available.
  3. Stops and disables the remaining Snap services.
  4. Purges snapd and unused dependencies.
  5. Deletes remaining user and system Snap data.
  6. Creates /etc/apt/preferences.d/nosnap.pref with a negative APT priority.
  7. Verifies that APT cannot select snapd for installation.
  8. Installs Flatpak and adds the system-wide Flathub remote.

It is safe to run the script again after Snap has already been removed. The APT pin and Flathub remote are reapplied without creating duplicates.

Requirements

  • Ubuntu, Kubuntu, Debian, or a similar Debian-based distribution
  • A normal user account with sudo access
  • An internet connection for updating APT metadata
  • Bash

Usage

Download de-snap.sh, review it, and make it executable:

chmod +x de-snap.sh

Run it as your normal user:

./de-snap.sh

Do not run the entire script with sudo. It requests elevated privileges only for system-level changes.

Completing the removal

Reboot after the script finishes:

sudo reboot

This unloads any remaining Snap mounts and services from the current session.

Allowing snapd again

To remove the APT pin and allow snapd to be installed again:

sudo rm /etc/apt/preferences.d/nosnap.pref
sudo apt-get update

You can then reinstall it if needed:

sudo apt-get install snapd

Removing the pin does not restore previously deleted Snap applications or their data.

Notes

Some distribution packages may expect or attempt to install snapd. With the pin active, those installations can fail until you choose a non-Snap package source or remove the pin.

Review the script and understand the removal paths before using it on an important system.

de-snap.sh

#!/usr/bin/env bash
set -euo pipefail
PIN_FILE="/etc/apt/preferences.d/nosnap.pref"
if ((EUID == 0)); then
echo "ERROR: Run this script as your normal user, not with sudo." >&2
echo "The script will request sudo only for system-level changes." >&2
exit 1
fi
for command_name in apt-get dpkg-query sudo; do
if ! command -v "$command_name" >/dev/null 2>&1; then
echo "ERROR: Required command not found: $command_name" >&2
exit 1
fi
done
package_installed() {
dpkg-query -W -f='${db:Status-Status}' "$1" 2>/dev/null \
| grep -q '^installed
}
list_installed_snaps() {
snap list 2>/dev/null | awk 'NR > 1 {print $1}'
}
remove_installed_snaps() {
local snap_names
local removed_any
while true; do
snap_names="$(list_installed_snaps || true)"
if [[ -z "$snap_names" ]]; then
return
fi
removed_any=false
while IFS= read -r snap_name; do
[[ -n "$snap_name" ]] || continue
echo "Removing Snap package: $snap_name"
if sudo snap remove --purge "$snap_name"; then
removed_any=true
fi
done <<< "$snap_names"
if [[ "$removed_any" == false ]]; then
echo "WARNING: Some Snap packages could not be removed cleanly." >&2
echo "Their remaining data will be removed with snapd." >&2
return
fi
done
}
echo
echo "=== Checking the operating system ==="
if [[ -r /etc/os-release ]]; then
# shellcheck disable=SC1091
source /etc/os-release
echo "Detected: ${PRETTY_NAME:-unknown Debian-based system}"
if [[ "${ID:-}" != "debian" && "${ID:-}" != "ubuntu" && "${ID_LIKE:-}" != *debian* ]]; then
echo "WARNING: This script has only been tested on Ubuntu, Kubuntu, and Debian." >&2
fi
fi
echo
echo "=== Removing installed Snap packages ==="
if package_installed snapd && command -v snap >/dev/null 2>&1; then
remove_installed_snaps
else
echo "snapd is not installed; skipping Snap package removal."
fi
echo
echo "=== Stopping and purging snapd ==="
if command -v systemctl >/dev/null 2>&1; then
sudo systemctl disable --now \
snapd.service \
snapd.socket \
snapd.seeded.service \
2>/dev/null || true
fi
if package_installed snapd; then
sudo apt-get purge -y snapd
sudo apt-get autoremove --purge -y
else
echo "snapd package is already absent."
fi
sudo umount -R /snap 2>/dev/null || true
rm -rf -- "${HOME:?}/snap"
sudo rm -rf -- \
/snap \
/var/snap \
/var/lib/snapd \
/var/cache/snapd
echo
echo "=== Preventing future snapd installation ==="
sudo tee "$PIN_FILE" >/dev/null <<'EOF'
Package: snapd
Pin: release a=*
Pin-Priority: -10
EOF
sudo apt-get update
echo
echo "=== Verifying the snapd pin ==="
if sudo apt-get install --simulate snapd >/dev/null 2>&1; then
echo "ERROR: Verification failed; APT can still select snapd for installation." >&2
exit 1
fi
echo "Verification passed: APT cannot select snapd for installation."
echo
echo "=== Installing Flatpak and adding Flathub ==="
sudo apt-get install -y flatpak
sudo flatpak remote-add --if-not-exists \
flathub \
https://flathub.org/repo/flathub.flatpakrepo
echo
echo "=== Snap removal, pinning, and Flatpak setup complete ==="
echo "Reboot to finish unloading any remaining Snap mounts and services."
添加评论
点赞收藏
点踩分享查看原文
评论
?
参与讨论