This guide covers three methods to install Ansible on Ubuntu — the system package manager (apt), the Ansible PPA for newer versions, and pip for the latest release. Each method has trade-offs between simplicity, version freshness, and control.
Which Installation Method Should You Use?
| Method | Best For | Ansible Version | Updates |
|---|---|---|---|
| apt (Universe) | Quick setup, no external repos | Older (distro version) | With system updates |
| PPA | Newer version via apt | Recent stable | PPA updates |
| pip | Latest version, virtual envs | Latest | pip install --upgrade |
Recommendation: Use pip in a virtual environment for production control nodes. Use apt or PPA for quick testing.
Method 1: Install via apt (Universe Repository)
The simplest method — uses Ubuntu's built-in universe repository:
sudo apt update
sudo apt install -y ansible
Verify Installation
ansible --version
Version by Ubuntu Release
| Ubuntu Version | Ansible Package Version |
|---|---|
| 20.04 LTS | 2.9.x |
| 22.04 LTS | 2.10.x |
| 24.04 LTS | 2.16.x |
Note: Ubuntu 20.04 ships Ansible 2.9, which is significantly outdated and missing many modern features (collections, FQCN support). Consider PPA or pip for 20.04.
Method 2: Install via Ansible PPA
The official Ansible PPA provides newer versions:
sudo apt update
sudo apt install -y software-properties-common
sudo add-apt-repository --yes --update ppa:ansible/ansible
sudo apt install -y ansible
Verify
ansible --version
# Should show a much newer version than the universe repo
Why Use the PPA?
- Gets Ansible updates faster than Ubuntu's release cycle
- Maintained by the Ansible team
- Still uses
aptfor easy management
Remove Old Version First
If you previously installed from universe:
sudo apt remove -y ansible
sudo add-apt-repository --yes --update ppa:ansible/ansible
sudo apt install -y ansible
Method 3: Install via pip (Recommended for Production)
For maximum control and the latest version:
Create a Virtual Environment (Best Practice)
# Install prerequisites
sudo apt update
sudo apt install -y python3 python3-pip python3-venv
# Create virtual environment
python3 -m venv ~/ansible-venv
# Activate it
source ~/ansible-venv/bin/activate
# Install Ansible
pip install ansible
# Verify
ansible --version
Install Globally via pip
sudo apt install -y python3-pip
pip install ansible
Upgrade Ansible
pip install --upgrade ansible
Install a Specific Version
pip install ansible==9.0.0
pip install ansible-core==2.16.0
Post-Installation Setup
Verify Ansible Works
# Check version
ansible --version
# Test with localhost
ansible localhost -m ping
Expected output:
localhost | SUCCESS => {
"changed": false,
"ping": "pong"
}
Create a Basic Configuration
mkdir -p ~/ansible
cat > ~/ansible/ansible.cfg << 'EOF'
[defaults]
inventory = ./inventory
host_key_checking = False
retry_files_enabled = False
[privilege_escalation]
become = True
become_method = sudo
EOF
Create a Test Inventory
cat > ~/ansible/inventory << 'EOF'
[local]
localhost ansible_connection=local
[webservers]
# Add your servers here
# web1.example.com
# web2.example.com
EOF
Troubleshooting
"ansible: command not found"
# Check if it's installed
which ansible
pip show ansible
# If installed via pip in venv, activate it first
source ~/ansible-venv/bin/activate
Python Version Conflicts
# Ensure python3 is the default
python3 --version
# Set the interpreter explicitly
export ANSIBLE_PYTHON_INTERPRETER=/usr/bin/python3
Permission Errors with pip
# Use --user flag instead of sudo
pip install --user ansible
# Or use a virtual environment (recommended)
python3 -m venv ~/ansible-venv
PPA GPG Key Errors
# Re-add the PPA with key
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 93C4A3FD7BB9C367
sudo add-apt-repository ppa:ansible/ansible
Uninstalling Ansible
Uninstall apt Package
sudo apt remove -y ansible
sudo apt autoremove -y
Uninstall pip Package
pip uninstall ansible ansible-core
Remove PPA
sudo add-apt-repository --remove ppa:ansible/ansible
Links
Related Articles
- How to Install Ansible Step-by-Step
- Ansible Tutorial for Beginners
- Ansible Ping Module: Test Connectivity
- How to Fix Ansible Connection Failures
- Ansible Best Practices
- Ansible Configuration File Guide
- Ansible apt Module Guide
Conclusion
For Ubuntu 20.04, avoid the universe repository (ships Ansible 2.9) and use PPA or pip. For Ubuntu 22.04 and 24.04, the universe repository provides reasonably current versions. For production control nodes, pip in a virtual environment gives you the most control over versions and upgrades. Whichever method you choose, verify with ansible localhost -m ping before proceeding.