Source: How to Install Pyenv on Ubuntu 22.04 – ericsysmin’s DevOps Blog
January 11, 2024
Eric Anderson
Due to the slowness of repositories or even lack thereof being updated with specific versions of Python, I’ve decided to move some of my environments over to Pyenv to allow me to dynamically install and configure Python specifically for my environment. As it turns out this will also allow VS Code to allow me to choose the version of Python that I’d like to use when testing. So, here’s a quick guide to installing Pyenv on Ubuntu 22.04
Steps to install Pyenv on Ubuntu 22.04
1. Update and Install Dependencies
We need to ensure our package cache is updated, and then install the dependencies to download, and build Python from Pyenv.
1
2
3
|
sudo apt–get update && sudo apt–get install make build–essential libssl–dev \
zlib1g–dev libbz2–dev libreadline–dev libsqlite3–dev wget curl llvm \
libncursesw5–dev xz–utils tk–dev libxml2–dev libxmlsec1–dev libffi–dev liblzma–dev
|
2. Install Pyenv using pyenv-installer
1
|
curl https://pyenv.run | bash
|
3. Configure user profile to use pyenv
Ensure the following is in your ~/.bash_profile (if exists), ~/.profile (for login shells), ~/.bashrc (for interactive shells), or ~/.zshrc
1
2
3
4
5
6
7
8
|
# Load pyenv automatically by appending
# the following to
# ~/.bash_profile if it exists, otherwise ~/.profile (for login shells)
# and ~/.bashrc (for interactive shells) :
export PYENV_ROOT=“$HOME/.pyenv”
[[ –d $PYENV_ROOT/bin ]] && export PATH=“$PYENV_ROOT/bin:$PATH”
eval “$(pyenv init -)”
|
Optionally enable pyenv-virtualenv
1
|
eval “$(pyenv virtualenv-init -)”
|
4. Reload your profile
1
|
source ~/.bashrc
|
5. Install python using pyenv
1
|
pyenv install <version>
|
6. Set your python version
pyenv shell <version> — select just for current shell session
pyenv local <version> — automatically select whenever you are in the current directory (or its subdirectories)
pyenv global <version> — select globally for your user account
7. Validate your installation of python
1
|
python3 —version
|
or
1
|
python —version
|