How to Install Docker on Ubuntu (Step-by-Step with Script) [2025 Guide]

Introduction

Docker is one of the most essential tools in modern DevOps and containerization. In this guide, you’ll learn how to install Docker on Ubuntu using a simple shell script — fully automated and optimized for local machines, servers, and even cloud VMs like AWS EC2.

Docker installed on Ubuntu via command line – 2025 Guide
Installing Docker on Ubuntu 22.04 using a step-by-step bash script

Prerequisites

  • Ubuntu 20.04 or 22.04
  • A non-root user with sudo access

Installation Script

Run below bash script to install docker on ubuntu server

#!/bin/bash

set -e

echo "Updating packages..."
sudo apt update && sudo apt upgrade -y

echo "Installing dependencies..."
sudo apt install -y ca-certificates curl gnupg lsb-release

echo "Adding Docker GPG key..."
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 " Adding Docker repository..."
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

echo "Updating package index..."
sudo apt update

echo "Installing Docker Engine and CLI..."
sudo apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

echo "Enabling Docker service..."
sudo systemctl enable docker
sudo systemctl start docker

echo " Adding user '$USER' to docker group..."
sudo usermod -aG docker $USER

echo "Docker installation complete and ready to use without sudo!"

Step to run the script

  1. Copy the script in a file

2. Give executable permission to script

3. Execute the script

4. Try doing docker ps: If getting below error :

ubuntu@ip-172-31-41-114:~$ docker ps
permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get "http://%2Fvar%2Frun%2Fdocker.sock/v1.49/containers/json": dial unix /var/run/docker.sock: connect: permission denied

Run newgrp docker or logout and open new terminal.

sudo usermod -aG docker $USER
newgrp docker

Verify Docker Installation:

docker ps
docker --version

Conclusion

Installing Docker on Ubuntu doesn’t have to be complicated. With the script provided above, you’ve now automated the entire process — from installing dependencies to setting up the Docker daemon and enabling non-root access. Whether you’re preparing your environment for local development, container-based CI/CD pipelines, or even Kubernetes deployments, having Docker set up correctly is the first critical step.

Leave a Reply

Your email address will not be published. Required fields are marked *