{"id":149135,"date":"2024-01-07T23:56:05","date_gmt":"2024-01-07T20:56:05","guid":{"rendered":"https:\/\/computingforgeeks.com\/?p=149135"},"modified":"2025-10-24T18:07:20","modified_gmt":"2025-10-24T15:07:20","slug":"install-python-3-1x-using-ansible-on-debian-ubuntu","status":"publish","type":"post","link":"https:\/\/computingforgeeks.com\/install-python-3-1x-using-ansible-on-debian-ubuntu\/","title":{"rendered":"Install Python 3.1x using Ansible on Ubuntu or Ubuntu"},"content":{"rendered":"\n<p>Python is an open source, and widely used programming language known to be easy to learn and with powerful capabilities. There exists hundreds of libraries and frameworks created by the open source community that makes Python dynamic for adoption in any environment and varying use cases. Some of the popular python frameworks are Flask, Django, TensorFlow, PyTorch, Pandas, SciPy, among many others.<\/p>\n\n\n\n<p>Overall, Python&#8217;s vast ecosystem make it a go-to programming language for a wide range of applications. You can use it as your favorite scripting language for Linux and other operating systems, and also to solve complex artificial intelligence and data analysis problems. To get the latest version of Python installed in your Debian or Ubuntu Linux, you&#8217;ll have to build from source. This article is created to automate the process of building and installing the latest release of Python from source using Ansible Playbook.<\/p>\n\n\n\n<p>An Ansible playbook contains a set of instructions and tasks in YAML file. It is used to define and execute automations on a local host or remote server machines. A Playbook can be defined as the heart of <a href=\"https:\/\/www.ansible.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">Ansible configuration management <\/a>since its execution results in the desired state of your system.<\/p>\n\n\n<p>[ebook product=&#8221;2&#8243; theme=&#8221;purple&#8221;]<\/p>\n\n\n\n<p>Before you can automate Python 3.x installation you&#8217;ll need Ansible installed. We have a number of articles that you can follow along to install Ansible.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-pale-pink-color\">ansible --version<\/mark>\nansible &#91;core 2.16.0]\n  config file = None\n  configured module search path = &#91;'\/Users\/jkmutai\/.ansible\/plugins\/modules', '\/usr\/share\/ansible\/plugins\/modules']\n  ansible python module location = \/usr\/local\/Cellar\/ansible\/9.0.1\/libexec\/lib\/python3.12\/site-packages\/ansible\n  ansible collection location = \/Users\/jkmutai\/.ansible\/collections:\/usr\/share\/ansible\/collections\n  executable location = \/usr\/local\/bin\/ansible\n  python version = 3.12.0 (main, Oct  2 2023, 12:03:24) &#91;Clang 15.0.0 (clang-1500.0.40.1)] (\/usr\/local\/Cellar\/ansible\/9.0.1\/libexec\/bin\/python)\n  jinja version = 3.1.2\n  libyaml = True<\/code><\/pre>\n\n\n\n<p>Create inventory file with the IP address of Ubuntu or Debian server where Python is to be installed.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-pale-pink-color\">vim hosts<\/mark>\n49.13.163.29<\/code><\/pre>\n\n\n\n<p>Create a Playbook for installing Python 3.x<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>vim build_install_python.yml<\/code><\/pre>\n\n\n\n<p>Here are the playbook contents. Parameters to set are host IP, remote user, and Python release number.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>---\n- name: Install Python 3.x on Debian \/ Ubuntu\n  hosts: <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">49.13.163.29<\/mark>\n  remote_user: <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">root<\/mark>\n  become: yes\n  become_method: sudo\n  vars:\n    python_release: <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">3.12.1<\/mark> # See https:\/\/www.python.org\/downloads\/source\/\n    python_src_dir: \/tmp\n    python_archive: Python-{{ python_release }}.tgz\n    python_url: \"https:\/\/www.python.org\/ftp\/python\/{{ python_release }}\/{{ python_archive }}\"\n    packages:\n      - git\n      - gcc\n      - make\n      - tar\n      - vim\n      - wget\n      - libncursesw5-dev\n      - libssl-dev\n      - libsqlite3-dev\n      - tk-dev\n      - libgdbm-dev\n      - libc6-dev\n      - libbz2-dev\n      - libffi-dev\n      - zlib1g-dev\n      - build-essential\n      - liblzma-dev\n      - tcl-dev\n  tasks:\n    - name: Upgrade OS packages\n      ansible.builtin.apt:\n        name: \"*\"\n        state: latest\n        update_cache: yes\n\n    - name: Check if a reboot is required\n      ansible.builtin.stat:\n        path: \/var\/run\/reboot-required\n        get_checksum: no\n      register: reboot_required_file\n    \n    - name: Reboot the server (if required).\n      ansible.builtin.reboot:\n        reboot_timeout: 900\n      when: reboot_required_file.stat.exists == true\n\n    - name: Install dependency packages\n      ansible.builtin.apt:\n        name: \"{{ item }}\"\n        state: latest\n      loop: \"{{ packages }}\"\n\n    - name: Download Python source archive\n      ansible.builtin.get_url:\n        dest: \"{{ python_src_dir }}\"\n        url: \"{{ python_url }}\"\n\n    - name: Extract archive\n      ansible.builtin.unarchive:\n        src: \"{{ python_src_dir }}\/{{ python_archive }}\"\n        dest: \"{{ python_src_dir }}\"\n        remote_src: true\n\n    - name: Run .\/configure\n      ansible.builtin.command:\n        cmd: .\/configure --enable-optimizations --prefix=\/usr\/local --enable-shared LDFLAGS=\"-Wl,-rpath \/usr\/local\/lib\"\n        chdir: \"{{ python_src_dir }}\/Python-{{ python_release }}\"\n        creates: \"{{ python_src_dir }}\/Python-{{ python_release }}\/config.log\"\n\n    - name: Run make to build python\n      community.general.make:\n        chdir: \"{{ python_src_dir }}\/Python-{{ python_release }}\"\n      register: make_result\n\n    - name: Run make install\n      community.general.make:\n        chdir: \"{{ python_src_dir }}\/Python-{{ python_release }}\"\n        target: install\n      register: install_result\n      when: make_result is not skipped<\/code><\/pre>\n\n\n\n<p>Default Python build configuration commands executed by playbook are as shown below. You can adjust them to suit your use case.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.\/configure --enable-optimizations --prefix=\/usr\/local --enable-shared LDFLAGS=\"-Wl,-rpath \/usr\/local\/lib\"<\/code><\/pre>\n\n\n\n<p>Copy SSH key to the remote server if you are doing password authentication.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ssh-copy-id remote_user@ServerIP<\/code><\/pre>\n\n\n\n<p>Next execute the playbook.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><em># Using user credentials defined in playbook file.\n<\/em>ansible-playbook -i hosts  build_install_python.yml\n\n# Using remote user called ubuntu with become password(sudo password)\n ansible-playbook -i hosts build_install_python.yml --become --become-user=<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-pale-cyan-blue-color\">ubuntu<\/mark> --ask-become-pass<\/code><\/pre>\n\n\n\n<p>Sample execution output.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-pale-pink-color\"> ansible-playbook -i hosts  build_install_python.yml<\/mark>\nPLAY &#91;Install Python 3.x on Debian \/ Ubuntu] *********************************************************************************************************************************************************\n\nTASK &#91;Gathering Facts] *******************************************************************************************************************************************************************************\nok: &#91;49.13.163.29]\n\nTASK &#91;Upgrade OS packages] ***************************************************************************************************************************************************************************\nchanged: &#91;49.13.163.29]\n\nTASK &#91;Check if a reboot is required] *****************************************************************************************************************************************************************\nok: &#91;49.13.163.29]\n\nTASK &#91;Reboot the server (if required).] **************************************************************************************************************************************************************\nchanged: &#91;49.13.163.29]\n\nTASK &#91;Install dependency packages] *******************************************************************************************************************************************************************\nok: &#91;49.13.163.29] =&gt; (item=git)\nchanged: &#91;49.13.163.29] =&gt; (item=gcc)\nchanged: &#91;49.13.163.29] =&gt; (item=make)\nok: &#91;49.13.163.29] =&gt; (item=tar)\nok: &#91;49.13.163.29] =&gt; (item=vim)\nok: &#91;49.13.163.29] =&gt; (item=wget)\nchanged: &#91;49.13.163.29] =&gt; (item=libncursesw5-dev)\nchanged: &#91;49.13.163.29] =&gt; (item=libssl-dev)\nchanged: &#91;49.13.163.29] =&gt; (item=libsqlite3-dev)\nchanged: &#91;49.13.163.29] =&gt; (item=tk-dev)\nchanged: &#91;49.13.163.29] =&gt; (item=libgdbm-dev)\nok: &#91;49.13.163.29] =&gt; (item=libc6-dev)\nchanged: &#91;49.13.163.29] =&gt; (item=libbz2-dev)\nchanged: &#91;49.13.163.29] =&gt; (item=libffi-dev)\nok: &#91;49.13.163.29] =&gt; (item=zlib1g-dev)\nok: &#91;49.13.163.29] =&gt; (item=build-essential)\nchanged: &#91;49.13.163.29] =&gt; (item=liblzma-dev)\nok: &#91;49.13.163.29] =&gt; (item=tcl-dev)\n\nTASK &#91;Download Python source archive] ****************************************************************************************************************************************************************\nchanged: &#91;49.13.163.29]\n\nTASK &#91;Extract archive] *******************************************************************************************************************************************************************************\nchanged: &#91;49.13.163.29]\n\nTASK &#91;Run .\/configure] *******************************************************************************************************************************************************************************\nchanged: &#91;49.13.163.29]\n\nTASK &#91;Run make to build python] **********************************************************************************************************************************************************************\nchanged: &#91;49.13.163.29]\n\nTASK &#91;Run make install] ******************************************************************************************************************************************************************************\nchanged: &#91;49.13.163.29]\n\nPLAY RECAP *******************************************************************************************************************************************************************************************\n49.13.163.29               : ok=10   changed=8    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0<\/code><\/pre>\n\n\n\n<p>After the playbook execution, login to the server and confirm Python installation. See examples below.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><em># Python 3.12\n<\/em>$ <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-pale-pink-color\">python3.12 -V<\/mark>\nPython 3.12.1\n\n<em># Python 3.11\n<\/em>$ <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-pale-pink-color\">python3.11 -V<\/mark>\nPython 3.11.7\n\n<em># Python 3.10\n<\/em>$ <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-pale-pink-color\">python3.10 -V<\/mark>\nPython 3.10.13<\/code><\/pre>\n\n\n\n<p>Pip matching Python version is also installed.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-pale-pink-color\">pip3.12 -V<\/mark>\npip 23.2.1 from \/usr\/local\/lib\/python3.12\/site-packages\/pip (python 3.12)<\/code><\/pre>\n\n\n\n<p>Check the absolute path of both Python and Pip installed<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-pale-pink-color\">which python3.12<\/mark>\n\/usr\/local\/bin\/python3.12\n\n$ <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-pale-pink-color\">which pip3.12<\/mark>\n\/usr\/local\/bin\/pip3.12<\/code><\/pre>\n\n\n\n<p>You can run it using absolute bin path or just calling python<em>&lt;version&gt;<\/em> command.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-pale-pink-color\">\/usr\/local\/bin\/python3.12<\/mark>\nPython 3.12.1 (main, Jan  7 2024, 19:29:29) &#91;GCC 11.4.0] on linux\nType \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n&gt;&gt;&gt;\n\n<em>#OR\n<\/em>$ <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-luminous-vivid-amber-color\">python3.12<\/mark>\nPython 3.12.1 (main, Jan  7 2024, 19:29:29) &#91;GCC 11.4.0] on linux\nType \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n&gt;&gt;&gt;<\/code><\/pre>\n\n\n\n<p>Congratulations, you\u2019ve just installed the latest release of Python by building it from source code and with the power of Ansible automation. We hope this article was helpful. You can check other Ansible guides available in our website.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/computingforgeeks.com\/create-lvm-logical-volume-and-mount-using-ansible\/\" target=\"_blank\" rel=\"noreferrer noopener\">Create LVM Logical Volume and Mount using Ansible<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/computingforgeeks.com\/how-to-deploy-matrix-server-using-ansible-and-docker\/\" target=\"_blank\" rel=\"noreferrer noopener\">How To Deploy Matrix Server using Ansible and Docker<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/computingforgeeks.com\/automate-linux-systems-with-ansible-system-roles\/\" target=\"_blank\" rel=\"noreferrer noopener\">Automate Linux Systems with Ansible System Roles<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Python is an open source, and widely used programming language known to be easy to learn and with powerful capabilities. There exists hundreds of libraries and frameworks created by the open source community that makes Python dynamic for adoption in any environment and varying use cases. Some of the popular python frameworks are Flask, Django, &#8230; <a title=\"Install Python 3.1x using Ansible on Ubuntu or Ubuntu\" class=\"read-more\" href=\"https:\/\/computingforgeeks.com\/install-python-3-1x-using-ansible-on-debian-ubuntu\/\" aria-label=\"Read more about Install Python 3.1x using Ansible on Ubuntu or Ubuntu\">Read more<\/a><\/p>\n","protected":false},"author":3,"featured_media":149841,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[606,329,299,832,81],"tags":[38932],"class_list":["post-149135","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ansible","category-automation","category-how-to","category-tech","category-ubuntu","tag-python-3-1x-using-ansible"],"_links":{"self":[{"href":"https:\/\/computingforgeeks.com\/wp-json\/wp\/v2\/posts\/149135","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/computingforgeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/computingforgeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/computingforgeeks.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/computingforgeeks.com\/wp-json\/wp\/v2\/comments?post=149135"}],"version-history":[{"count":1,"href":"https:\/\/computingforgeeks.com\/wp-json\/wp\/v2\/posts\/149135\/revisions"}],"predecessor-version":[{"id":160768,"href":"https:\/\/computingforgeeks.com\/wp-json\/wp\/v2\/posts\/149135\/revisions\/160768"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/computingforgeeks.com\/wp-json\/wp\/v2\/media\/149841"}],"wp:attachment":[{"href":"https:\/\/computingforgeeks.com\/wp-json\/wp\/v2\/media?parent=149135"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/computingforgeeks.com\/wp-json\/wp\/v2\/categories?post=149135"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/computingforgeeks.com\/wp-json\/wp\/v2\/tags?post=149135"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}