1. Introduction

We can install a specific Ansible version using the default package manager of some Linux distros. But there may be some limitations. Conversely, we get more flexibility when we use pip, Python’s package installer, to install specific Ansible versions.

In this tutorial, we discuss how to install a specific Ansible version on Linux.

2. Using a Default Package Manager

Setting up specific Ansible versions with the default package manager on Ubuntu and Debian machines is straightforward. However, compared to Ubuntu, version-specific Ansible installations on Debian involve more steps.

2.1. Ubuntu

When trying to install a specific version of Ansible on Ubuntu, we’ll start by adding the PPA for that specific version using add-apt-repository. After that, we’ll install the package with the sudo apt command.

So, say we want to install version 5 of Ansible, we’ll add the PPA, ppa:ansible/ansible-5 first:

$ sudo add-apt-repository --yes ppa:ansible/ansible-5

Then, we’ll install Ansible:

$ sudo apt install ansible

After that, we’ll confirm the version of Ansible we installed using apt show:

$ apt show ansible
Package: ansible
Version: 5.10.0-1ppa~focal
...truncated...

If we ever need to switch to a different Ansible version, we’ll have to remove the current PPA:

$ sudo add-apt-repository --remove --yes ppa:ansible/ansible-5

Then, we’ll uninstall the current Ansible version:

$ sudo apt remove --purge ansible\* -y

After that, we’ll add the PPA for the different Ansible version:

$ sudo add-apt-repository --yes ppa:ansible/ansible-3

Then, we’ll install the new Ansible version – in this case, ansible-3:

$ sudo apt install ansible

Once installed, we verify with apt show once again:

$ apt show ansible
Package: ansible
Version: 3.4.0-1ppa~focal
...truncated...

To get a list of Ubuntu PPAs available for Ansible, we check the “Ansible” Team Launchpad page. We’d click on the hyperlink of our desired version to get more information about its supported Ubuntu releases. From the same page, we’ll also get to know the exact Ansible version and ansible-core version each PPA will install on our Ubuntu release.

2.2. Debian

While we can install Ansible directly from Debian’s default repository, the available versions are typically outdated. So, instead of that, we use .deb packages from Ubuntu PPAs.

When installing Ubuntu’s Ansible packages on a Debian machine, the following equivalences apply:

  • Debian 12 = Ubuntu 22.04 (jammy)
  • Debian 11 = Ubuntu 20.04 (focal)
  • Debian 10 = Ubuntu 18.04 (bionic)

The bullet points above are a guide for choosing the .deb package to install on a Debian machine. What they mean is that, for instance, when installing Ansible on a Debian 11, we’ll use a focal (Ubuntu 20.04) package.

When trying to install a specific Ansible version on Debian, we’ll download the .deb package file from Launchpad manually. Then, we’ll install the package with dpkg.

Before we install the Ansible package, we have to install certain ansible dependencies first. After that, we’ll install ansible-core and, finally, ansible.

Let’s install the dependencies:

$ sudo apt install python3-yaml python3-resolvelib python3-packaging python3-jinja2 python3-markupsafe python3-pyparsing -y

To install ansible-core, we’ll check Launchpad for a suitable .deb package. Then, we’ll download it using curl:

$ curl http://ppa.launchpad.net/ansible/ansible/ubuntu/pool/main/a/ansible-core/ansible-core_2.12.10-1ppa~focal_all.deb -o ansible-core_2.12.10.deb

We’ll install our package, ansible-core_2.12.10.deb:

$ sudo dpkg -i ansible-core_2.12.10.deb

Next, we’ll get a suitable Ansible package from Launchpad:

$ curl http://ppa.launchpad.net/ansible/ansible/ubuntu/pool/main/a/ansible/ansible_5.10.0-1ppa~focal_all.deb -o ansible_5.10.0.deb

Finally, we’ll install our Ansible package, ansible_5.10.0.deb:

$ sudo dpkg -i ansible_5.10.0.deb

Now, we verify the version using dpkg -s:

$ dpkg -s ansible
Package: ansible
Status: install ok installed
Priority: optional
Section: admin
Installed-Size: 308185
Maintainer: Ansible Community Builds <[email protected]>
Architecture: all
Version: 5.10.0-1ppa~focal
...truncated...

Since the server for the .deb packages doesn’t use SSL, installing a specific version of Ansible using this method is potentially unsafe.

3. Using pip

As seen already, using default Linux package managers to install specific Ansible versions come with some disadvantages. With Ubuntu, we cannot use multiple versions simultaneously. With Debian, we have to download .deb packages over unencrypted connections.

To get around those shortcomings, we can install our desired Ansible versions using pip. Once we have pip installed, we can install an ansible version in a single command:

$ pip install ansible==[version]

In some cases, we may have to run pip as python -m pip or python3 -m pip:

$ python3 -m pip install ansible==[version]

So, let’s install ansible 6.6.0 on our machine:

$ pip install ansible==6.6.0

On running the command above, we’ll install ansible 6.6.0 for our current user. But if we want Ansible for all users, we’ll run the same command with sudo.

As we did before, we’ll verify the installed version:

$ pip show ansible
Name: ansible
Version: 6.6.0
...truncated...

Using the same syntax, we can install a specific version of ansible-core. So, let’s install ansible-core 2.11.0 with pip:

$ pip install ansible-core==2.11.0

3.1. Installing Multiple Ansible Versions

If we want multiple Ansible versions, we can install each version in a virtual environment. So, whenever we need any of them, all we have to do is activate the corresponding virtual environment.

To set up Ansible in a virtual environment, we’ll create the virtual environment first:

$ python3 -m venv ansible-[version]-venv

We can assign any name to the virtual environment. In our case, we’re choosing to use ansible-[version]-venv, so we can easily identify the virtual environment of each Ansible version.

So, let’s create a virtual environment for ansible 6.6.0:

$ python3 -m venv ansible-6.6.0-venv

Once created, we’ll activate the virtual environment:

$ source ./ansible-6.6.0-venv/bin/activate

Finally, we’ll install Ansible in the virtual environment:

(ansible-6.6.0-venv) $ pip install ansible==6.6.0

Now, whenever we need ansible 6.6.0, all we have to do is activate ansible-6.6.0-venv, and we’re good to go. Of course, we can follow the same steps to install multiple ansible-core versions.

4. Using pipx

In some cases, we may be unable to install Ansible using pip due to some operating system restrictions. But in such instances, pipx will come in handy.

pipx circumvents OS restrictions on pip by installing and executing Python packages in a virtual environment. It does pretty much the same thing as pip with some extra features.

To use pipx, we’ll install it first:

$ python3 -m pip install pipx

Where possible, we can run the pip command without python3 -m or python -m.

On Ubuntu, we can also install pipx using apt:

$ sudo apt install pipx

On downloading pipx, we’ll run pipx ensurepath to ensure that pipx‘s installation directory is in the PATH variable:

$ pipx ensurepath
Added /home/baeldung/.local/bin to the PATH environment variable in /home/baeldung/.bashrc

As seen above, if the directory isn’t in PATH, the command will add it to PATH.

Now that we’ve installed pipx, we can do a version-specific Ansible installation following the same syntax as pip:

$ pipx install ansible==[version]

So, let’s install ansible 6.6.0:

$ pipx install ansible==6.6.0

With that, we now have ansible 6.6.0 on our machine:

$ pipx list
...truncated...
   package ansible 6.6.0, installed using Python 3.9.2
    - ansible-community

We can also do a version-specific installation of ansible-core using pipx. We’ll illustrate this by installing ansible-core 2.11.0:

$ pipx install ansible-core==2.11.0

5. Installing Ansible From GitHub

If we’re unable to install a particular version of Ansible using the other methods, we can install it from the source code. For this, we must have the Python setuptools module installed.

We’ll start by cloning the branch of our desired version using git clone:

$ git clone -b stable-2.9 --single-branch https://github.com/ansible/ansible

The Ansible GitHub repository has multiple branches for various Ansible versions. In our case, we’re cloning the stable-2.9 branch because we want to install ansible 2.9.

While cloning the repository, we specified the branch we wanted using the -b flag. Then, we ensured we cloned only that branch by adding the –single-branch flag.

5.1. Using pip

Now that we have the ansible 2.9 repository locally, we’ll cd into the directory. Then, when in the directory, we’ll install dependencies:

$ pip install -r requirements.txt

Once that’s done, we’ll run pip install to install Ansible from either pyproject.toml or setup.py as the case may be:

$ pip install .

With that, we have ansible 2.9 installed:

$ ansible --version
ansible 2.9.27.post0
...truncated...

5.2. Using make

We can also install Ansible from the repo’s local directory using make. All we have to do is run make install from the directory:

$ sudo make install

On complete execution, we can confirm the version:

$ ansible --version
ansible 2.9.27.post0
...truncated...

When installing with make, we had to create a symbolic link from /usr/bin/python to /usr/bin/python3:

$ sudo ln -s /usr/bin/python3 /usr/bin/python

We did that because the source Makefile specified the Python binary as python (PYTHON=python). However, our Python binary is python3. So, we were getting an error message:

$ sudo make install
/bin/sh: 1: python: not found
Makefile:43: *** "version_helper failed".  Stop.

Instead of creating a symbolic link, we could’ve also edited the Makefile, replacing PYTHON=python with PYTHON=python3.

6. Conclusion

In this article, we talked about version-specific Ansible installations using apt on Ubuntu and dpkg on Debian. Then, we discussed pip, pipx, and installation from the source code as uncomplicated and flexible alternatives.