在安装python3.9.0之前,首先要进行换源,这样才能防止下载过慢的情况。

我这里换的是阿里云的镜像源,在终端输入一下命令。

其他镜像源可以查看:https://www.myfreax.com/ubuntu-22-04geng-gai-jing-xiang-ruan-jian-yuan/

sudo bash -c “cat < /etc/apt/sources.list && apt update
deb http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
EOF”

开始之前要安装依赖

sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev libsqlite3-dev wget libbz2-dev

1.下载python3.9.0

wget https://www.python.org/ftp/python/3.9.0/Python-3.9.0.tgz

2.对下载后的python3.9.0进行解压

tar -zxvf Python-3.9.0.tgz

3.解压后进入Python-3.9.0目录

cd Python3.9.0/

4.开始安装

./configure –enable-optimizations

接着执行以下代码

make

sudo make install

5.设置软链接

rm python

which python3

找到的路径建立新的链接

sudo ln -s /usr/local/bin/python3 /usr/bin/python

6.设置python的环境变量

vim ~/.bashrc

在bashrc里面添加一下代码

allias python=’/usr/local/bin/python3′

然后让变量生效

source ~/.bashrc

最后直接输入python就可以了