Command To Install Docker In Ubuntu

Command To Install Docker In Ubuntu

Reading time1 min
#Docker#Ubuntu#Linux#DockerInstallation#Containerization#DevOps

How to Install Docker on Ubuntu: Step-by-Step Guide

If you’re diving into containerization or want to run lightweight, portable applications, Docker is the tool to learn. Installing Docker on Ubuntu is quick and easy—once you know the correct commands.

In this guide, I’ll walk you through the exact commands needed to install the latest version of Docker on your Ubuntu system. Whether you’re using Ubuntu 20.04, 22.04, or newer, these steps will work almost identically.


Why Install Docker on Ubuntu?

Docker allows you to package applications and their dependencies into containers that run consistently across environments. On Ubuntu—a popular Linux distribution—Docker can empower developers, sysadmins, or anyone curious about modern app deployment.


Prerequisites

  • Ubuntu machine (desktop or server)
  • Basic command line knowledge
  • A user with sudo privileges

Step 1: Update Your System Packages

Before installing anything new, update your package index so your system downloads the latest versions.

sudo apt update
sudo apt upgrade -y

Step 2: Install Required Dependencies

To let apt use repositories over HTTPS—which Docker’s setup requires—we install some additional packages:

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

Step 3: Add Docker’s Official GPG Key

Docker signs their packages with a GPG key to verify authenticity. Add this key so your system can trust Docker’s repository:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

Step 4: Add the Official Docker Repository

Next, add Docker’s stable repository to your APT sources:

echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] \
  https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

This ensures that when you install packages named docker-ce, you get the official Docker build and not the old distro version.


Step 5: Update Package Index Again

Now that you’ve added a new repository, refresh your package lists:

sudo apt update

Verify APT picks up Docker from their repo by running:

apt-cache policy docker-ce

You should see something like:

  Candidate: <version>
  Version table:
     <version> 500
        500 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
     ...

Step 6: Install Docker Engine

Now install Docker Community Edition (docker-ce) along with CLI tools and container runtime:

sudo apt install docker-ce docker-ce-cli containerd.io -y

Step 7: Verify that Docker is Installed Successfully

Check Docker version:

docker --version

Expected output for recent versions:

Docker version 24.0.xx, build ...

Test running a container (hello-world image):

sudo docker run hello-world

If everything is working well, this prints a friendly message confirming your installation.


Optional: Run Docker Without Sudo

By default, you need sudo to run docker commands. To avoid typing it every time:

  1. Create the docker group if it doesn’t exist:

    sudo groupadd docker
    
  2. Add your user to the group:

    sudo usermod -aG docker $USER
    
  3. Log out and log back in or reboot for changes to take effect.

Now test without sudo:

docker run hello-world

It should work seamlessly!


Troubleshooting Tips

  • Docker service not running?

    Start and enable it with:

    sudo systemctl start docker
    sudo systemctl enable docker
    
  • Permission denied errors? Make sure your user is in the docker group or use sudo.

  • Old version installed? Remove old versions before installing new ones by running:

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

Summary of Commands

For quick reference — here are all commands in order:

sudo apt update && sudo apt upgrade -y 
sudo apt install ca-certificates curl gnupg lsb-release -y 
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg 
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] \
https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null 
sudo apt update 
sudo apt install docker-ce docker-ce-cli containerd.io -y 
docker --version 
sudo docker run hello-world 

(Optional for non-sudo usage)

sudo groupadd docker 
sudo usermod -aG docker $USER 
# Then log out/in again.

Final Thoughts

Installing Docker on Ubuntu unlocks powerful capabilities for building and deploying apps easily inside containers. The commands above ensure a smooth setup on any supported Ubuntu release.

Try pulling some images like nginx, postgres, or your own project. Happy Dockering!

If you found this guide useful, share it with friends and bookmark it for future setups.


Have questions or want me to cover advanced topics like Docker Compose or Kubernetes next? Drop a comment below!