Using “pip” is the supported installation method for Ansible and Molecule. In Ubuntu, just using “pip” will lead to the error “externally-managed-environment” because of the Python environment that is “externally managed” by the package manager (this prevents direct use of pip for system-wide installations to avoid conflicts or issues).
(This post is similar to the one about Arch Linux.)
Instead, let’s install Python libraries and applications (in this case, Ansible and Molecule) in a Python virtual environment.
First, install the required packages, including the Python virtual environment package:
1 |
sudo apt install -y python3-pip python3-venv libssl-dev |
Create a virtual environment somewhere (in this example, I create it in my home folder as a subdirectory of a folder for all the virtual environments; the directory will be created automatically):
1 |
python3 -m venv ~/.venv/molecule |
Once the virtual environment has been created, “enter” the virtual environment:
1 |
source ~/.venv/molecule/bin/activate |
Install the Python packages for Ansible, Molecule, and its plugins in the virtual environment:
1 |
python3 -m pip install ansible molecule "molecule-plugins[docker]" yamllint ansible-lint |
You can verify that everything is installed correctly, e.g., at the time of writing:
1 2 3 4 5 6 7 8 9 10 11 12 |
❯ molecule --version molecule 25.4.0 using python 3.13 ansible:2.18.5 gce:23.7.0 from molecule_plugins requiring collections: google.cloud>=1.0.2 community.crypto>=1.8.0 openstack:23.7.0 from molecule_plugins requiring collections: openstack.cloud>=2.1.0 containers:23.7.0 from molecule_plugins requiring collections: ansible.posix>=1.3.0 community.docker>=1.9.1 containers.podman>=1.8.1 docker:23.7.0 from molecule_plugins requiring collections: community.docker>=3.10.2 ansible.posix>=1.4.0 default:25.4.0 from molecule azure:23.7.0 from molecule_plugins ec2:23.7.0 from molecule_plugins vagrant:23.7.0 from molecule_plugins podman:23.7.0 from molecule_plugins requiring collections: containers.podman>=1.7.0 ansible.posix>=1.3.0 |
Each time you want to run Ansible or Molecule, just run the “source” command above:
1 |
source ~/.venv/molecule/bin/activate |
And then you can run “ansible” and “molecule”.
Pingback: Installing Ansible and Molecule in Arch Linux | Lorenzo Bettini