1.下载python安装包
wget https://www.python.org/ftp/python/3.6.8/Python-3.6.8.tar.xz
2.解压安装包
sudo xz -d Python-3.6.8.tar.xz
sudo tar -xvf Python-3.6.8.tar
3.安装
cd Python-3.6.8/
./configure --prefix=/opt/python3.6
4.编译
sudo make
sudo make install
在编译过程中会遇到错误
zipimport.ZipImportError: can't decompress data; zlib not available Makefile:1102: recipe for target 'install' failed ma...
原因是缺少相关依赖
进行apt-get install zlib安装发现安装不了,显示
E: Unable to locate package zlib
在之前以为是软件源没有更新,于是进入/etc/apt/sources.list 修改源
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
然后进行 sudo apt-get update后发现还是无法解决问题。
后来发现在ubuntu软件源里zlib和zlib-devel叫做zlib1g zlib1g.dev
sudo apt-get install zlib1g
sudo apt-get install zlib1g.dev
apt-get install python3-dev
然后在Python-3.6.8/Modules/Setup中将
#zlib zlibmodule.c -I$(prefix)/include -L$(exec_prefix)/lib -lz
的注释去掉。并重新编译sudo make & make install。
5.更改软连接
ln -s /opt/python3.6/bin/python3 /usr/bin/python3
ln -s /opt/python3.6/bin/pip3 /usr/local/bin/pip3
在运行pip3时可能会出现
subprocess.CalledProcessError: Command '('lsb_release', '-a')' returned non-zero exit status 1.
使用sudo
rm
/usr/bin/lsb_release即可。