内容纲要
前置环境准备
sudo apt-get update
sudo apt-get install libffi-dev
sudo apt install sqlite*
sudo apt-get install libbz2-dev
ll /usr/include/openssl
sudo apt-get install -y openssl libssl-dev
sudo apt-get install build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev
Python3环境安装
-
下载Python的源代码
https://www.python.org/downloads/source/wget https://www.python.org/ftp/python/3.11.7/Python-3.11.7.tgz
wget https://www.python.org/ftp/python/3.9.9/Python-3.9.9.tgz
2. 将源码包传输到Linux服务器
3. 解压
```bash
tar -zxvf Python-3.11.7.tgz
cd Python-3.11.7
tar -zxvf Python-3.9.9.tgz
cd Python-3.9.9
-
创建安装目录
pwd mkdir python311
```bash
mkdir python39 -
进入解压的Python-3.11.7目录,编译安装Python
cd Python-3.11.7/ ./configure --prefix=/linqingyang/software/python311 --enable-shared --enable-optimizations --with-openssl=/usr/local/ssl make make install
--prefix
是希望安装Python的目录绝对路径注意:如果make失败,则需要执行
make clean
清除之前安装的make编译文件,然后再执行以下步骤。./configure --prefix=/linqingyang/software/python3.11 --enable-optimizations make
如果make失败,可执行
make clean
清除编译过程中生成的文件。
如果报错no acceptable C compiler found in $PATH
需要安装C编译器-> ubuntu
sudo apt update sudo apt install build-essential
-> CentOS Linux
sudo yum groupinstall 'Development Tools'
安装完成后,再次运行configure命令
-
安装成功,显示成功安装的结果
Successfully installed pip-23.2.1 setuptools-65.5.0
-
验证安装结果
/linqingyang/software/python311/bin/python3 --version
/linqingyang/software/python39/bin/python3 --version
输入安装的python版本
Python 3.11.7
Python 3.9.9
-
配置python3.11到当前用户的环境变量中
vi ~/.bash_profile
添加
```
export PATH=/linqingyang/software/python311/bin:$PATH:wq
保存退出 -
激活修改后的配置文件
source ~/.bash_profile
-
再次验证,显示版本号即可
```bash
python3 --version
Python 3.11.7
Python 3.9.9 -
(可选)清理源码包和源码解压目录
rm -rf Python-3.11.7/ rm -rf Python-3.11.7.tgz