Notes

Python - Install pyenv on Ubuntu and Fedora

author: Paul Kim

categories: pyenv

tags: pyenv

On Ubuntu and Fedora, Python 3 is already installed by default. Do NOT delete Python 3 on Ubuntu and Fedora. The GNOME desktop environment depends on Python 3. Deleting Python 3 will break Ubuntu and Fedora desktop GUI.

Ubuntu pre-install

# update packages
sudo apt update
sudo apt upgrade
sudo apt autoremove

# install python3
sudo apt install python3 python3-pip python3-venv python3-wheel

# uninstall python2
sudo apt remove python python-pip
sudo apt autoremove

# install pyenv dependencies
sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev \
libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev \
xz-utils tk-dev libffi-dev

Fedora pre-install

# update packages
sudo dnf update
sudo dnf upgrade
sudo dnf autoremove

# install python3
sudo dnf install python3 python3-pip python3-wheel

# uninstall python2
sudo dnf remove python2 python2-pip
sudo dnf autoremove

# install pyenv dependencies
sudo dnf install zlib-devel bzip2 bzip2-devel readline-devel sqlite sqlite-devel \
openssl-devel xz xz-devel libffi-devel

Install pyenv and pyenv-virtualenv plugin on Ubuntu and Fedora

Install pyenv using pyenv-installer

curl https://pyenv.run | bash

Install pyenv using basic GitHub checkout

# checkout pyenv to ~/.pyenv
git clone https://github.com/pyenv/pyenv.git ~/.pyenv

# define PYENV_ROOT environment variable and add $PYENV_ROOT/bin to $PATH
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc

# add pyenv init to your shell
echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n  eval "$(pyenv init -)"\nfi' >> ~/.bashrc

# restart your shell
exec "$SHELL"

Install pyenv-virtualenv

# check out pyenv-virtualenv into plugin directory
git clone https://github.com/pyenv/pyenv-virtualenv.git $(pyenv root)/plugins/pyenv-virtualenv

# enable auto-activation of virtualenvs (optional)
echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc

# restart your shell
exec "$SHELL"

Basically, your .bashrc should look like this:

export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
if command -v pyenv 1>/dev/null 2>&1; then
  eval "$(pyenv init -)"
  eval "$(pyenv virtualenv-init -)"
fi

Upgrade pyenv on Ubuntu and Fedora

Upgrade pyenv using pyenv-installer

pyenv update

Upgrade pyenv using basic GitHub checkout

# upgrade your installation at any time using git
cd $(pyenv root)
git pull

# upgrade to a specific release of pyenv
# by checking out the corresponding tag
cd $(pyenv root)
git fetch
git tag
# v0.1.0
git checkout v0.1.0

Uninstall pyenv on Ubuntu and Fedora

  1. disable pyenv by removing pyenv init from .bashrc:
  2. completely uninstall pyenv by removing its root directory.

Note: this will delete all Python versions that were installed under $(pyenv root)/versions/ directory.

# comment out or remove these lines to disable pyenv
if command -v pyenv 1>/dev/null 2>&1; then
  eval "$(pyenv init -)"
  eval "$(pyenv virtualenv-init -)"
fi

# remove pyenv root directory to uninstall pyenv
rm -rf $(pyenv root)

ChromeEdgeFirefoxOpera

© 2021 paulkode.com. All rights reserved.