How to Install Ansible on Ubuntu 22.04 (Step-by-Step Guide)

Install Ansible on Ubuntu

Before we dive into the installation process of Ansible on Ubuntu, let’s first explore what Ansible is, why it’s beneficial to install ansible on Ubuntu, and the prerequisites you should know before getting started.

What is Ansible?

Imagine having dozens of servers and needing to configure or update them all at once. Doing it manually? That’d be a nightmare. Enter Ansible — an open-source, agentless automation tool that lets you manage systems, deploy apps, and orchestrate configurations with ease.

Ansible uses YAML-based playbooks to define your automation tasks. It’s simple, powerful, and — best of all — doesn’t require installing any agent on the nodes you want to manage. You just need SSH access and Python installed.

In short, it’s like having a remote control for your infrastructure, and yes, it’s as cool as it sounds.

Why Use Ansible on Ubuntu?

Ubuntu is one of the most widely used Linux distributions for servers and dev environments, and pairing it with Ansible makes automation even smoother.

Here’s why Ansible + Ubuntu = match made in DevOps heaven:

  • Ubuntu’s package system makes installing Ansible painless
  • Built-in Python support ensures compatibility
  • Strong community support
  • Ideal for cloud environments (AWS, Azure, DigitalOcean, etc.)

Common Use Cases for Ansible

  • Provisioning cloud instances
  • Patching and updating servers
  • Automating repetitive tasks
  • Managing Docker containers
  • Deploying web servers or microservices
  • Setting up dev environments for teams

With Ansible, you write it once and reuse it forever. Total time-saver!

Prerequisites Before Installing Ansible on Ubuntu

What You Need (System Requirements

To install Ansible on Ubuntu 22.04, you need:

  • A system running Ubuntu 22.04 LTS
  • Sudo privileges or root access
  • Internet connection to pull packages
  • Basic terminal knowledge

If you plan on managing other systems, make sure:

  • SSH is enabled on the target machines
  • Python is installed (it’s usually already there on most Linux distros)

Update and Upgrade Your System

Before installing anything new, it’s always a good idea to make sure your system is up-to-date.

Run:

bash
sudo apt update && sudo apt upgrade -y

This ensures that you’re not running into any conflicts during or after installation.

Install Required Packages

Ansible relies on Python and a few supporting tools. Run this to install what you need:

bash
sudo apt install software-properties-common python3 python3-pip -y

These packages enable the addition of PPAs and handle Python-based dependencies. Python is especially important since Ansible is written in Python and depends on it.

Method 1 – Installing Ansible Using Apt (Official Ubuntu Repos)

Step-by-Step Installation

Ubuntu 22.04 includes Ansible in its default repositories. If you’re okay with using the version that Ubuntu provides (which may not always be the latest), then this is the fastest and easiest way.

  • Install Ansible:
bash

sudo apt install ansible -y
  • Check the installed version:
bash

ansible --version You should see something like:
css
ansible [core 2.10.x]
  • That’s it! Ansible is now installed and ready to go.

Pros and Cons of This Method

Pros:

  • Quick and easy
  • Stable packages from Ubuntu repos
  • Minimal setup required

Cons:

  • Version may be outdated compared to upstream releases
  • Some new features might be missing

If you’re just getting started or setting up a non-critical environment, this method is totally fine. But if you need the latest features, check out the next method.

Method 2 – Installing Ansible Using PPA (Preferred for Latest Version)

Add Ansible PPA

Ansible provides its own official PPA (Personal Package Archive) with the latest version for Ubuntu systems. This is the recommended way to get the most current, stable release.

  1. Add the PPA:
    bash
    sudo add-apt-repository –yes –update ppa:ansible/ansible

  • Update package list:
bash

sudo apt update

Install Latest Ansible Version

  1. Now install Ansible from the PPA: bashCopyEditsudo apt install ansible -y
  2. Confirm the version:
bash

ansible --version You’ll likely see something like:
css
ansible [core 2.14.x]

This version includes newer modules, improved playbook performance, and enhanced cloud integrations.

Verify the Installation

Try running a quick ping to localhost:

bash
ansible localhost -m ping --connection=local

If you get a "pong" response, you’re officially up and running!

Method 3 – Installing Ansible via Pip (Python Package Manager)

When to Use pip for Ansible

Using pip to install Ansible on ubuntu is ideal if:

  • You want to run multiple Ansible versions (per project)
  • You’re using Python virtual environments
  • You’re building a custom setup or working in a Python-centric workflow

This method gives you flexibility and control, especially if you’re managing automation scripts in isolated environments.

Installing pip and Ansible

If pip isn’t already installed, start with:

bash
sudo apt install python3-pip -y

Then, install Ansible using pip:

bash
pip3 install --user ansible

This installs Ansible in your user directory without requiring sudo. To make sure it’s accessible from your terminal, add the following to your .bashrc or .zshrc:

bash
export PATH=$PATH:~/.local/bin

Reload the shell or source the config:

bash
source ~/.bashrc

Pros and Cons of pip Method

Pros:

  • Installs the latest version directly from PyPI
  • Works well with Python virtual environments
  • Great for isolated, project-specific installations

Cons:

  • Slightly more manual setup
  • Not managed through your system package manager
  • Can cause confusion if multiple versions are installed

Use this method if you’re comfortable with Python and want more fine-tuned control over your environment.

Post-Installation Setup

Creating an Ansible Inventory File

The inventory file tells Ansible where your target machines live. By default, it’s located at:

bash
/etc/ansible/hosts

Here’s a simple example:

ini
[webservers]
192.168.1.10
192.168.1.11

[databases]
192.168.1.12

This allows you to group hosts and target them in playbooks or ad-hoc commands.

Setting Up SSH Access to Managed Nodes

Ansible connects to other machines via SSH. Make sure:

  • SSH is installed on both control and target machines
  • You have passwordless SSH access (via keys)

To generate an SSH key (if you don’t have one already):

bash
ssh-keygen

Then copy your key to the target server:

bash
ssh-copy-id user@192.168.1.10

This step is crucial because Ansible is agentless — it relies on SSH instead of running a separate daemon on the managed nodes.

Running Your First Ansible Command

With SSH set up and your inventory ready, try this:

bas
ansible all -m ping

If everything’s working, each host will reply with "pong". This confirms that Ansible can reach and communicate with your machines.

Troubleshooting Common Ansible Installation Issues

Fixing Broken Dependencies

If Ansible won’t install or throws dependency errors:

  • Clean the package manager cache:
    bash
    sudo apt clean sudo apt update
  • Use --fix-broken to resolve missing dependencies:
    bash
    sudo apt –fix-broken install

Resolving pip Errors

Common pip-related errors:

  • Permission denied: Use --user to install without sudo.
  • Path not found: Add ~/.local/bin to your $PATH.

Example fix:

bash
echo 'export PATH=$PATH:~/.local/bin' >> ~/.bashrc
source ~/.bashrc

Checking Python and pip Versions

Make sure Python and pip are correctly installed:

bash
python3 --version
pip3 --version

Ansible needs Python 3.6+ to run smoothly. If your system doesn’t meet the requirements, upgrade Python first.

Best Practices for Using Ansible on Ubuntu

Organizing Your Playbooks and Roles

Once you’re comfortable running a few ad-hoc commands, it’s time to level up your Ansible game. Start organizing your automation using playbooks and roles.

Structure your project like this:

css
project/
├── inventory
├── ansible.cfg
├── playbook.yml
└── roles/
├── webserver/
│ ├── tasks/
│ │ └── main.yml
│ ├── handlers/
│ └── templates/
└── database/

Why use this structure?

  • Makes your setup scalable and reusable
  • Helps you collaborate with teams more easily
  • Keeps things tidy and easy to troubleshoot

Using Virtual Environments

For Python users or teams managing multiple projects, using virtual environments is a smart move:

bash
python3 -m venv ansible-env
source ansible-env/bin/activate
pip install ansible

This way, your Ansible version is isolated from the system version — perfect for avoiding conflicts.

Keeping Ansible Up to Date

If you installed Ansible using apt or the official PPA, update it regularly:

bash
sudo apt update
sudo apt upgrade ansible

If you used pip, then:

bash
pip install --upgrade ansible

Staying updated ensures you get:

  • New modules and features
  • Performance enhancements
  • Security patches

Always check Ansible’s release notes for major version changes before upgrading in production.

Conclusion

The install of Ansible on Ubuntu 22.04 process is fast, easy, simple, and user-friendly, no matter if you’re an experienced DevOps professional or just beginning your journey to automation.

You’ve now learned:

  • How to install Ansible on ubuntu using apt, PPA, and pip
  • How to configure your inventory and test SSH access
  • The basics of running Ansible commands
  • How to troubleshoot and apply best practices

With Ansible within your toolbox You’re not just managing servers – you can master the process. Automatization is the way of the future and Ansible assists you in achieving it with less stress and more power available to you.

FAQs

1. What is the default location of Ansible config files?

The default config file is:

bash
/etc/ansible/ansible.cfg

You can create a project-specific config file in your working directory for custom settings like inventory paths and logging.

2. Can I use Ansible without root access?

Yes, but you’ll need:

  • SSH access to the target machines
  • Sudo permissions configured for the user (if you need to make privileged changes)

You can also configure Ansible to use --ask-become-pass for privilege escalation.

3. Is Ansible agentless?

Absolutely. That’s one of its biggest perks. It doesn’t need to install anything on the nodes it manages — just SSH access and Python (usually already there on Linux systems).

4. What’s the difference between Ansible and Terraform?

  • Ansible: Focuses on configuration management and automation after infrastructure exists.
  • Terraform: Focuses on infrastructure provisioning — setting up servers, databases, and networks.

Many teams use both tools together in DevOps pipelines.

5. How do I upgrade Ansible on Ubuntu?

It depends on how you installed it:

  • From apt:
    bash
    sudo apt update && sudo apt upgrade ansible
  • From pip: bash
    pip install –upgrade ansible
  • From PPA: Update the PPA and install the new version:
    bash
    sudo apt update sudo apt upgrade