前言
Debian 和 Ubuntu 系统自带的 Python 版本通常比较低,对于一些要求 Python 高版本的项目很是烦恼。下面是升级 Python 版本的代码
一键脚本安装 Python3.6-Python3.10
1
| bash <(curl -sSL https://raw.githubusercontent.com/midoks/choose-linux-python/main/install.sh)
|
text
安装 Python3.8
注意下面的脚本如果安装包下载不下来,换成国内源 https://mirrors.aliyun.com/python-release/source/
1 2 3 4 5 6 7 8 9 10 11 12 13
| cat <<"EOF" | bash sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libsqlite3-dev libreadline-dev libffi-dev curl libbz2-dev -y && \ curl -O https://www.python.org/ftp/python/3.8.2/Python-3.8.2.tar.xz && \ tar -xf Python-3.8.2.tar.xz && \ cd Python-3.8.2 && \ ./configure --enable-optimizations && \ make -j 4 && \ sudo make altinstall && \ python3.8 --version EOF
apt update && apt install python3-pip
|
安装 Python3.10
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| cat <<"EOF" | bash apt update && apt upgrade -y apt install git wget curl screen htop vim sudo -y apt-get --purge remove python3 python3-pip -y apt install build-essential libreadline-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev libffi-dev zlib1g-dev -y wget https://www.python.org/ftp/python/3.10.8/Python-3.10.8.tgz tar -xzvf Python-3.10.8.tgz cd Python-3.10.8 ./configure --prefix=/usr/local/python3 make altinstall rm -f /usr/bin/python3 rm -f /usr/local/bin/pip3 ln -s /usr/local/python3/bin/python3.10 /usr/bin/python3 ln -s /usr/local/python3/bin/pip3.10 /usr/bin/pip3 python3 -m pip install --upgrade pip EOF
python3 -V && pip3 -V
|
查看 python 位置
一般是/usr/bin/python2 和/usr/bin/python3
查询当前配置
1
| update-alternatives --list python
|
没有配置过的话会返回 update-alternatives: error: no alternatives for python
配置默认版本
通过设置优先级来实现默认使用 python3
1 2
| sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 1 sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 2
|
查询默认版本号
注意
有些脚本需要手动修改 python 版本(如 yum),将/usr/bin/python 改为/usr/bin/python2