For some time now I do system installations on Linux machines and I find myself searching the exact commands to quickly install Docker on Ubuntu and be able to run docker commands without sudo. So, here is a quick guide to do that.

System configuration

The steps hereafter have been tested on:

  • Ubuntu 18
  • Ubuntu 20
  • Ubuntu 22
  • Ubuntu 24

Install Docker

Cleanup old installations

Bash
# delete old and conflicting packages
for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc; do sudo apt-get remove $pkg; done

Install tools and Docker repository

Bash
# install necessary tools
sudo apt-get update
sudo apt-get install ca-certificates curl

# add docker repository to your repository list
sudo install -m 0755 -d /etc/apt/keyrings

sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
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

Install Docker and recommended third-party packages

Bash
# install docker packages
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

# test the installation
sudo docker run hello-world

Run Docker without sudo

Add the current user to the docker group

In order for a user to be able to run docker commands, he must be part of the docker group which has been created during the previous Docker installation.

Bash
# check that the docker group has been created
# if so, you shoud find an entry like "docker:x:999:"
sudo cat /etc/group

# if not, add it explicitely; otherwise skip this step
sudo groupadd docker

# add the current user to the docker group; he will be the one able to run docker without privileges
sudo usermod -aG docker $USER

# check that your user has been added to the docker group
# if everything went fine, the docker entry should be now so;ething like "docker:x:999:ubuntu" where ubuntu is the current user name
sudo cat /etc/group

After these commands, you need to log out and back in for your groups to be reevaluated.

Test the installation

Bash
# re-test; you should have the same result with and without root now
docker run hello-world