1.先检查相关信息
# 三个命令均可 查询Ubuntu版本
cat /proc/version
uname -a
lsb_release -a
# 查看系统python位置(Ubuntu16.04默认有2.7和3.5)
whereis python
2.配置依赖问题
sudo apt-get install zlib1g-dev libbz2-dev libssl-dev libncurses5-dev libsqlite3-dev
3.官网下载Python3.8并解压
wget https://www.python.org/ftp/python/3.8.2/Python-3.8.2.tar.xz
tar -xvJf Python-3.8.2.tar.xz
cd Python-3.8.2
4.安装必要依赖
sudo apt-get install python-dev libffi-dev libssl-dev
sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev
5.执行安装
./configure prefix=/usr/local/python3
sudo make && sudo make install
6.安装pip并升级
#这句是给(python2.7安装pip)
sudo apt-get install python-pip
#这句是给python3安装pip
sudo apt-get install python3-pip
# 更换pip源
mkdir .pip
vim .pip/pip.conf
# 添加下面内容
[global]
index-url=https://pypi.tuna.tsinghua.edu.cn/simple
[install]
trusted-host=https://pypi.tuna.tsinghua.edu.cn
# 升级pip
pip install pip -U
pip3 install pip -U
7.安装Python虚拟环境
# 执行python-virtualenv会同时执行python3-virtualenv
sudo apt-get install python-virtualenv
# 安装virtualwrapper方便管理python虚拟环境管理工具
pip3 install virtualenvwrapper
# 可能会出现locale.Error: unsupported locale setting的错误
# 解决方法两种
# 1.export LC_ALL=C 执行该命令, 临时作用
# 2.vim ~/.bashrc 编辑.bashrc
# export LC_ALL=C 尾部添加
# source ~/.bashrc 执行该命令; 再从新执行pip3命令
# 配置virtualwrapper环境变量
vim ~/.bashrc
# 尾部增加以下内容
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
export WORKON_HOME=$HOME/.virtualenvs
source ~/.local/bin/virtualenvwrapper.sh # 确认好virtualenvwrapper.sh位置
# 执行命令
source ~/.bashrc
8.创建虚拟环境
# 创建指定python版本的虚拟环境
mkvirtualenv -p /usr/bin/python3 env_py3
mkvirtualenv -p /usr/bin/python2 env_py2
# pip3可能会出现某些问题,命令安装相关包
# sudo pip3 install virtualenv
# sudo pip3 install --no-deps stevedore
# sudo pip3 install --no-deps virtualenvwrapper
# 运行环境
workon env_py3
# 退出虚拟环境
deactivate
# 删除虚拟环境
rmvirtualenv env_py3