Install Docker on Ubuntu

Learn how to install the latest Docker version on Ubuntu straight from the Docker developers.

Looking for the easiest way to install Docker in Ubuntu? Well, here you have it.

But before proceeding with installation, it is important to remove the old docker package (if you have any):

sudo apt-get remove docker docker-engine docker.io containerd runc

Now, if you don't care about using a slightly old docker version, you can use the default repository for docker installation:

sudo apt install docker.io

And that's it.

But if you want to use the most recent docker version, you will have to use the official repository for installation, and here's a detailed guide.

How to install Docker in Ubuntu

So let's start with installing packages that will let apt use HTTPS over the repository:

sudo apt-get install ca-certificates curl gnupg lsb-release

Next, use the given command that will create the directory /etc/apt/keyrings if not exist with -p option:

sudo mkdir -p /etc/apt/keyrings

Adding GPG keys

Now, you have to add the GPG key that will ensure the authenticity of the packages coming from the Docker developers.

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

Adding Docker repository

The repository is where the packages are located. To add a docker repository to your package sources in Ubuntu, you can use the given command:

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

Installing Docker in Ubuntu

To take effect from the changes you've made till now, you have to update the repository index:

sudo apt update

Now, you can install the most recent version of docker using the given command:

sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin

Running hello-world image

To check whether you have successfully installed docker or not, you can run hello-world image:

sudo docker run hello-world
install latest version of docker in ubuntu

And that's it! you have the most recent version of docker installed in Ubuntu.

But have you noticed that it needs sudo access to run docker commands? If you find that annoying, here's what you can do.

Use Docker without sudo in Ubuntu

If you try to use docker without sudo (superuser privileges), it throws the following error:

First, let's create a new user group named docker by using the following command:

sudo groupadd docker

Now, you just have to add the user to the recently created user group:

sudo usermod -aG docker <username>
⚠️
Make sure the user you are adding to the docker user group has superuser privileges.

Next, you have to log out and log back in so your system can re-evaluate your group membership. And if you are using VM, a reboot is necessary.

If you want to avoid a reboot, you can use newgrp command:

newgrp docker

And now you can access docker without adding sudo every time:

use docker without sudo in ubuntu

Wrapping Up

With this tutorial, you learned to install the latest Docker version straight from the Docker developers. You also learned to run docker commands without sudo.

Let me know if you have questions or suggestions.

Linux Foundation Training