在Ubuntu18.04.6系统下安装python3.8.5
1.配置下载源
1.1 备份原有软件源文件 cp /etc/apt/sources.list /etc/apt/sources.list.bak_yyyymmdd
1.2 打开sources.list文件vim /etc/apt/sources.list
1.3 下载源配置
#阿里源地址
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
#华为源地址
deb https://repo.huaweicloud.com/ubuntu/ focal main restricted
deb https://repo.huaweicloud.com/ubuntu/ focal-updates main restricted
deb https://repo.huaweicloud.com/ubuntu/ focal universe
deb https://repo.huaweicloud.com/ubuntu/ focal-updates universe
deb https://repo.huaweicloud.com/ubuntu/ focal-backports main restricted universe
deb https://repo.huaweicloud.com/ubuntu/ focal-security main restricted
deb https://repo.huaweicloud.com/ubuntu/ focal-security universe
#中科大源地址
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
#163源地址
deb http://mirrors.163.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-backports main restricted universe multiverse
#清华源地址
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe ultiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe ultiverse
1.4 更新系统软件源地址并下载python:
apt-get update
apt-get upgrade
wget https://mirrors.huaweicloud.com/python/3.8.5/Python-3.8.5.tgz
1.5 下载 c 编译器
sudo apt install gcc
sudo apt install g++
sudo apt install build-essential
1.6 解压
tar -zxvf Python-3.8.5.tgz
cd Python-3.8.5
./configure --enable-optimizations --prefix=/usr/local/python3
make && make install
注意:若找不到./configure,删除解压后的文件夹和安装包,重新下载解压,然后安装
在执行make && make install 时报错(zipimport.ZipImportError: can’t decompress data)
解决思路:
-
安装zlib相关依赖包
apt -y install zlib*
-
修改Module路径下的Setup文件
vim Modules/Setup
,去掉注释
#zlib zlibmodule.c -I$(prefix)/include -L$(exec_prefix)/lib -lz
zlib zlibmodule.c -I$(prefix)/include -L$(exec_prefix)/lib -lz
- 设置python和pip的环境变量
若系统自带了其他版本的python,对其进行重命名或删除操作
# 设置python环境变量
ln -s /usr/local/python3/bin/python3.8 /usr/bin/python
# 设置pip环境变量
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip
- 重新执行
make && make install
正确设置 pip3,避免There was a problem confirming the ssl certificate问题
pip config set global.extra-index-url http://mirrors.aliyun.com/pypi/simple
此时可以看到 pip 自动生成了一个文件( /root/.config/pip/pip.conf)
编辑文件
vim /root/.config/pip/pip.conf
修改文件,配置如下
[global]
index-url = http://mirrors.aliyun.com/pypi/simple
extra-index-url =
http://pypi.douban.com/simple
https://pypi.tuna.tsinghua.edu.cn/simple
http://pypi.mirrors.ustc.edu.cn/simple
http://pypi.hustunique.com
[install]
trusted-host =
mirrors.aliyun.com
pypi.douban.com
pypi.tuna.tsinghua.edu.cn
pypi.mirrors.ustc.edu.cn
mirrors.ustc.edu.cn
mirrors.bfsu.edu.cn
pypi.hustunique.com