July 22, 2019

Install Python 3.7.4 in pyenv on Ubuntu 18.04

I was testing a Python application that was written in Python 3.3 and pyenv seemed like a good solution for having multiple versions of Python at the same time. I could have gone with official Debian based Python Docker image but I am running Ubuntu on Windows Linux Subsystem (WSL) and it wasn’t a big deal. In this runbook, I list the steps required to install Python 3.7.4 in pyenv, you can install any other version as you please.

Cunningham’s Law: “the best way to get the right answer on the internet is not to ask a question; it’s to post the wrong answer”.

Disclaimer: Please post a comment if I am wrong on anything

  1. Install build dependencies
sudo apt-get install \
    build-essential \
    libsqlite3-dev \
    sqlite3 \
    bzip2 \
    libbz2-dev \
    zlib1g-dev \
    libssl-dev \
    openssl \
    libgdbm-dev \
    libgdbm-compat-dev \
    liblzma-dev \
    libreadline-dev \
    libncursesw5-dev \
    libffi-dev \
  1. Clone pyenv from the source to the right path
git clone https://github.com/pyenv/pyenv.git ~/.pyenv
  1. Set related environment variables
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n  eval "$(pyenv init -)"\nfi' >> ~/.bashrc
  1. Reload your shell
exec $SHELL
  1. Install your Python version of choice
# list all the avaiable python versions
pyenv install --list

# install python 3.7.4
pyenv install 3.7.4

# set the global python version
pyenv global 3.7.4

# check what python versions are installed
pyenv versions

# verify the right version of python is active
python --version


© Mike Hosseini 2019