Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Quick install pyenv and Python

Tested only on Ubuntu 20.04, KDE Neon User Edition (based on Ubuntu 20.04) and OSX Mojave.

will probably work on other newer versions, with no changes, or with few changes in non-python dependencies (apt-get packages)

NOTE: Don't create a .sh file and run it all at once. It will not work. Copy, paste, and execute each command below manually. :-)

Ubuntu

# DO NOT RUN THIS AS A ROOT USER
# Enter your password when prompted.
# your user must be allowed to run "sudo"
sudo bash -c "echo -e 'Starting...\n'"
sudo apt-get update;
# install most common python interpreter itself compile dependencies
sudo apt-get install aria2 build-essential curl git libbz2-dev libffi-dev liblzma-dev libncurses5-dev libncursesw5-dev libreadline-dev libsqlite3-dev libssl-dev llvm make tk-dev wget xz-utils zlib1g-dev --yes;
# install pyenv
curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash
# install a virtualenvwrapper plugin to pyenv
git clone https://github.com/pyenv/pyenv-virtualenvwrapper.git $HOME/.pyenv/plugins/pyenv-virtualenvwrapper
# add pyenv required configurations on your .bashrc file
if ! egrep -q "^[#]{4}[[:space:]]pyenv[[:space:]]config$" "${HOME}/.bashrc" ; then echo -e "\n\nsetup pyenv configuration:\nThe following content was inserted at the end of the ${HOME}/.bashrc file\n"; echo -e '\n#### pyenv config\nif [ -f "$HOME/.pyenv/bin/pyenv" ] && ! type -P pyenv &>/dev/null ; then\n  export PYTHON_CONFIGURE_OPTS="--enable-shared"\n  export CFLAGS="-O2"\n  export PYTHON_BUILD_ARIA2_OPTS="-x 10 -k 1M"\n  export PATH="$HOME/.pyenv/bin:$PATH"\n  eval "$(pyenv init -)"\n  eval "$(pyenv virtualenv-init -)"\n  if [[ ! "$(pyenv which python)" == "/usr/bin/python" ]] ; then \n    pyenv virtualenvwrapper_lazy;\n  fi\nfi\n#### pyenv config end' | tee --append "${HOME}/.bashrc"; source "$HOME/.bashrc"; else  echo -e "\n\npyenv configuration already installed in $HOME/.bashrc"; fi
# reload .bashrc to run pyenv configurations
source $HOME/.bashrc
# download and compile python 3.9.4
pyenv install 3.9.4
# download and compile python 3.8.9
pyenv install 3.8.9
# download and compile python 3.7.10
pyenv install 3.7.10
# download and compile python 3.6.13
pyenv install 3.6.13
# download and compile python 2.7.18
pyenv install 2.7.18
# set "python", "python3", "python3.9" to point python3.9.0,
# "python3.8" to point python3.8.9 and "python2" and "python2.7" to point python2.7.18 and etc
# It is useful to have all pythons available globally because it makes it easier for example to use tox to test code against various versions of python
pyenv global 3.9.4 3.8.9 3.7.10 3.6.13 2.7.18
# reload .bashrc to run pyenv configurations again
source $HOME/.bashrc
for v in "3.9" "3.8" "3.7" "3.6" "2.7"; do \ 
python${v} -m pip install pip setuptools wheel virtualenv virtualenvwrapper -U; pyenv rehash; \
done

After install use the virtualenvwrapper to create virtualenvs.

OSX

Install the Xcode Command Line Tools

xcode-select --install

Install brew (https://brew.sh/)

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

Install the pyenv, pyenv-virtualenvwrapper and the most common python interpreter itself compile dependencies for OSX

brew install pyenv pyenv-virtualenvwrapper aria2 openssl readline sqlite3 xz zlib

On OSX Mojave:

touch ${HOME}/.bash_profile

On OSX Catalina:

touch ${HOME}/.zprofile

Add pyenv load scripts on your shell

Add to ${HOME}/.zprofile, ${HOME}/.bash_profile, .bashrc or .zshrc

Note: In OSX Catalina and later versions, the default shell is zsh and not bash anymore, so the configuration file is ${HOME}/.zprofile

if command -v pyenv &>/dev/null; then
    ZLIB_DIR="/usr/local/opt/zlib"
    OPEN_SSL_DIR="/usr/local/opt/openssl@1.1"
    READLINE_DIR="/usr/local/opt/readline"
    SQLITE3_DIR="/usr/local/opt/sqlite"
    export PATH="${OPEN_SSL_DIR}/bin:${SQLITE3_DIR}/bin:$PATH"
    export LDFLAGS="-L${OPEN_SSL_DIR}/lib -L${READLINE_DIR}/lib -L${SQLITE3_DIR}/lib -L${ZLIB_DIR}/lib ${LDFLAGS}"
    export CPPFLAGS="-I${OPEN_SSL_DIR}/include -I${READLINE_DIR}/include -I${SQLITE3_DIR}/include -I${ZLIB_DIR}/include ${CPPFLAGS}"
    export PKG_CONFIG_PATH="${OPEN_SSL_DIR}/lib/pkgconfig:${READLINE_DIR}/lib/pkgconfig:${SQLITE3_DIR}/lib/pkgconfig:${ZLIB_DIR}/lib/pkgconfig:${PKG_CONFIG_PATH}"
    export PYTHON_CONFIGURE_OPTS="--enable-shared"
    export CFLAGS="-O2"
    # Makes pyenv use aria2c to manage and accelerate the download of the python source code.
    # Useful to avoid problems on slower or unstable internet connection
    export PYTHON_BUILD_ARIA2_OPTS="-x 10 -k 1M"
    # load pyenv
    eval "$(pyenv init -)"
    if command -v pyenv-virtualenvwrapper &>/dev/null && echo $(pyenv which python) | grep -qv "/usr/bin"; then
        pyenv virtualenvwrapper
    fi
fi

Close the terminal and open again.

# download and compile python 3.9.4
pyenv install 3.9.4
# download and compile python 3.8.9
pyenv install 3.8.9
# download and compile python 3.7.10
pyenv install 3.7.10
# download and compile python 3.6.13
pyenv install 3.6.13
# download and compile python 2.7.18
pyenv install 2.7.18
# set "python", "python3", "python3.9" to point python3.9.4,
# "python3.8" to point python3.8.9 and "python2" and "python2.7" to point python2.7.18, and etc
# It is useful to have all pythons available globally because it makes it easier for example to use tox to test code against various versions of python
pyenv global 3.9.4 3.8.9 3.7.10 3.6.13 2.7.18

for v in "3.9" "3.8" "3.7" "3.6" "2.7"; do \ 
python${v} -m pip install pip setuptools wheel virtualenv virtualenvwrapper -U; pyenv rehash; \
done

Close the terminal and open again.

Virtualenvwrapper quick tutorial

  • Create a virtualenv with python3.8 and named it as myenv and load the virtualenv
mkvirtualenv -p python3.8 myenv
  • Unload (deactivate) the virtualenv
deactivate
  • Load the virtualenv
workon myenv

TIP: type workon and press tab two times to autocomplete

  • Delete the virtualenv

it is necessary to deactivate virtualenv first

rmvirtualenv myenv
  • Create a virtualenv with python3.8, named it as myenv2 and mark the current directory as the virtualenv linked Project Directory to automatically go to the Project Directory when activating it.
mkvirtualenv -p python3.8 -a $(pwd) myenv2
  • Create a virtualenv with python3.8, named it as myenv3, mark the current directory as the virtualenv linked Project Directory to automatically go to the Project Directory when activating it and install two python libraries
mkvirtualenv -p python3.8 -a $(pwd) -i cookiecutter -i ipython myenv3
  • Create a virtualenv with python3.8, named it as myenv4, mark the current directory as the virtualenv linked Project Directory to automatically go to the Project Directory when activating it and python libraries from the requirements files.
echo "cookiecutter>=1.7" > requirements.txt
echo "ipython" > requirements2.txt
mkvirtualenv -p python3.8 -a $(pwd) -r requirements.txt -r requirements2.txt myenv4
  • Create a virtualenv with python3.8, named it as the current directory, mark the current directory as the virtualenv linked Project Directory to automatically go to the Project Directory when activating it and python libraries from the requirements files.
mkvirtualenv -p python3.8 -a $(pwd) -r requirements.txt $(basename $(pwd))
@audreyfeldroy

This comment has been minimized.

Copy link

@audreyfeldroy audreyfeldroy commented Feb 27, 2020

Cool! This inspires me to get my old Ubuntu laptop working again with a modern dev environment. Thanks for writing this up @luzfcb!

@VictorPinas

This comment has been minimized.

Copy link

@VictorPinas VictorPinas commented Aug 16, 2020

Muito obrigado por este tutorial Fábio! Me ajudou bastante. :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment