Skip to content

Instantly share code, notes, and snippets.

@luzfcb
Last active October 29, 2024 18:59
Show Gist options
  • Save luzfcb/1a7f64adf5d12c2d357d0b4319fe9dcd to your computer and use it in GitHub Desktop.
Save luzfcb/1a7f64adf5d12c2d357d0b4319fe9dcd to your computer and use it in GitHub Desktop.
Quick install pyenv and Python

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

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 may 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 ! grep -Eq "^[#]{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 PYTHON_CFLAGS="-O2"\n  export PYTHON_BUILD_ARIA2_OPTS="-x 10 -k 1M"\n  export PYENV_ROOT="${HOME}/.pyenv"\n  export PATH="${PYENV_ROOT}/bin:${PATH}"\n  eval "$(pyenv init --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"

# install the python versions listed on python_versions variable
python_versions="3.13.0 3.12.7 3.11.10 3.10.15 3.9.20 3.8.20 3.7.17 3.6.15 2.7.18"

# I created this documentation for my own use, as I maintain certain 
# libraries that require test the backward compatibility. 
# Therefore, I decided to install every possible version I may need, 
# regardless of the time required to download, compile, and install them.
# I suggest installing only the latest stable version. You can install other 
# versions later, but remember to use `pyenv global <version1> <version2>` 
# to activate the installation of additional versions.
# https://github.com/pyenv/pyenv/blob/master/COMMANDS.md#pyenv-global-advanced
# simple use:
# python_versions="3.13.0"

for version in ${python_versions}; do \
    # download, compile and install python on the user home
    pyenv install --skip-existing "${version}";
done

# set "python", "python3", "python3.13" to point python3.13.0,
# "python3.9" to point python3.9.20 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 ${python_versions}

# reload .bashrc to run pyenv configurations again
source "${HOME}/.bashrc"

# upgrade some basic packages and install virtualenv virtualenvwrapper
# on all python versions
for version in ${python_versions}; do \
version="${version:0:4}"; \
python"${version%\.}" -m pip install pip setuptools wheel virtualenv virtualenvwrapper -U; \
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 or higher:

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 PYTHON_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.

# install the python versions listed on python_versions variable
python_versions="3.13.0 3.12.7 3.11.10 3.10.15 3.9.20 3.8.20 3.7.17 3.6.15 2.7.18"
for version in ${python_versions}; do \
    # download, compile and install python on the user home
    pyenv install --skip-existing "${version}";
done

# set "python", "python3", "python3.13" to point python3.13.0,
# "python3.9" to point python3.9.20 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 ${python_versions}


# upgrade some basic packages and install virtualenv virtualenvwrapper
# on all python versions
for version in ${python_versions}; do \
version="${version:0:4}"; \
python"${version%\.}" -m pip install pip setuptools wheel virtualenv virtualenvwrapper -U; \
done

Close the terminal and open again.

------------------------------------------------------------------------

Virtualenvwrapper quick tutorial

  • Create a virtualenv with python3.13 and named it as myenv and load the virtualenv
mkvirtualenv -p python3.13 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.13, 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.13 -a $(pwd) myenv2
  • Create a virtualenv with python3.13, 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.13 -a $(pwd) -i cookiecutter -i ipython myenv3
  • Create a virtualenv with python3.13, 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.13 -a $(pwd) -r requirements.txt -r requirements2.txt myenv4
  • Create a virtualenv with python3.13, 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.13 -a $(pwd) -r requirements.txt $(basename $(pwd))
@audreyfeldroy
Copy link

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

@VictorPinas
Copy link

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

@angeloCastanheira
Copy link

Grande Script.
Instalei no ubuntu 22.04 LTS e para já nada apontar.

Obrigado Fábio.

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