CentOS and Newer Versions of Python

Wednesday, September 2, 2020

When working on Python development locally on my Arch machine, I am generally either working on Python 3.7 or 3.8. This is usually not an issue since Lambda natively supports Python 3.7. However, on CentOS 7 and 8 the latest version of Python available is 3.6. In order to sanely get Python 3.7 on CentOS I wrote the following little playbook that uses Pyenv to install the version of Python I need on the servers.

Below is the basic yaml, which includes the build requirements needed for Pyenv:

  - name: Install base packages
    package:
      name: "{{ item }}"
      state: present
    loop:
      - epel-release
      - vim
      - tmux
      - htop
      - git
      - zlib-devel
      - bzip2
      - bzip2-devel
      - readline-devel
      - sqlite
      - sqlite-devel
      - openssl-devel
      - xz
      - xz-devel
      - libffi-devel
      - gcc

  - name: Install Pyenv
    shell: curl https://pyenv.run | bash
    args:
      creates: /home/centos/.pyenv
    become: yes
    become_user: centos

  - name: Put updated bashrc in place
    copy:
      src: bashrc
      dest: /home/centos/.bashrc
      owner: centos
      group: centos
      mode: '0644'

  - name: Install Python 3.7
    shell: source ~/.bashrc && pyenv install -v 3.7.7
    args:
      creates: /home/centos/.pyenv/shims/python
    become: yes
    become_user: centos

  - name: Set 3.7.7 as the global version for user
    shell: source ~/.bashrc && pyenv global 3.7.7
    become: yes
    become_user: centos

  - name: Install pipenv for user
    shell: source ~/.bashrc && pip install pipenv
    become: yes
    become_user: centos

In the example above I am using the centos user that is standard on AWS CentOS machines. You can also see I am putting an updated .bashrc in place. This updated .bashrc includes the following which is needed for Pyenv to fuction:

# User specific aliases and functions
export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

Nice and simple solution to getting other versions of Python installed without dealing with external repositories or hand-building packages.

linuxansiblepythoncentos

Cloudflared and Internal Services

Vagrant, Libvirt, and nftables