Install specific Python version with Ansible
Really quick tutorial of how to install a custom Python version from source code with a help of Ansible. The resulting playbook will help us to install python in automatic mode on any remote machine as well as on virtual machine within Vagrant. So the code of Ansible playbook first followed with a little bit of explanation
First we install packages that are required to compile/build Python properly, then download and extract source code from official ftp and finally build it from sources with a help of a
Optionally, you can add a check for current python version to save your time if running a playbook often enough
- name: Check which python version is installed
After that just wrap the task above in a
block section with
when condition or do the similar with
- name: Install specific version of Python
And yes, you can make it as a separate role or include directly into the
tasks section within your playbook. In case of using Vagrant you need to update your
Vagrantfile if haven’t done it yet.
config.vm.provision :ansible do |ansible|
Do not forget to specify required python version in your
vars.yml and you’ll be good to go. Next time invoking a playbook on a remote or running provisioning on your vm (
vagrant provision) you will get Python installed and ready to use.
$ sudo add-apt-repository ppa:deadsnakes/ppa
This PPA contains most recent Python versions for each minor release so it will not fit your needs in case you should have specific patch version on your system. (X.Y.Z - x - major, y - minor, z - patch).
Next option is to use pyenv and allow it to install required distribution for you
$ pyenv install 3.5.2
That’s it! Stick with preferred solution and don’t forget to reuse the approach writing ansible playbook for that.