如何在 Ubuntu 26.04 上从源码安装 Python 3.14
How to Install Python3.14 from source on Ubuntu 26.04

原始链接: https://jamesoclaire.com/2026/08/06/how-to-install-python3-14-from-source-on-ubuntu-26-04/

从源码安装 Python 是管理环境的高效方式,在 Ubuntu 26.04 上这一过程已变得日益简便。 首先,请确保已安装 C 编译器(`build-essential`)。你必须在 `/etc/apt/sources.list.d/ubuntu.sources` 中启用 `deb-src`,以便通过 `sudo apt build-dep python3` 获取必要的构建依赖项。同时建议安装一系列完整的支持库(如 `libssl-dev`、`zlib1g-dev` 和 `libreadline-dev`),以确保功能的完整性。 准备工作完成后,从官网下载最新的 Python 源码压缩包,解压并将该目录移动到 `/opt/`。进入文件夹内,运行 `./configure --enable-optimizations`,随后执行 `make`。 关键点在于,请使用 `sudo make altinstall` 而非 `make install`,以避免覆盖系统默认的 Python 二进制文件。最后,运行 `sudo ldconfig` 并通过调用特定版本的 Python(例如 `python3.14`)来验证安装。此过程通常耗时约十分钟,能够为你提供一个干净且专业的 Python 环境。

这篇 Hacker News 帖子讨论了在 Ubuntu 26.04 上安装最新版 Python(3.14)的方法。 发帖人分享了他们从源代码编译 Python 的个人工作流程。尽管他们承认 Ubuntu 26.04 为此过程增加了一个额外步骤,但他们更倾向于这种方法,因为它能确保安装环境和具体配置完全透明。 对此,另一位用户建议使用“Deadsnakes PPA”——这是一个广受欢迎的个人软件包存档,允许用户直接通过 `apt` 安装较新版本的 Python。发帖人认同 Deadsnakes 方法更快、更简便,且具有自动更新的优势,对于大多数用户而言是更实用的选择,尽管他们个人仍偏好手动编译。
相关文章

原文

I’ve always been a big fan of installing Python from source. It keeps you close to your environments and understanding exactly where the code comes from and goes to.

I feel like in the past it was more difficult, but year after year it seems like it’s getting easier. This year for Ubuntu 26.04 there is a bit of a new step added, but ultimately it is feeling effortless. The whole process takes about 10 or so minutes, and is good for people of all levels to understand.

Prep for on a fresh install of Ubuntu 26.04

You may need a C compiler if you haven’t yet downloaded:

sudo apt update && sudo apt -y install build-essential

New Ubuntu sources need to add ‘deb-src’

There is a new way to get the build-deps for Python on Ubuntu, so you’ll need to edit /etc/apt/sources.list.d/ubuntu.sources and add deb-src to the Types line.

sudo vim /etc/apt/sources.list.d/ubuntu.sources

Types: deb deb-src

Now that has been added you can get build-deps for Python:

sudo apt update
sudo apt build-dep python3
sudo apt install -y pkg-config

Install all the additional packages for python extras, you want these

I generally install all additionals since many are quite important for python. Some are more specialized and you may not use, while others like libcurses are basically required if you expect to ever need to interact with an interpreter (eg troubleshooting a production environment). Others you may not need until some future data, say when you install a package that expects a certain kind of data compression. Overall, my advice is just install all unless you know more or have a specialized use case for streamlined environment.

sudo apt -y install build-essential gdb lcov pkg-config libbz2-dev libffi-dev libgdbm-dev libgdbm-compat-dev liblzma-dev libncurses-dev libreadline-dev libsqlite3-dev libssl-dev lzma tk-dev uuid-dev zlib1g-dev libmpdec-dev libzstd-dev inetutils-inetd

Download the latest version:

Choose Gzipped Tarball https://www.python.org/

  1. curl -O URL
  2. tar -xvf ZIPPEDPYTHONFILE you will now have a directory like Python3.12.xxx
  3. sudo mv Python3.12.x /opt/ Move python installation directory to /opt/ to keep home clean
  4. cd /opt/Python3.12.x into the directory and run each command
  5. Check you have all packages installed from above
  6. ./configure --enable-optimizations
  7. make Builds Python, this step takes some time.
  8. sudo make altinstall altinstall skips creating the python link and the manual pages links, install will hide the system binaries and manual pages. This means that we will leave the system python installation untouched

Python is now installed.

Last important step is to configure the system links: sudo ldconfig /opt/Python3.14.x

Finally you can test:

python3.14

联系我们 contact @ memedata.com