Ubuntu 25.04 安装 pyenv 并安装多个 python 版本

目录

1、安装 pyenv

1.1、安装命令

1.2、按照脚本的提示,将以下内容添加到 ~/.bashrc 文件:

1.3、重新加载 shell 配置:

1.4、安装完pyenv后,不能直接进行下面的操作,pyenv 本质是从python官网下载然后替你进行编译,本地需要一些编译工具以及依赖库,下面进行安装

1.5、tips 如果没有之前的操作,导致编译失败,可能会出现下面的日志:

1.5.1、清理之前的失败安装

2、使用 pyenv 安装 Python 版本

验证安装

设置当前版本


1、安装 pyenv

1.1、安装命令
curl https://pyenv.run | bash
1.2、按照脚本的提示,将以下内容添加到 ~/.bashrc 文件:
# 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 - bash)"
# Restart your shell for the changes to take effect.
# Load pyenv-virtualenv automatically by adding
# the following to ~/.bashrc:
eval "$(pyenv virtualenv-init -)"

逐行解释

  1. export PYENV_ROOT="$HOME/.pyenv"设置环境变量 PYENV_ROOT,定义 pyenv 的安装目录(默认为 ~/.pyenv)。
  2. [[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"检查 $PYENV_ROOT/bin 目录是否存在,如果存在,则将其添加到 PATH 环境变量开头。目的是让 Shell 优先从 pyenv 的目录中查找命令(如 pyenv 可执行文件)。
  3. eval "$(pyenv init - bash)"初始化 pyenv,并配置 Shell 的自动补全和路径管理功能。- bash 表示针对 Bash Shell 的配置。
  4. eval "$(pyenv virtualenv-init -)"启用 pyenv-virtualenv 插件(用于管理虚拟环境)。自动激活或退出虚拟环境(当进入/离开包含 .python-version 文件的目录时)。
1.3、重新加载 shell 配置:
source ~/.bashrc
1.4、安装完pyenv后,不能直接进行下面的操作,pyenv 本质是从python官网下载然后替你进行编译,本地需要一些编译工具以及依赖库,下面进行安装
sudo apt update
sudo apt install -y \
build-essential \
zlib1g-dev \
libbz2-dev \
libreadline-dev \
libsqlite3-dev \
libncurses-dev \
libssl-dev \
libffi-dev \
liblzma-dev \
tk-dev \
python3-tk \
wget \
curl \
llvm

关键依赖说明

  • libbz2-dev:解决 _bz2 模块缺失问题。

  • libssl-dev:解决 _ssl 模块缺失问题(OpenSSL)。

  • libffi-dev:解决 _ctypes 模块缺失问题。

  • libreadline-dev:解决 readline 模块缺失问题。

  • libncurses-dev:解决 _curses 模块缺失问题。

1.5、tips 如果没有之前的操作,导致编译失败,可能会出现下面的日志:

elenda@elenda-ThinkBook-16-G7-IAH:~$ pyenv install 3.12 Downloading Python-3.12.11.tar.xz... -> https://www.python.org/ftp/python/3.12.11/Python-3.12.11.tar.xz Installing Python-3.12.11... Traceback (most recent call last): File "<string>", line 1, in <module> File "/home/elenda/.pyenv/versions/3.12.11/lib/python3.12/bz2.py", line 17, in <module> from _bz2 import BZ2Compressor, BZ2Decompressor ModuleNotFoundError: No module named '_bz2' WARNING: The Python bz2 extension was not compiled. Missing the bzip2 lib? Traceback (most recent call last): File "<string>", line 1, in <module> File "/home/elenda/.pyenv/versions/3.12.11/lib/python3.12/curses/__init__.py", line 13, in <module> from _curses import * ModuleNotFoundError: No module named '_curses' WARNING: The Python curses extension was not compiled. Missing the ncurses lib? Traceback (most recent call last): File "<string>", line 1, in <module> File "/home/elenda/.pyenv/versions/3.12.11/lib/python3.12/ctypes/__init__.py", line 8, in <module> from _ctypes import Union, Structure, Array ModuleNotFoundError: No module named '_ctypes' WARNING: The Python ctypes extension was not compiled. Missing the libffi lib? Traceback (most recent call last): File "<string>", line 1, in <module> ModuleNotFoundError: No module named 'readline' WARNING: The Python readline extension was not compiled. Missing the GNU readline lib? Traceback (most recent call last): File "<string>", line 1, in <module> File "/home/elenda/.pyenv/versions/3.12.11/lib/python3.12/ssl.py", line 100, in <module> import _ssl # if we can't import it, let the error propagate ^^^^^^^^^^^ ModuleNotFoundError: No module named '_ssl' ERROR: The Python ssl extension was not compiled. Missing the OpenSSL lib? Please consult to the Wiki page to fix the problem. https://github.com/pyenv/pyenv/wiki/Common-build-problems BUILD FAILED (Ubuntu 25.04 using python-build 2.6.5) Inspect or clean up the working tree at /tmp/python-build.20250807135721.14689 Results logged to /tmp/python-build.20250807135721.14689.log Last 10 log lines: LD_LIBRARY_PATH=/tmp/python-build.20250807135721.14689/Python-3.12.11 ./python -E -m ensurepip \ $ensurepip --root=/ ; \ fi WARNING: Disabling truststore since ssl support is missing Looking in links: /tmp/tmpx2oh0blj Processing /tmp/tmpx2oh0blj/pip-25.0.1-py3-none-any.whl Installing collected packages: pip WARNING: The scripts pip3 and pip3.12 are installed in '/home/elenda/.pyenv/versions/3.12.11/bin' which is not on PATH. Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location. Successfully installed pip-25.0.1

这个错误表明在通过 pyenv 安装 Python 3.12.11 时,编译过程中缺少多个关键的依赖库,导致部分 Python 内置模块(如 _bz2_ssl_ctypes 等)无法编译。

1.5.1、清理之前的失败安装

删除之前编译失败的 Python 3.12.11 残留文件:

rm -rf /tmp/python-build.*  # 清理临时编译目录
pyenv uninstall 3.12.11     # 如果已部分安装,先卸载

2、使用 pyenv 安装 Python 版本

pyenv install  # pyenv install 3.12
验证安装
pyenv versions

会输出下面的内容

elenda@elenda-ThinkBook-16-G7-IAH:~$ pyenv versions
  system
* 3.12.11 (set by /home/elenda/.pyenv/version)

前面带星号表示当前设置的python版本,这个我已经设置过了

设置当前版本
pyenv global 3.12.11

posted @ 2025-08-10 08:37  wzzkaifa  阅读(234)  评论(0)    收藏  举报