怪鲶鱼很好钓 發表於 2025-6-8 15:48:00

python之Ubuntu环境安装python3.10

<h1 id="python之ubuntu环境安装python310">python之Ubuntu环境安装python3.10</h1>
<p></p><div class="toc"><div class="toc-container-header">目录</div><ul><li>python之Ubuntu环境安装python3.10<ul><li>1 Ubuntu 安装python3.10<ul><li>1.1 方法1:使用 deadsnakes PPA 安装 Python 3.10</li><li>1.2 方法2:源码编译安装 Python 3.10</li></ul></li><li>2 设置默认 Python 版本(可选)<ul><li>2.1 update-alternatives 的工作原理</li></ul></li><li>3 升级对应的pip<ul><li>3.1 方法1:使用 <code>ensurepip</code> 模块</li><li>3.2 方法2:使用 <code>get-pip.py</code> 脚本</li><li>3.3 方法3:通过包管理器安装(适用于某些发行版)</li></ul></li><li>4 设置默认 <code>pip</code> 版本(可选)</li></ul></li></ul></div><p></p>
<p>Ubuntu环境安装较新的python版本,可能会遇见一些问题,这里以Ubuntu安装python3.10为例</p>
<h2 id="1-ubuntu-安装python310">1 Ubuntu 安装python3.10</h2>
<p>直接使用ubuntu apt命令安装python3.10:</p>
<pre><code class="language-sh">$ sudo apt update
$ sudo apt install python3.10
</code></pre>
<p>如果安装不成功,可能是因为apt软件源中并没有直接提供 <code>python3.10</code> 这个包。要正确安装 Python 3.10,可以尝试以下办法:</p>
<h3 id="11-方法1使用-deadsnakes-ppa-安装-python-310">1.1 方法1:使用 deadsnakes PPA 安装 Python 3.10</h3>
<ol>
<li>
<p><strong>添加 deadsnakes PPA</strong>:<br>
<code>ppa:deadsnakes/ppa</code> 这是一个 Launchpad 提供的个人包仓库(Personal Package Archive,简称 PPA),由 Deadsnakes 团队 维护。它提供了官方 Ubuntu 不再支持或尚未包含的 Python 版本,例如:Python 3.6、3.7、3.8、3.9、3.10、3.11、3.12 等,可以运行以下命令来添加它:</p>
<pre><code class="language-bash">$ sudo apt update
$ sudo apt install -y software-properties-common
$ sudo add-apt-repository ppa:deadsnakes/ppa -y
</code></pre>
<p>上述步骤后,ppa源添加到 /etc/apt/sources.list.d/deadsnakes-ubuntu-ppa-jammy.list 中</p>
</li>
<li>
<p><strong>更新包列表并安装 Python 3.10</strong>:<br>
添加 PPA 后,确保更新包列表以包含新添加的资源</p>
<pre><code class="language-bash">$ sudo apt update
$ sudo apt install python3.10 python3.10-venv python3.10-dev
</code></pre>
</li>
<li>
<p><strong>验证安装</strong>:<br>
最后,检查是否正确安装了 Python 3.10</p>
<pre><code class="language-bash">$ python3.10 --version
</code></pre>
</li>
</ol>
<h3 id="12-方法2源码编译安装-python-310">1.2 方法2:源码编译安装 Python 3.10</h3>
<p>如果通过 PPA 仍然无法安装,可以考虑手动下载Python 3.10源码并编译安装</p>
<ol>
<li>
<p><strong>安装依赖项</strong>:<br>
编译 Python 需要一些开发工具和库</p>
<pre><code class="language-bash">$ sudo apt update
$ sudo apt install -y build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev curl libbz2-dev
</code></pre>
</li>
<li>
<p><strong>下载 Python 源码</strong>:<br>
从 Python 官方网站下载 Python 3.10 的源码</p>
<pre><code class="language-shell">$ sudo wget https://www.python.org/ftp/python/3.10.0/Python-3.10.0.tgz
$ sudo tar xzf Python-3.10.0.tgz
</code></pre>
</li>
<li>
<p><strong>编译和安装</strong>:<br>
编译并安装 Python</p>
<pre><code class="language-bash">$ cd Python-3.10.0
$ sudo ./configure --enable-optimizations
$ sudo make altinstall
</code></pre>
<p>注意:这里使用 <code>make altinstall</code> 而不是 <code>make install</code>,以避免覆盖系统默认的 <code>python3</code> 命令。</p>
</li>
<li>
<p><strong>验证安装</strong>:<br>
最后,检查是否正确安装了 Python 3.10</p>
<pre><code class="language-bash">$ python3.10 --version
</code></pre>
</li>
</ol>
<h2 id="2-设置默认-python-版本可选">2 设置默认 Python 版本(可选)</h2>
<p>如果希望将 Python 3.10 设置为默认版本,可以使用 <code>update-alternatives</code> 来管理不同版本的 Python,选择 Python 3.10 作为默认版本:</p>
<pre><code class="language-bash">$ sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 1
# 调整配置
$ sudo update-alternatives --config python3
# 查看当前配置
$ sudo update-alternatives --display python3

# 将python3.10 命名为python
$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.10 1
</code></pre>
<h3 id="21-update-alternatives-的工作原理">2.1 update-alternatives 的工作原理</h3>
<p>这里简单介绍一下 update-alternatives 的工作原理,update-alternatives通过<strong>双重软链接</strong>对软链接进行管理,实现对软件版本的切换。</p>
<p><strong>update-alternatives 命令的参数:</strong></p>
<pre><code class="language-sh">$ sudo update-alternatives &lt;link&gt; &lt;name&gt; &lt;path&gt; &lt;priority&gt;
</code></pre>
<p>其参数列表如下:</p>
<table>
<thead>
<tr>
<th>参数</th>
<th>示例</th>
<th>含义</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>&lt;link&gt;</code></td>
<td><code>/usr/bin/python3</code></td>
<td>系统中使用的符号链接路径(用户执行 <code>python3</code> 时实际调用的路径)</td>
</tr>
<tr>
<td><code>&lt;name&gt;</code></td>
<td><code>python3</code></td>
<td>替代项组的名字,对应一个逻辑名称,对应 /etc/alternatives/python3</td>
</tr>
<tr>
<td><code>&lt;path&gt;</code></td>
<td><code>/usr/local/bin/python3.10</code></td>
<td>实际要被链接的可执行文件路径</td>
</tr>
<tr>
<td><code>&lt;priority&gt;</code></td>
<td><code>1</code></td>
<td>优先级,数字越大越优先(用于自动选择默认版本)</td>
</tr>
</tbody>
</table>
<p><code>&lt;link&gt;</code> 与 <code>&lt;name&gt;</code>可以自定义,但是要注意:</p>
<ol>
<li>
<p><code>&lt;link&gt;</code> 名字是你要使用的命令名,比如使用python或python3,则需建立<code>/usr/bin/python</code>或 <code>/usr/bin/python3</code>的快捷方式</p>
</li>
<li>
<p><code>&lt;name&gt;</code>对应 /etc/alternatives/<code>&lt;name&gt;</code>,与&lt;link&gt;是绑定关系,一个 <code>&lt;link&gt;</code> 只能绑定一个 <code>&lt;name&gt;</code>所以不能将不同的<code>&lt;name&gt;</code>绑定到同一个<code>&lt;link&gt;</code> 上</p>
</li>
<li>
<p>可以对相同的<code>&lt;link&gt;</code> +<code>&lt;name&gt;</code> 绑定不同的可执行文件路径<code>&lt;path&gt;</code>,由<code>&lt;priority&gt;</code>来确定哪个可执行文件生效</p>
</li>
</ol>
<p><strong>举例:</strong></p>
<p>(假设Ubuntu系统中安装了python3.10与python3.11):</p>
<pre><code class="language-sh">$ sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 2
$ ls -l /usr/bin/python3
/usr/bin/python3 -&gt; /etc/alternatives/python3
$ ls -l /etc/alternatives/python3
/etc/alternatives/python3 -&gt; /usr/bin/python3.10

# 设置python3.11 优先级比python3.10低,不会改变/etc/alternatives/python3指向位置
$ sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.11 1
# 可见:执行python3 时使用的还是python3.10
$ ls -l /etc/alternatives/python3
/etc/alternatives/python3 -&gt; /usr/bin/python3.10


# 设置python3.11 优先级比python3.10高,则会改变/etc/alternatives/python3指向位置
$ sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.11 3
# 可见:执行python3 时使用的是python3.11
$ ls -l /etc/alternatives/python3
/etc/alternatives/python3 -&gt; /usr/bin/python3.11
</code></pre>
<p>不能将不同的<code>&lt;name&gt;</code>绑定到同一个<code>&lt;link&gt;</code>上,否则会报错,也不能将不同的<code>&lt;link&gt;</code>绑定到同一个<code>&lt;name&gt;</code>上,否则将会rename<code> &lt;link&gt;</code>:</p>
<pre><code class="language-sh"># 不能将不同的`&lt;name&gt;`绑定到同一个`&lt;link&gt;`上,否则会报错:
$ sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 2
$ sudo update-alternatives --install /usr/bin/python3 python310 /usr/bin/python3.10 2
update-alternatives: error: alternative link /usr/bin/python3 is already managed by python3

# 也不能将不同的`&lt;link&gt;`绑定到同一个`&lt;name&gt;`上,否则将会重命名 &lt;link&gt;:
$ sudo update-alternatives --install /usr/bin/python310 python3 /usr/bin/python3.10 2
update-alternatives: renaming python3 link from /usr/bin/python3 to /usr/bin/python310
</code></pre>
<p><strong>update-alternatives 的常用操作:</strong></p>
<table>
<thead>
<tr>
<th>操作</th>
<th>命令</th>
</tr>
</thead>
<tbody>
<tr>
<td>添加 Python 3.10 到 alternatives</td>
<td>sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 1</td>
</tr>
<tr>
<td>查看当前配置</td>
<td>sudo update-alternatives --display python3</td>
</tr>
<tr>
<td>切换默认 Python 版本</td>
<td>sudo update-alternatives --config python3</td>
</tr>
<tr>
<td>删除某个版本</td>
<td>sudo update-alternatives --remove python3 /usr/bin/python3.10</td>
</tr>
</tbody>
</table>
<h2 id="3-升级对应的pip">3 升级对应的pip</h2>
<p>安装与 Python 3.10 对应的 <code>pip</code> 版本可以通过几种方法实现。以下是针对不同情况的具体步骤:</p>
<h3 id="31-方法1使用-ensurepip-模块">3.1 方法1:使用 <code>ensurepip</code> 模块</h3>
<p>Python 3.4 及以上版本自带了 <code>ensurepip</code> 模块,可以直接用来安装或升级 <code>pip</code>。</p>
<pre><code class="language-bash">python3.10 -m ensurepip --upgrade
</code></pre>
<p>这将确保为 Python 3.10 安装最新版本的 <code>pip</code> 或者升级现有的 <code>pip</code>。</p>
<h3 id="32-方法2使用-get-pippy-脚本">3.2 方法2:使用 <code>get-pip.py</code> 脚本</h3>
<p>如果 <code>ensurepip</code> 不可用或你需要更灵活地控制 <code>pip</code> 的安装,可以使用官方提供的 <code>get-pip.py</code> 脚本来安装 <code>pip</code>。</p>
<ol>
<li>
<p><strong>下载 <code>get-pip.py</code></strong>:</p>
<pre><code class="language-bash">$ curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
</code></pre>
</li>
<li>
<p><strong>运行脚本安装 <code>pip</code></strong>:<br>
使用 Python 3.10 来执行 <code>get-pip.py</code></p>
<pre><code class="language-bash">$ python3.10 get-pip.py
</code></pre>
</li>
<li>
<p><strong>验证安装</strong>:<br>
确认 <code>pip</code> 是否正确安装并关联到 Python 3.10</p>
<pre><code class="language-bash">$ python3.10 -m pip --version
</code></pre>
</li>
</ol>
<h3 id="33-方法3通过包管理器安装适用于某些发行版">3.3 方法3:通过包管理器安装(适用于某些发行版)</h3>
<p>在某些 Linux 发行版中,您可以直接通过包管理器安装与特定 Python 版本对应的 <code>pip</code> 包。</p>
<pre><code class="language-bash">$ sudo apt install python3.10-distutils# 如果缺失 distutils 模块
$ sudo apt install python3.10-venv       # 如果需要创建虚拟环境
$ sudo apt install python3.10-pip      # 直接安装 pip
</code></pre>
<h2 id="4-设置默认-pip-版本可选">4 设置默认 <code>pip</code> 版本(可选)</h2>
<p>如果您有多个版本的 <code>pip</code> 并希望设置 Python 3.10 的 <code>pip</code> 作为默认版本,可以使用 <code>update-alternatives</code> 来管理。</p>
<pre><code class="language-bash"># 如果系统有多个版本,可以将优先级提高,这里优先级用100
$ sudo update-alternatives --install /usr/bin/pip pip /usr/bin/pip3.10 100
</code></pre>
<p>选择 Python 3.10 的 <code>pip</code> 作为默认版本。</p>
<p><strong>验证安装</strong></p>
<p>最后,验证 <code>pip</code> 是否正确安装并关联到 Python 3.10。</p>
<pre><code class="language-bash">$ pip --version
$ python3.10 -m pip --version
</code></pre>
<p><strong>注意事项:</strong></p>
<ul>
<li><strong>避免覆盖系统默认 <code>pip</code></strong>:如果您不想覆盖系统的默认 <code>pip</code> 命令,可以使用 <code>python3.10 -m pip</code> 来调用 Python 3.10 的 <code>pip</code></li>
<li><strong>使用虚拟环境</strong>:对于开发工作,建议使用虚拟环境来隔离项目依赖。创建虚拟环境时,<code>pip</code> 会自动安装在该环境中</li>
</ul>
<p>通过上述方法之一,您应该能够成功为 Python 3.10 安装对应的 <code>pip</code> 版本。</p>
<p>1 ubuntu系统下多个的python版本,设置默认python和pip_ubuntu设置默认python版本-CSDN博客</p><br><br>
来源:https://www.cnblogs.com/sureZ-learning/p/18919428
頁: [1]
查看完整版本: python之Ubuntu环境安装python3.10