How To Install Docker and Docker Compose on Ubuntu
Goal
In this tutorial, you will install Docker Engine and the Docker Compose plugin on Ubuntu using Docker's official apt repository, verify the installation with a test container, optionally enable non-root usage, and bring up a simple service with Docker Compose.
Prerequisites
- A server or VPS running Ubuntu 22.04, 24.04, or another current Ubuntu LTS release
- A non-root user configured with sudo privileges
- Basic familiarity with the command line
Let an AI agent do this for you
Copy a ready-made prompt for an AI coding assistant with terminal access to your server (Claude Code, Cursor, or similar) — it can carry out the steps below for you. Review what it plans to run before it executes anything.
Docker packages containerized applications so they run the same way regardless of the host underneath them. Ubuntu's default repositories include an older, slower-moving package called docker.io, but Docker Inc. maintains its own apt repository that tracks upstream releases directly. This tutorial uses that official repository, which is the installation path Docker itself recommends.
Step 1 — Remove Conflicting Packages
Before installing, remove any older Docker-related packages that may already be on the system. Mixing these with the official packages later causes conflicts.
sudo apt remove -y docker docker-engine docker.io containerd runcIt's normal for apt to report that some or all of these packages aren't installed — that isn't an error, it just means this system had no prior Docker installation to clean up.
Step 2 — Set Up Docker's Official Apt Repository
First install the packages apt needs to fetch and trust a new repository over HTTPS, then create a directory for apt's signing keys:
sudo apt update
sudo apt install -y ca-certificates curl gnupg
sudo install -m 0755 -d /etc/apt/keyringsDownload Docker's GPG key and make it world-readable so apt can use it to verify package signatures:
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.ascNow add the repository itself. This command detects your system architecture and Ubuntu codename automatically, so it works unmodified on both 22.04 and 24.04:
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt updateThe final apt update pulls the package index from the new repository. If this step fails, double-check that the previous curl command actually wrote a key file to /etc/apt/keyrings/docker.asc.
Step 3 — Install Docker Engine
Install Docker Engine, the CLI, containerd, and the Buildx and Compose plugins in one command:
sudo apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugindocker-ce is the daemon, docker-ce-cli is the docker command itself, containerd.io is the low-level container runtime Docker sits on top of, and the two plugins add docker buildx and docker compose as subcommands.
Tricknowtech VPS Hosting
Dedicated KVM resources and full root access — deployed in under 60 seconds, no ticket required.
Ready to try it yourself?
Create a free account and follow along.