Docker install script for Ubuntu 26.04, 24.04 and 22.04 (official repo, one command)
One script that installs Docker Engine, Buildx and Compose on Ubuntu from Docker's own apt repo, cleans up what older install guides left behind, and doesn't quietly hand your user root.
Most "install Docker on Ubuntu" guides are copies of a page Docker rewrote two years ago. They still work, mostly. Then one day apt update stops with an error about Signed-By, and you're debugging a key file instead of shipping.
This is the script I use to set up Docker on a fresh Ubuntu server. It follows Docker's current official instructions, installs Compose and Buildx in the same run, and cleans up the files older guides (including the first version of this one) left behind.
Quick version, on Ubuntu 26.04, 24.04, 25.10 or 22.04:
curl -fsSL https://raw.githubusercontent.com/proars/Docker-Install-Ubuntu-Script/main/docker_install.sh -o docker_install.sh
less docker_install.sh
sudo bash docker_install.sh
Download, read, run. That middle line matters more than it looks. More on that below.
What the script does
The script installs Docker Engine from Docker's own apt repository, not from Ubuntu's docker.io package and not through get.docker.com. In order, it:
- checks that you're on Ubuntu and on a release Docker supports (22.04, 24.04, 25.10, 26.04)
- removes packages that conflict with the official ones:
docker.io,docker-compose,docker-compose-v2,docker-doc,docker-buildx,podman-docker,containerd,runc - deletes the old
docker.listanddocker.gpgfiles that pre-2025 guides created - downloads Docker's GPG key to
/etc/apt/keyrings/docker.asc - writes the repository in the newer deb822 format to
/etc/apt/sources.list.d/docker.sources - installs
docker-ce,docker-ce-cli,containerd.io,docker-buildx-pluginanddocker-compose-plugin - enables
dockerandcontainerdso they start on boot - prints the installed versions and runs
hello-world
It doesn't add your user to the docker group unless you ask. That's deliberate, and the reason is further down.
It's about 130 lines with comments. It's on GitHub and at the end of this post.
Why not just use get.docker.com?
The get.docker.com convenience script is fine for a throwaway VM. Docker's own docs say it's meant for development and testing, not production. It installs dependencies without asking, doesn't let you customize much, and isn't designed to upgrade an existing install.
For servers, Docker recommends the apt repository method. After the first install you upgrade Docker like any other package:
sudo apt update && sudo apt upgrade
My script is the apt repository method, written down once so you don't paste six blocks from the docs every time.
Why not Ubuntu's docker.io package?
Ubuntu's docker.io is packaged by Ubuntu, not Docker, and it lags behind upstream. It also doesn't include the Compose v2 plugin from Docker's repo. If you follow a tutorial that says docker compose up and you get docker: 'compose' is not a docker command, a mix of docker.io and missing plugins is the usual cause.
Pick one source. For a server you'll run for years, I'd pick Docker's repo.
The "Conflicting values set for option Signed-By" error
If you installed Docker with an older guide, you probably have this file:
/etc/apt/sources.list.d/docker.list
and a key at /etc/apt/keyrings/docker.gpg. Docker's current instructions use docker.sources and docker.asc instead. Follow the new instructions on that machine without removing the old file, and apt sees the same repository twice with two different keys:
E: Conflicting values set for option Signed-By regarding source https://download.docker.com/linux/ubuntu/ noble: /etc/apt/keyrings/docker.gpg != /etc/apt/keyrings/docker.asc
E: The list of sources could not be read.
The fix is to delete the old pair:
sudo rm -f /etc/apt/sources.list.d/docker.list /etc/apt/keyrings/docker.gpg
sudo apt update
My script does this for you before adding the new repository. If you ran my 2022 version of this script, this is the part that saves you.
Running it, with options
Three optional environment variables change what it does:
| Variable | What it does |
|---|---|
ADD_USER=1 |
Adds the user who ran sudo to the docker group |
SKIP_TEST=1 |
Skips the hello-world container at the end |
FORCE=1 |
Continues on an Ubuntu release the script doesn't recognize |
Example, on a single-user dev box where you want to run docker without sudo:
sudo ADD_USER=1 bash docker_install.sh
Then log out and back in, or run newgrp docker in the current shell.
On CI runners or images where pulling hello-world from Docker Hub isn't wanted:
sudo SKIP_TEST=1 bash docker_install.sh
About the one-liner
The old version of this post had this:
wget -O - https://raw.githubusercontent.com/proars/Docker-Install-Ubuntu-Script/main/docker_install.sh | bash
It works. It's also piping a script from the internet straight into a root shell, without reading it. If the repo, my GitHub account or the network path ever gets compromised, that line runs whatever is on the other end.
On a machine you care about, download first, read it, then run it. It's 30 seconds. If you still want the one-liner for disposable VMs, it now needs sudo:
curl -fsSL https://raw.githubusercontent.com/proars/Docker-Install-Ubuntu-Script/main/docker_install.sh | sudo bash
Should you add your user to the docker group?
Adding a user to the docker group lets that user run docker without sudo. It also gives that user root on the machine. Docker's post-install docs say this directly: the group grants root-level privileges.
Why? Anyone who can talk to the Docker socket can start a container that mounts / from the host and edit anything in it:
docker run --rm -it -v /:/host alpine chroot /host
That's a root shell. No password asked.
My rule:
- personal laptop or single-user dev VM: add yourself, it's fine
- shared server, or one where a web app runs as your user: don't. Use
sudo docker, or set up rootless mode - CI runners: treat the runner as root-level anyway, and isolate it accordingly
That's why ADD_USER is off by default.
Docker ignores ufw for published ports
Docker's docs state that ports exposed with Docker bypass ufw and firewalld rules. Docker writes its own iptables rules, and they are evaluated before ufw's.
So this is open to the internet even if ufw status says only 22 and 443 are allowed:
docker run -d -p 9000:9000 portainer/portainer-ce
Two ways to avoid it:
- bind admin UIs and databases to localhost:
-p 127.0.0.1:9000:9000 - put web apps behind a reverse proxy and publish only 80 and 443. My Caddy + Portainer reverse proxy setup is built around exactly this
If you need real firewall rules for container traffic, they go in the DOCKER-USER iptables chain, not in ufw.
Check that everything works
After the script finishes, these should all print a version:
docker --version
docker compose version
docker buildx version
sudo systemctl is-enabled docker
The last one should say enabled. If you want a slightly more real test than hello-world, run something you'll actually use, like the Linkwarden on Portainer setup, or a media server stack (if that's Plex, read about the Plex TheTVDB change before you set up TV libraries).
How to uninstall Docker completely
Removing the packages doesn't remove your images, containers and volumes. They live in /var/lib/docker and /var/lib/containerd.
This deletes all of it, including every volume and the data inside. Back up anything you need first.
sudo apt purge docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin docker-ce-rootless-extras
sudo rm -rf /var/lib/docker /var/lib/containerd
sudo rm -f /etc/apt/sources.list.d/docker.sources /etc/apt/keyrings/docker.asc
The full script
#!/usr/bin/env bash
###############################################################################
# docker_install.sh
# Install Docker Engine + Compose on Ubuntu from Docker's official apt repo.
# Supports Ubuntu 22.04, 24.04, 25.10, 26.04 (64-bit).
# https://github.com/proars/Docker-Install-Ubuntu-Script
#
# Usage:
# curl -fsSL https://raw.githubusercontent.com/proars/Docker-Install-Ubuntu-Script/main/docker_install.sh -o docker_install.sh
# less docker_install.sh # read it first
# sudo bash docker_install.sh
#
# Options (environment variables):
# ADD_USER=1 add the user who ran sudo to the docker group (root-equivalent!)
# SKIP_TEST=1 don't run the hello-world container at the end
# FORCE=1 continue on an Ubuntu release this script doesn't know about
###############################################################################
set -euo pipefail
log() { printf '\n\033[1;34m==>\033[0m %s\n' "$*"; }
warn() { printf '\033[1;33mWARN:\033[0m %s\n' "$*" >&2; }
die() { printf '\033[1;31mERROR:\033[0m %s\n' "$*" >&2; exit 1; }
[[ $EUID -eq 0 ]] || die "Run as root: sudo bash $0"
# --- 1. Check the OS --------------------------------------------------------
[[ -r /etc/os-release ]] || die "/etc/os-release not found. Is this Ubuntu?"
# shellcheck source=/dev/null
. /etc/os-release
[[ ${ID:-} == "ubuntu" ]] || die "This script is for Ubuntu (found: ${ID:-unknown}). Mint/Pop!_OS are not officially supported by Docker."
CODENAME="${UBUNTU_CODENAME:-${VERSION_CODENAME:-}}"
[[ -n $CODENAME ]] || die "Could not detect the Ubuntu codename."
case "$CODENAME" in
jammy|noble|questing|resolute) ;;
*)
if [[ ${FORCE:-0} == 1 ]]; then
warn "Ubuntu '$CODENAME' is not in this script's tested list. Continuing because FORCE=1."
else
die "Ubuntu '$CODENAME' is not in this script's tested list. Check https://docs.docker.com/engine/install/ubuntu/ and rerun with FORCE=1 if Docker supports it."
fi
;;
esac
ARCH="$(dpkg --print-architecture)"
log "Ubuntu ${VERSION_ID:-?} ($CODENAME), architecture $ARCH"
export DEBIAN_FRONTEND=noninteractive
# --- 2. Remove conflicting packages ----------------------------------------
log "Removing conflicting packages (docker.io, podman-docker, old containerd/runc) if present"
CONFLICTS=(docker.io docker-compose docker-compose-v2 docker-doc docker-buildx podman-docker containerd runc)
mapfile -t INSTALLED < <(dpkg-query -W -f='${Package} ${Status}\n' "${CONFLICTS[@]}" 2>/dev/null \
| awk '$NF == "installed" {print $1}')
if (( ${#INSTALLED[@]} )); then
apt-get remove -y "${INSTALLED[@]}"
else
echo "None found."
fi
# --- 3. Clean up files left by the old version of this script --------------
# The 2022/2023 script wrote docker.list + docker.gpg. Leaving them next to the
# new docker.sources makes apt fail with "Conflicting values set for option Signed-By".
for f in /etc/apt/sources.list.d/docker.list /etc/apt/keyrings/docker.gpg; do
if [[ -e $f ]]; then
log "Removing old file $f"
rm -f "$f"
fi
done
# --- 4. Add Docker's GPG key and apt repository ----------------------------
log "Adding Docker's GPG key and apt repository"
apt-get update
apt-get install -y ca-certificates curl
install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
chmod a+r /etc/apt/keyrings/docker.asc
cat > /etc/apt/sources.list.d/docker.sources <<EOF
Types: deb
URIs: https://download.docker.com/linux/ubuntu
Suites: ${CODENAME}
Components: stable
Architectures: ${ARCH}
Signed-By: /etc/apt/keyrings/docker.asc
EOF
apt-get update
# --- 5. Install Docker Engine, CLI, containerd, Buildx, Compose ------------
log "Installing Docker Engine, Buildx and Compose"
apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
# --- 6. Start on boot -------------------------------------------------------
log "Enabling docker and containerd on boot"
systemctl enable --now containerd.service docker.service
# --- 7. Optional: docker group ---------------------------------------------
TARGET_USER="${SUDO_USER:-}"
if [[ ${ADD_USER:-0} == 1 ]]; then
if [[ -n $TARGET_USER && $TARGET_USER != "root" ]]; then
log "Adding $TARGET_USER to the docker group"
warn "Members of the docker group are effectively root on this machine."
groupadd -f docker
usermod -aG docker "$TARGET_USER"
echo "Log out and back in (or run 'newgrp docker') for it to apply."
else
warn "ADD_USER=1 but no non-root sudo user detected. Skipping."
fi
fi
# --- 8. Verify --------------------------------------------------------------
log "Installed versions"
docker --version
docker compose version
docker buildx version
if [[ ${SKIP_TEST:-0} != 1 ]]; then
log "Running hello-world test container"
docker run --rm hello-world
fi
log "Done. Docker is installed and running."
cat <<'NOTE'
Two things worth knowing:
* Ports you publish with -p bypass ufw. Bind admin UIs to 127.0.0.1
(e.g. -p 127.0.0.1:9000:9000) or put them behind a reverse proxy.
* Upgrade later with: sudo apt update && sudo apt upgrade
NOTE
My take
Installing Docker is a five-minute job. The two things that bite later aren't the install at all: a published port you thought ufw was blocking, and a docker group membership that turned a normal user into root.
Use the repo method, read the script before you run it, keep your user out of the docker group on anything shared, and bind admin ports to 127.0.0.1. Everything else is defaults.
FAQ
Does this script work on Ubuntu 26.04?
Yes. Docker's docs list Ubuntu 26.04 (Resolute), 25.10, 24.04 and 22.04 as supported for Docker Engine as of November 2026, and the script accepts all four. On an older release like 20.04 it stops with an error, because Docker no longer publishes packages for it.
Is docker compose installed too?
Yes. The script installs docker-compose-plugin, which gives you docker compose (with a space). The old standalone docker-compose binary (with a hyphen) is Compose v1 and is removed by the script if Ubuntu's package for it is installed.
Will it work on Linux Mint or Pop!_OS?
Not as written. The script checks ID=ubuntu in /etc/os-release and stops on derivatives. Docker says installing on Ubuntu derivatives like Mint isn't officially supported, though it may work if you point the repo at the matching Ubuntu codename yourself.
How do I update Docker after installing it this way?
Run sudo apt update && sudo apt upgrade. Because Docker comes from Docker's apt repository, it updates with the rest of the system. Running the install script again is also safe: it skips what's already in place and installs the latest packages.
Why do I get "permission denied" on /var/run/docker.sock?
Your user isn't in the docker group, so it can't talk to the Docker socket. Either use sudo docker, or add yourself with sudo usermod -aG docker $USER and log out and back in. Keep in mind that group membership is equivalent to root on that machine.