相信不少小伙伴在linux安装python时候都会遇到一个坑,就是使用系统命令安装python3时会把自带的python2覆盖掉,然后就导致系统的yum命令使用不了,linux想要多个python版本并存常用的办法是使用anconda,今天我们说一个不需要anconda的方法。
安装依赖
首先执行下面命令安装需要的依赖
yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
如果安装的是python3.7以上的版本,那么还需要安装libffi-devel
yum -y install libffi-devel -y
python3安装
下载我就不说了,大家去官网下载一个python镜像(linux版本的)
解压
为了安装方便,我们把下载的python包解压到/usr/local/python3.7路径下,没有的创建一个
tar -zxvf Python-3.7.3.tgz
编译安装
进入解压的目录里面,然后开始解压,解压前先看一下有没有gcc
[root@bupps50-smoke-root-171 local]# gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/cloog-install --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix
gcc version 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC)
[root@bupps50-smoke-root-171 local]#
如果自己的环境上没有gcc的话执行命令安装一下
yum -y install gcc
确定有了gcc之后我们就可以编译安装了,我们先指定一下安装目录然后再编译安装
[root@bupps50-smoke-root-171 local]# ./configure --prefix=/usr/local/python3
[root@bupps50-smoke-root-171 local]# make && make install
建立python3和pip3的软连接
[root@bupps50-smoke-root-171 local]# ln -s /usr/local/python3/bin/python3 /usr/bin/python3
[root@bupps50-smoke-root-171 local]# ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
环境变量添加
打开文件编辑 vim /etc/profile,在文件的末尾添加下面两行,路径换成自己的路径
export PYTHON_HOME=/usr/local/python3
export PATH=$PYTHON_HOME/bin:$PATH
然后执行下面命令让其生效
[root@bupps50-smoke-root-171 local]# source /etc/profile
我们来验证一下
[root@bupps50-smoke-root-171 local]# python3
Python 3.7.3 (default, Sep 30 2020, 09:40:57)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
我们再来验证一下pip3的版本
[root@bupps50-smoke-root-171 local]# pip3 -V
pip 19.0.3 from /usr/local/python3/lib/python3.7/site-packages/pip (python 3.7)
ok,至此我们的环境已经安装完成,而且不用anconda就可以和linux自带的python2并存