CentOS7下安装python3.8
有一台centos7服务器,自带的是python2.7,所以想升级到python3,以下即是详细升级步骤,方便日后查询。
1. 查看当前python版本
[root@racknerd ~]# python
Python 2.7.5 (default, Nov 16 2020, 22:23:17)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
可以看到执行python,默认是2.7。
2. 安装相关依赖包
yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make libffi-devel wget
编译python源码时,需要一些依赖包,一次安装完毕。
3. 下载源码包
mkdir -p /root/soft
cd /root/soft
wget https://www.python.org/ftp/python/3.8.1/Python-3.8.1.tgz
下载python3.8,如果想安装其他版本,去python官网下载页面下载对应的版本即可。
4. 解压安装
# 解压压缩包
tar -zxvf Python-3.8.1.tgz
# 进入文件夹
cd Python-3.8.1
# 配置安装位置
./configure prefix=/usr/local/python3
# 安装
make && make install
如果最后没提示出错,就代表正确安装了,在/usr/local/目录下就会有python3目录
[root@racknerd ~/soft/Python-3.8.1]# ls /usr/local/
bin etc games include lib lib64 libexec man python3 sbin share src
5. 添加软连接
#添加python3的软链接
ln -s /usr/local/python3/bin/python3.8 /usr/bin/python3
#添加 pip3 的软链接
ln -s /usr/local/python3/bin/pip3.8 /usr/bin/pip3
#检查
[root@racknerd ~]# ll /usr/bin/python3 /usr/bin/pip3
lrwxrwxrwx 1 root root 29 Mar 23 10:25 /usr/bin/pip3 -> /usr/local/python3/bin/pip3.8
lrwxrwxrwx 1 root root 32 Mar 23 10:25 /usr/bin/python3 -> /usr/local/python3/bin/python3.8
#升级pip3
pip3 install --upgrade pip
添加好了,测试一下python3
[root@racknerd ~]# python3
Python 3.8.1 (default, Mar 23 2023, 10:21:58)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
注:这里我没有链接到python上,是因为yum
要用到python2
才能执行,所以现在输入python的话还是会进入python2.7,输入python3才会进入python3.8
如果执意想要软链接到python的话,就得先修改如下yum的配置,不然yum就不能使用:
vi /usr/bin/yum
把 #!/usr/bin/python 修改为 #!/usr/bin/python2
vi /usr/libexec/urlgrabber-ext-down
把 #!/usr/bin/python 修改为 #!/usr/bin/python2