概述
安装环境
CentOS Linux release 7.6.1810 (Core)
Linux 3.10.0-957.21.3.el7.x86_64 #1 SMP Tue Jun 18 16:35:19 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
准备安装的是python3.6.4这个版本
1、首先是安装一下相关依赖
yum install -y lib-devel bzip2-devel openssl-devel openssl-static ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel libffi-devel lzma gcc
2、接下来去到官网找的python3.6.4这个版本
我选择下载tgz包,下载连接https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tgz
3、在linux中用wget命令获取这个包
[root@hobby ~]# wget https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tgz
4、用tar命令解压这个压缩包
[root@hobby ~]# tar -xvf Python-3.6.4.tgz
5、解压好后,创建一个安装目录
[root@hobby ~]# mkdir /usr/local/python3
6、进到解压后的文件夹,然后进行编译,并指定安装目录
[root@hobby ~]# cd Python-3.6.4
[root@hobby Python-3.6.4]# ll
total 1036
-rw-r--r-- 1 501 501 13335 Dec 19 2017 aclocal.m4
-rwxr-xr-x 1 501 501 44259 Dec 19 2017 config.guess
-rwxr-xr-x 1 501 501 36515 Dec 19 2017 config.sub
-rwxr-xr-x 1 501 501 489889 Dec 19 2017 configure
-rw-r--r-- 1 501 501 162850 Dec 19 2017 configure.ac
drwxr-xr-x 18 501 501 4096 Dec 19 2017 Doc
drwxr-xr-x 2 501 501 4096 Dec 19 2017 Grammar
drwxr-xr-x 2 501 501 4096 Dec 19 2017 Include
-rwxr-xr-x 1 501 501 7122 Dec 19 2017 install-sh
drwxr-xr-x 33 501 501 4096 Dec 19 2017 Lib
-rw-r--r-- 1 501 501 12757 Dec 19 2017 LICENSE
drwxr-xr-x 8 501 501 4096 Dec 19 2017 Mac
-rw-r--r-- 1 501 501 61139 Dec 19 2017 Makefile.pre.in
drwxr-xr-x 2 501 501 4096 Dec 19 2017 Misc
drwxr-xr-x 13 501 501 4096 Dec 19 2017 Modules
drwxr-xr-x 4 501 501 4096 Dec 19 2017 Objects
drwxr-xr-x 2 501 501 4096 Dec 19 2017 Parser
drwxr-xr-x 6 501 501 4096 Dec 19 2017 PC
drwxr-xr-x 2 501 501 4096 Dec 19 2017 PCbuild
drwxr-xr-x 2 501 501 4096 Dec 19 2017 Programs
-rw-r--r-- 1 501 501 42074 Dec 19 2017 pyconfig.h.in
drwxr-xr-x 3 501 501 4096 Dec 19 2017 Python
-rw-r--r-- 1 501 501 9269 Dec 19 2017 README.rst
-rw-r--r-- 1 501 501 103237 Dec 19 2017 setup.py
drwxr-xr-x 23 501 501 4096 Dec 19 2017 Tools
[root@hobby Python-3.6.4]# ./configure --prefix=/usr/local/python3
7、接下来就是安装了,还是在解压后的目录
[root@hobby Python-3.6.4]# make && make install
8、做软链接,因为linux自带python2.7并且很多内置依赖都用的是python2.7所以这里我就软链接为python3
[root@hobby python3]# ln -s /usr/local/python3/bin/python3 /usr/bin/python3
9、软链成功后查看使用情况,阔以看到输入python默认用的是2.7,python3用的是3.6
[root@hobby python3]# python
Python 2.7.5 (default, Apr 9 2019, 14:30:50)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
[root@hobby python3]# python3
Python 3.6.4 (default, Feb 16 2020, 20:27:59)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
10、最后一步,就是将python3加入环境变量
[root@hobby bin]# sudo vim ~/.bash_profile
#文件内容如下
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
# PATH=$PATH:$HOME/bin
# 将python3的路径加入此文件
PATH=$PATH:$HOME/bin:/usr/local/python3/bin
export PATH
11、修改完文件后提交修改(让上边修改内容生效),查看pip3版本,成功
[root@hobby bin]# source ~/.bash_profile
[root@hobby bin]# pip3 -V
pip 9.0.1 from /usr/local/python3/lib/python3.6/site-packages (python 3.6)
至此,python3.6就妥妥的安装完成了
但是作为一个强迫症,看到pip版本如此的陈旧,当然就要更新一下啦
[root@hobby bin]# python3 -m pip install --upgrade pip
# 验证更新结果
[root@hobby bin]# pip -V
pip 19.1.1 from /usr/lib/python2.7/site-packages/pip (python 2.7)
[root@hobby bin]# pip3 -V
pip 20.0.2 from /usr/local/python3/lib/python3.6/site-packages/pip (python 3.6)
可以看到python3版本的已经更新到最新版本了~
最后
以上就是现实小虾米为你收集整理的Centos7安装Python3.6.4(与自带的Python2.7共存)的全部内容,希望文章能够帮你解决Centos7安装Python3.6.4(与自带的Python2.7共存)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复