Monday, February 10, 2025

How to Setting Up Docker and Python on Ubuntu 24.04 LTS


This guide will walk you through setting up Docker and Python on Ubuntu 24.04 LTS, the latest Long Term Support release, ensuring a robust and future-proof development environment.

In this article, you'll learn how to:

  • Install Docker on Ubuntu 24.04 LTS: Get Docker up and running to containerize your applications for consistent and portable workflows.
  • Validate Docker Installation: Verify your Docker setup with a simple "Hello World" container.
  • Set Up Python on Ubuntu 24.04 LTS: Ensure Python, along with essential tools like pip and venv, is properly configured for your development projects.
  • Validate Python Setup: Confirm Python and its package manager are ready to go with a quick test.

With clear, step-by-step instructions and hands-on validation, you'll have a fully functional development environment ready to kickstart your projects!

Setting Up Docker on Ubuntu 24.04 LTS

Docker is a game-changer for modern application development, allowing you to build, ship, and run applications in isolated containers. Let's dive into setting up Docker on Ubuntu 24.04 LTS.

Why Docker?

Docker simplifies the entire application lifecycle. It enables you to:

  • Simplify Deployment: Create portable, containerized environments that ensure your application runs consistently across different systems.
  • Efficiently Manage Databases: Easily provision and manage databases within containers, keeping your development environment clean and organized.
  • Isolate Dependencies: Avoid conflicts between project dependencies by encapsulating each project in its own container.
  • Enhance Testing: Create consistent and reproducible testing environments, ensuring reliable application behavior.

Step-by-Step Docker Installation on Ubuntu 24.04 LTS

Here’s how to install Docker on Ubuntu 24.04 LTS:

  1. Update Your Package Index:

    First, update your system's package index to ensure you have the latest package information:

    Bash
    sudo apt update
    
  2. Install Prerequisites:

    Install packages that allow apt to use repositories over HTTPS:

    Bash
    sudo apt install apt-transport-https ca-certificates curl software-properties-common -y
    
  3. Add Docker’s Official GPG Key:

    Add Docker’s GPG key to authenticate packages from the Docker repository:

    Bash
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
    
  4. Set Up the Docker Repository:

    Add the Docker APT repository to your system's sources list:

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

    Note: While Ubuntu 24.04 LTS is 'noble', using 'mantic' ensures compatibility as Docker often lags slightly in official support for the newest Ubuntu releases. This is a common practice and generally safe.

  5. Update Package Index Again:

    Update the package index to include the new Docker repository:

    Bash
    sudo apt update
    
  6. Install Docker Engine:

    Finally, install Docker Engine, containerd, and Docker Compose:

    Bash
    sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y
    
  7. Verify Docker Installation:

    Check if Docker is installed and running correctly:

    Bash
    sudo systemctl status docker
    

    You should see output indicating that the Docker service is active and running.

Validating Docker

To ensure Docker is working as expected, run the classic "Hello World" container image:

Bash
docker run hello-world

If Docker is set up correctly, you'll see a confirmation message from the hello-world image.

Grant User Docker Access (Optional, Recommended)

By default, Docker commands require sudo. To run Docker commands without sudo, add your user to the docker group:

Bash
sudo usermod -aG docker ${USER}

Important: After running this command, restart your current session (log out and log back in) for the changes to take effect.

Setting Up and Validating Python on Ubuntu 24.04 LTS

Python is a highly versatile and widely used programming language, essential for various development tasks, data analysis, and scripting. Ubuntu 24.04 LTS comes with Python 3 pre-installed, but let's validate the installation and set up essential Python1 tools.

Steps to Validate Python

  1. Check Python 3 Installation:

    Open your terminal and check if Python 3 is installed by running:

    Bash
    python3 --version
    

    You should see the Python 3 version2 printed in the output.

  2. Test Python Interpreter:

    Launch the Python 3 interpreter and run a simple command:

    Bash
    python3
    

    Once the Python interpreter starts (you'll see >>>), type:

    Python
    print("Hello, World from Python!")
    

    and press Enter. It should print "Hello, World from Python!". Then, exit the interpreter by typing:

    Python
    exit()
    
  3. Install pip (Python Package Installer):

    pip is the package installer for Python. While it might be pre-installed, ensure it's available and up-to-date:

    Bash
    sudo apt update
    sudo apt install python3-pip -y
    
  4. Validate pip:

    Check the pip version:

    Bash
    pip3 --version
    

    You should see the pip version associated with Python 3.

  5. Set Up Virtual Environments (venv):

    Virtual environments are crucial for isolating project dependencies. Install the venv module:

    Bash
    sudo apt install python3-venv -y
    
  6. Create and Test a Virtual Environment:

    Create a virtual environment to validate the setup:

    Bash
    python3 -m venv test_env
    

    This command creates a virtual environment directory named test_env. List the directory contents to confirm:

    Bash
    ls -ltr test_env
    

    To clean up, remove the test environment:

    Bash
    rm -rf test_env
    

Tips for a Smooth Setup

  • Run Commands Sequentially: Execute each command in the provided order and ensure each step completes successfully before proceeding to the next. Pay attention to any error messages and troubleshoot accordingly.
  • Restart Session After Docker Group Modification: Remember to restart your session after adding your user to the docker group to apply the permissions changes.
  • Keep Software Updated: Regularly update Docker and Python packages using sudo apt update and sudo apt upgrade to benefit from the latest features, security patches, and bug fixes.
  • Practice Containerization: Experiment with creating simple Docker images and containers. Start with basic images like hello-world and gradually explore more complex applications to solidify your understanding of Docker concepts.

Conclusion

Congratulations! You've successfully set up Docker and validated Python on your Ubuntu 24.04 LTS system. You now have a powerful and versatile development environment ready for your next coding adventure. Embrace containerization with Docker and leverage the flexibility of Python to build amazing applications.

0 comments:

Post a Comment