Last modified: Oct 04, 2026

Install Ansible in Python

Ansible is a powerful automation tool. It helps you manage servers, deploy apps, and configure systems. Many people think Ansible is only a Linux tool. In fact, it runs on Python. So you can install it like any other Python package.

This guide shows you how to install Ansible in Python. It is written for beginners. You will learn the easy way and the clean way. You will also learn how to fix common problems.

What You Need Before You Start

Before you install Ansible, check a few things. First, you need Python on your machine. Ansible works best with Python 3.8 or newer.

Second, you need pip. This is the Python package installer. It comes with most Python versions.

Third, you need a terminal or command prompt. On Windows, use PowerShell or Command Prompt. On macOS or Linux, use your normal terminal.

Let us check your Python version first.


python3 --version

You should see something like this:


Python 3.11.4

If you see an old version, update Python first. Ansible may not work on Python 2.7 anymore.

The Simple Way to Install Ansible

The fastest way to install Ansible is with pip. Open your terminal and run this command.


pip install ansible

On some systems, you may need to use pip3 instead.


pip3 install ansible

This command downloads Ansible and all its dependencies. It may take a minute or two. When it finishes, Ansible is ready to use.

Now check if it worked. Run this command.


ansible --version

You should see output like this:


ansible [core 2.15.4]
  config file = None
  configured module search path = ['/home/user/.ansible/plugins/modules']
  ansible python module location = /usr/lib/python3/dist-packages/ansible
  ansible collection location = /home/user/.ansible/collections
  executable location = /usr/bin/ansible
  python version = 3.11.4 (main, Jun  7 2023, 10:13:09) [GCC 12.2.0]

If you see this, you are done. The installation was a success.

The Clean Way: Use a Virtual Environment

The simple way works fine. But it installs Ansible for your whole system. This can cause problems later. A better way is to use a virtual environment.

A virtual environment is a separate space for your Python packages. It keeps your projects apart. This is a best practice for any Python work.

First, create a new folder for your project.


mkdir ansible-project
cd ansible-project

Next, create the virtual environment. Run this command.


python3 -m venv venv

This creates a folder named venv. It holds your isolated Python setup.

Now activate the environment. The command is different on each system.

On Linux and macOS:


source venv/bin/activate

On Windows:


venv\Scripts\activate

You will see the name of your environment in the terminal. It looks like (venv).

Now install Ansible inside this environment.


pip install ansible

This time, Ansible goes only into your project folder. Your system stays clean. When you are done, just run deactivate to leave the environment.

Install a Specific Version of Ansible

Sometimes you need an older or exact version. Maybe your team uses a fixed version. You can install it with pip too.


pip install ansible==2.15.4

This installs exactly version 2.15.4. This is useful for reproducible setups. Everyone on your team gets the same tool.

Test Your Ansible Installation

Now let us run a quick test. Create a simple inventory file first. This file lists the machines you want to manage.


echo "localhost ansible_connection=local" > inventory.ini

Next, run a simple Ansible command. The ping module checks if a host is reachable.


ansible all -i inventory.ini -m ping

You should see output like this:


localhost | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}

The word pong means it works. Your Ansible installation is ready.

Common Problems and How to Fix Them

Sometimes things go wrong. Here are the most common issues and their fixes.

Problem 1: pip is not found

If you see pip: command not found, pip is missing. Install it with your system package manager. On Ubuntu, run sudo apt install python3-pip.

Problem 2: Permission denied

If you see a permission error, do not use sudo with pip. That can break your system. Instead, use a virtual environment. Or add --user to install for your account only.


pip install --user ansible

Problem 3: ansible command not found

If Ansible installs but the command fails, your PATH may be wrong. This often happens with --user installs. The scripts go to a hidden folder. Add it to your PATH.


export PATH="$HOME/.local/bin:$PATH"

Add this line to your .bashrc or .zshrc file. Then it stays set for every new terminal.

Problem 4: Old Python version

If your Python is too old, pip will refuse to install Ansible. Check the version with python3 --version. Upgrade Python if needed.

Using Ansible After Installation

Once installed, Ansible has many uses. You can run ad-hoc commands. You can write playbooks. You can manage cloud servers.

A playbook is just a YAML file. Here is a small example. It installs a web server on a remote host.


# playbook.yml
# This playbook installs nginx on all web servers
- name: Install nginx
  hosts: webservers
  become: yes
  tasks:
    - name: Install nginx package
      apt:
        name: nginx
        state: present

Run it with this command.


ansible-playbook -i inventory.ini playbook.yml

This is the real power of Ansible. It turns manual work into simple, repeatable code.

Should You Use pip or a Package Manager

Some systems have Ansible in their package manager. For example, Ubuntu has apt install ansible. This works, but the version may be old.

Using pip gives you the newest version. It also works on any system. This is why most people install Ansible with pip inside a virtual environment.

Just remember one rule. Do not mix both methods. Pick one and stick with it. Mixing them causes version conflicts.

Conclusion

Installing Ansible in Python is easy. Use pip install ansible for a quick setup. Use a virtual environment for a clean, safe setup. Both methods work well.

Always check your Python version first. Always verify the install with ansible --version. If you hit an error, check pip, permissions, and your PATH.

Now you are ready to automate. Write your first playbook and let Ansible do the hard work for you.