# How to Install Ansible on a Mac: A Modern Approach

> A guide to installing and managing Ansible on macOS using Conda, with tips for handling collections and dependencies.

- Canonical: https://www.russ.cloud/2024/09/15/how-to-install-ansible-on-a-mac-a-modern-approach/
- Published: 2024-09-15
- Author: Russ McKendrick
- Tags: ansible, macos, devops, python

---

As the DevOps landscape evolves, so do our methods for managing tools like Ansible. This post outlines my current preferred approach to installing and managing Ansible on macOS.

## The Traditional Homebrew Approach

For years, many macOS users, including myself, relied on Homebrew to install and maintain Ansible. The process was straightforward:

```text frame="terminal" title="Installing Ansible using Homebrew"
brew install ansible
```


While this method effectively managed the core Ansible installation, it became problematic as Ansible's ecosystem grew more complex.

### The Dependency Dilemma

As more collections moved away from the core Ansible distribution, managing prerequisites for various collections became increasingly challenging. Some collections had specific dependency requirements, leading to potential conflicts and a maintenance headache - especially when doing other Python development which have the same dependency requirements but you need to use other versions than those defined by the `requirements.txt` which ships with a collection.

## A Modern Solution: Conda for Ansible Management

Having experienced the pitfalls of Python dependency management, I've transitioned to using Conda for Ansible. This approach allows for a dedicated, isolated Ansible environment, mitigating the risks of "dependency hell."

### Step-by-Step Installation Process

1. **Create and activate a Conda environment for Ansible:**

```text frame="terminal" title="Setting up Ansible environment with Conda"
conda create -n ansible python=3.12
conda activate ansible
```


2. **Install Ansible within the environment:**

```text frame="terminal" title="Installing Ansible in Conda environment"
python -m pip install ansible
```


### Managing Ansible Collections

Once Ansible is installed, you can add collections and their dependencies using Ansible Galaxy and pip.

<TipCallout title="Tip">
Always ensure you're in your Ansible Conda environment by running `conda activate ansible` before executing the following commands.
</TipCallout>

For example, to install the Azure Collection and its dependencies:

```text frame="terminal" title="Adding the Azure collection"
ansible-galaxy collection install azure.azcollection
python -m pip install -r ~/.ansible/collections/ansible_collections/azure/azcollection/requirements.txt
```


### Upgrading Ansible and Collections

Keeping Ansible and its collections up-to-date is crucial. Here's how to do it:

1. **Upgrade Ansible:**

```text frame="terminal" title="Upgrading Ansible"
python -m pip install ansible --upgrade
```


2. **Upgrade a specific collection (e.g., Azure):**

```text frame="terminal" title="Upgrading Azure Collection"
ansible-galaxy collection install azure.azcollection --upgrade
python -m pip install -r ~/.ansible/collections/ansible_collections/azure/azcollection/requirements.txt
```


## Summary and Best Practices

Adopting Conda for Ansible management on macOS offers several advantages:

1. **Isolation**: Your Ansible setup exists in its own environment, preventing conflicts with other Python applications.
2. **Flexibility**: Easily manage different versions of Ansible for various projects.
3. **Dependency Control**: Better handle complex dependencies required by different Ansible collections.
4. **Consistency**: Ensure a consistent environment across different machines or team members.

To make the most of this setup:

- Regularly update both Ansible and its collections to benefit from the latest features and security patches.
- Explore [Conda's environment export](https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html#exporting-the-environment-yml-file) features to share your exact setup with team members.

By embracing this modern approach to Ansible management on macOS, you'll find yourself with a more maintainable, flexible, and robust Ansible environment. Happy automating!

## Audio Summary

<TipCallout title="Please note">
The following audio summary of this blog post was generated by [NotebookLM](https://notebooklm.google).
</TipCallout>

<Audio mp3="/assets/2024-09-15-how-to-install-ansible-on-a-mac-a-modern-approach/2024-09-15-how-to-install-ansible-on-a-mac-a-modern-approach.mp3" />
