Install Python 3.11 on Ubuntu 24.04 (Keep System 3.12) with pyenv

Need Python 3.11 on Ubuntu 24.04 (which ships 3.12)? Use pyenv to install 3.11 side-by-side without touching /usr/bin/python3.


At a Glance

  • Safe: Doesn’t replace system Python (3.12 stays for Ubuntu tools).
  • Flexible: Pick per-project versions with pyenv local.
  • Reversible: pyenv global system to go back.

Prerequisites

  • Ubuntu 24.04 (Noble)
  • A sudo-capable user
  • Default shell: Bash

Quick Start (Copy–Paste)

# 0) Check system Python (should be 3.12.x)
python3 --version

# 1) Install pyenv
curl -fsSL https://pyenv.run | bash

# 2) Shell init (Bash). Add pyenv to PATH & enable shims
cat >> ~/.bashrc <<'EOF'
export PYENV_ROOT="$HOME/.pyenv"
[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init - bash)"
EOF

# 3) Reload shell so pyenv shims are active
exec "$SHELL"

# 4) Build dependencies
sudo apt update; sudo apt install make build-essential libssl-dev zlib1g-dev \
libbz2-dev libreadline-dev libsqlite3-dev curl git \
libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev

# 5) Install & select Python 3.11
pyenv install 3.11.13
pyenv global 3.11.13

# 6) Verify
python --version
python3 --version
which python3   # should be ~/.pyenv/shims/python3

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top