Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save markthomas93/f9cce98eaefea8e2fcbfd677bb225dbe to your computer and use it in GitHub Desktop.
Save markthomas93/f9cce98eaefea8e2fcbfd677bb225dbe to your computer and use it in GitHub Desktop.
Quick install pyenv and Python

Tested only on Ubuntu 18.04 and KDE Neon User Edition (based on Ubuntu 18.04).

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.8.2
pyenv install 3.8.2
# download and compile python 2.7.17
pyenv install 2.7.17
# set "python", "python3", "python3.8" to point python3.8.2, and "python2" and "python2.7" to point python2.7.17
pyenv global 3.8.2 2.7.17
# reload .bashrc to run pyenv configurations again
source $HOME/.bashrc
python3 -m pip install pip setuptools wheel virtualenv virtualenvwrapper -U
python2 -m pip install pip setuptools wheel virtualenv virtualenvwrapper -U

OSX

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

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
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 to ${HOME}/.zprofile, ${HOME}/.bash_profile, .bashrc or .zshrc

if command -v pyenv &>/dev/null; then
    ZLIB_DIR="/usr/local/opt/zlib"
    OPEN_SSL_DIR="/usr/local/opt/[email protected]"
    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.8.2
pyenv install 3.8.2
# download and compile python 2.7.17
pyenv install 2.7.17
# set "python", "python3", "python3.8" to point python3.8.2, and "python2" and "python2.7" to point python2.7.17
pyenv global 3.8.2 2.7.17

python3 -m pip install pip setuptools wheel virtualenv virtualenvwrapper -U
python2 -m pip install pip setuptools wheel virtualenv virtualenvwrapper -U

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))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment