How to install the latest version of Docker on Ubuntu

You can install the latest version of Docker on Ubuntu with the following command:

wget -qO - https://gist.githubusercontent.com/jmgraff/1d12b09ad4b8d03914a96334aa4c1a2e/raw/506ef2cca5cf0a754e8da3c2b81cbc91dddca3a9/install-docker.sh | bash

This will run the script described below, which is also hosted as a gist on GitHub.

Why not use apt?

The apt repository does not contain the latest version of Docker. As of this writing, the latest version of Docker is 20.10.23, and the version available via apt-get commands is 20.10.12. That’s pretty close for now, but it’s usually a couple versions behind depending on when you look.

For something like Docker that runs a daemon as the root user, it’s important to have the latest version with all the latest security patches.

The script

The following script combines all the steps found here in Docker’s official documentation (recommend reading this before blindly running the script!) plus it adds you to the docker group, which is necessary to be able to run docker without sudo:

#!/bin/bash

function _separator() {
    cols=$(tput cols)
    for ((i=0; i<cols; i++));do printf "*"; done; echo
}

function _print() {
    echo
    _separator
    echo $@
    _separator
}

_print Removing old docker versions
sudo apt-get -y remove docker docker-engine docker.io containerd runc

_print Updating apt repos
sudo apt-get -y update

_print Installing packages necessary to add 3rd party repos
sudo apt-get -y install \
    ca-certificates \
    curl \
    gnupg \
    lsb-release
    
_print Adding the official Docker repo
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
  
_print "Installing latest Docker from official repo (you may see errors about starting the Docker daemon)"
sudo apt-get -y update
sudo apt-get -y install docker-ce docker-ce-cli containerd.io docker-compose-plugin

_print Adding `whoami` to docker group
sudo usermod -aG docker `whoami`

_print "You've been added to the docker group; log-out and log back in again to run docker commands without sudo"

How the script works

The above script does the following:

  1. Removes any previously installed docker products that you might have installed via apt
  2. Installs packages necessary to use 3rd party apt repos
  3. Adds Docker’s GPG key to your apt keyring
  4. Adds Docker’s official apt repo to your system’s list of repos
  5. Updates your system’s apt repositories to include packages from the official Docker repo
  6. Installs all necessary packages to run docker
  7. Adds the current logged in user to the docker group

Note: You’ll have to log out and back in again after the script finishes in order to run docker commands without sudo.

Troubleshooting

If you previously had the default apt repo version of docker.io installed, you might get Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running? when trying to run docker commands after installing.

To fix this:

  1. Log out
  2. Log back in again
  3. Run sudo systemctl stop docker
  4. Run sudo systemctl start docker
  5. Try running docker ps

And you should be good to go.

Still didn’t work?

  1. Reboot

Works 90% of the time, every time.