前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Centos python3编译安装以及编译gcc升级

Centos python3编译安装以及编译gcc升级

作者头像
嘻哈记
发布2020-11-30 10:15:20
6.3K2
发布2020-11-30 10:15:20
举报
文章被收录于专栏:运维学习交流运维学习交流

引言:由于我是在新的虚拟机上测试学习,正好听到同事讲一个朋友在gcc升级安装导致系统出问题,所以在安装gcc的时候一定要小心。

1.系统环境

1.1 gcc版本

代码语言:javascript
复制
[root@linux-01 ~]# yum install -y gcc
# 安装过程省略

[root@linux-01 ~]# gcc -v
使用内建 specs。
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/lto-wrapper
目标:x86_64-redhat-linux
配置为:../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
线程模型:posix
gcc 版本 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)

1.2 centos版本

代码语言:javascript
复制
[root@linux-01 ~]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)

1.3 下载Python3.9版本

代码语言:javascript
复制
[root@linux-01 ~]# wget https://www.python.org/ftp/python/3.9.0/Python-3.9.0.tar.xz

[root@linux-01 ~]# tar -xvJf Python-3.9.0.tar.xz

1.4 python安装报错

代码语言:javascript
复制
[root@linux-01 ~]# cd Python-3.9.0

[root@linux-01 Python-3.9.0]# ./configure --prefix=/usr/local/python3 --enable-optimizations --with-ssl
#第一个指定安装的路径,不指定的话,安装过程中可能软件所需要的文件复制到其他不同目录,删除软件很不方便,复制软件也不方便.
#第二个可以提高python10%-20%代码运行速度.
#第三个是为了安装pip需要用到ssl,后面报错会有提到.

[root@linux-01 Python-3.9.0]# make -j4

尝试使用编译安装Python 3.9.0,但是在make过程中报错,报错信息如下:

代码语言:javascript
复制
Could not import runpy module
Traceback (most recent call last):
  File "/root/Python-3.9.0/Lib/runpy.py", line 15, in <module>
    import importlib.util
  File "/root/Python-3.9.0/Lib/importlib/util.py", line 2, in <module>
    from . import abc
  File "/root/Python-3.9.0/Lib/importlib/abc.py", line 17, in <module>
    from typing import Protocol, runtime_checkable
  File "/root/Python-3.9.0/Lib/typing.py", line 21, in <module>
    import collections
SystemError: <built-in function compile> returned NULL without setting an error
generate-posix-vars failed
make[1]: *** [pybuilddir.txt] 错误 1
make[1]: 离开目录“/root/Python-3.9.0”
make: *** [profile-opt] 错误 2

经过查询,导致的原因是:gcc的版本比较低,gcc8.1.0修复了此问题,在使用configure编译安装的时候去掉–enable-optimizations选项就可以,本身是虚拟机环境,就想尝试升级GCC测试下。所以就有了下文中gcc版本升级,升级遇到的报错,看了很多帖子写的不是很完整,做一个纪录。

2. 安装gcc依赖以及gcc

2.1 安装gcc所需要的依赖

编译之前需先安装好GCC的依赖库:gmp、mpfr和mpc。编译还依赖m4等编译工具,如果没有,则在执行configue时会报相应的错误,这时需要先安装好这些编译工具。

2.1.1 安装gmp6.1.0

GMP为“GNU MP Bignum Library”的缩写,是一个GNU开源数学运算库

代码语言:javascript
复制
[root@linux-01 ~]# wget https://mirrors.tuna.tsinghua.edu.cn/gnu/gmp/gmp-6.1.0.tar.bz2
[root@linux-01 ~]# tar xvfj gmp-6.1.0.tar.bz2
[root@linux-01 ~]# cd gmp
[root@linux-01 ~]# ./configure --prefix=/usr/local/gmp6.1.0
[root@linux-01 ~]# make
[root@linux-01 ~]# make install

2.1.2 安装isl

代码语言:javascript
复制
[root@linux-01 ~]# wget https://gcc.gnu.org/pub/gcc/infrastructure/isl-0.18.tar.bz2
[root@linux-01 ~]# tar xvfj isl-0.18.tar.bz2
[root@linux-01 ~]# cd isl
[root@linux-01 ~]# ./configure --prefix=/usr/local/isl0.18  --with-gmp-prefix=/usr/local/gmp6.1.0 
[root@linux-01 ~]# make
[root@linux-01 ~]# make install 

2.1.3 安装mpfr

mpc是GNU的开源复杂数字算法,它依赖gmp和mpfr。

代码语言:javascript
复制
#安装mpfr
[root@linux-01 ~]# wget https://mirrors.tuna.tsinghua.edu.cn/gnu/mpfr/mpfr-3.1.4.tar.bz2
[root@linux-01 ~]# tar xvfj mpfr-3.1.4.tar.bz2
[root@linux-01 ~]# cd mpfr 
[root@linux-01 mpfr]# ./configure --prefix=/usr/local/mpfr3.1.4   --with-gmp=/usr/local/gmp6.1.0
[root@linux-01 ~]# make
[root@linux-01 ~]# make install 

2.1.4 安装mpc

mpfr是一个GNU开源大数运算库,它依赖gmp。

代码语言:javascript
复制
#安装mpc
[root@linux-01 ~]# wget https://mirrors.tuna.tsinghua.edu.cn/gnu/mpc/mpc-1.0.3.tar.gz
[root@linux-01 ~]# tar xvfz mpc-1.0.3.tar.gz
[root@linux-01 mpc]# ./configure --prefix=/usr/local/mpc1.0.3  --with-gmp=/usr/local/gmp6.1.0/   --with-mpfr=/usr/local/mpfr3.1.4/
[root@linux-01 ~]# make && make install

2.2 安装GCC

2.2.1 下载gcc 8.1.0

代码语言:javascript
复制
[root@linux-01 ~]# wget ftp://ftp.mirrorservice.org/sites/sourceware.org/pub/gcc/releases/gcc-8.1.0/gcc-8.1.0.tar.xz
--2020-11-24 18:08:04--  ftp://ftp.mirrorservice.org/sites/sourceware.org/pub/gcc/releases/gcc-8.1.0/gcc-8.1.0.tar.xz
           => “gcc-8.1.0.tar.xz”
正在解析主机 ftp.mirrorservice.org (ftp.mirrorservice.org)... 212.219.56.184, 2001:630:341:12::184
正在连接 ftp.mirrorservice.org (ftp.mirrorservice.org)|212.219.56.184|:21... 已连接。
正在以 anonymous 登录 ... 登录成功!
==> SYST ... 完成。   ==> PWD ... 完成。
==> TYPE I ... 完成。 ==> CWD (1) /sites/sourceware.org/pub/gcc/releases/gcc-8.1.0 ... 完成。
==> SIZE gcc-8.1.0.tar.xz ... 63372320
==> PASV ... 完成。   ==> RETR gcc-8.1.0.tar.xz ... 完成。
长度:63372320 (60M) (非正式数据)

100%[========================================================================================================================================================================>] 63,372,320  3.67MB/s 用时 25s

2020-11-24 18:08:37 (2.40 MB/s) - “gcc-8.1.0.tar.xz” 已保存 [63372320]

[root@linux-01 ~]# tar -xvJf   gcc-8.1.0.tar.xz

[root@linux-01 ~]# cd gcc-8.1.0

2.2.2 编译安装gcc

代码语言:javascript
复制
[root@linux-01 gcc-8.1.0]# ./configure --prefix=/usr/local/gcc8.1.0  --with-gmp=/usr/local/gmp6.1.0 --with-mpfr=/usr/local/mpfr3.1.4/  --with-isl=/usr/local/isl0.18  --with-mpc=/usr/local/mpc1.0.3/

…………
*** This configuration is not supported in the following subdirectories:
     gnattools gotools target-libada target-libhsail-rt target-libgo target-libffi target-liboffloadmic
    (Any other directories should still work fine.)
checking for default BUILD_CONFIG... bootstrap-debug
checking for --enable-vtable-verify... no
/usr/bin/ld: cannot find crt1.o: No such file or directory
/usr/bin/ld: cannot find crti.o: No such file or directory
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-redhat-linux/4.8.5/libgcc_s.so when searching for -lgcc_s
/usr/bin/ld: cannot find -lgcc_s
/usr/bin/ld: cannot find -lc
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-redhat-linux/4.8.5/libgcc_s.so when searching for -lgcc_s
/usr/bin/ld: cannot find -lgcc_s
/usr/bin/ld: cannot find crtn.o: No such file or directory
collect2: error: ld returned 1 exit status
configure: error: I suspect your system does not have 32-bit development libraries (libc and headers). If you have them, rerun configure with --enable-multilib. If you do not have them, and want to build a 64-bit-only compiler, rerun configure with --disable-multilib.
  • 上面的报错信息提示需要添加 --disable-multilib 选项。
代码语言:javascript
复制
[root@linux-01 gcc-8.1.0]# ./configure -enable-checking=release -enable-languages=c,c++ -enable-multilib   --prefix=/usr/local/gcc8.1.0  --with-gmp=/usr/local/gmp6.1.0 --with-mpfr=/usr/local/mpfr3.1.4/  --with-isl=/usr/local/isl0.18  --with-mpc=/usr/local/mpc1.0.3/

[root@linux-01 gcc-8.1.0]#  make -j4

…………
/root/gcc-8.1.0/host-x86_64-pc-linux-gnu/gcc/cc1: error while loading shared libraries: libisl.so.15: cannot open shared object file: No such file or directory
make[3]: *** [s-selftest-c] 错误 1
rm gcc.pod
make[3]: 离开目录“/root/gcc-8.1.0/host-x86_64-pc-linux-gnu/gcc”
make[2]: *** [all-stage1-gcc] 错误 2
make[2]: 离开目录“/root/gcc-8.1.0”
make[1]: *** [stage1-bubble] 错误 2
make[1]: 离开目录“/root/gcc-8.1.0”
make: *** [all] 错误 2
  • 上面的错误通过网络搜索需要修改/etc/ld.so.conf,添加:include /usr/local/lib ,添加完成后使用ldconfig命令
  • ldconfig命令的用途主要是在默认搜寻目录/lib和/usr/lib以及动态库配置文件/etc/ld.so.conf内所列的目录下,搜索出可共享的动态链接库(格式如lib*.so*),进而创建出动态装入程序(ld.so)所需的连接和缓存文件。缓存文件默认
代码语言:javascript
复制
[root@linux-01 gcc-8.1.0]# vim /etc/ld.so.conf
include ld.so.conf.d/*.conf
include /usr/local/lib

[root@linux-01 gcc-8.1.0]# find /usr/local/lib/  -name 'libisl.so.15'
[root@linux-01 gcc-8.1.0]# ldconfig
  • 需要删除host-x86_64-pc-linux-gnu目录重新运行configure,重新运行还是发现有同样的报错,想到用find命令去查找下libisl.so.15这个库文件在哪,发现不是在/usr/local/lib下,而是在isl0.18下面。
代码语言:javascript
复制
[root@linux-01 gcc-8.1.0]# rm -rf host-x86_64-pc-linux-gnu

[root@linux-01 gcc-8.1.0]# find /  -name 'libisl.so.15'
/root/isl-0.18/.libs/libisl.so.15
/usr/local/isl0.18/lib/libisl.so.15

[root@linux-01 gcc-8.1.0]# ln -s /usr/local/isl0.18/lib/libisl.so.15   /usr/lib64/libisl.so.15
  • 添加这个库文件所在的目录到/etc/ld.so.conf再次进行尝试编译。
代码语言:javascript
复制
[root@linux-01 gcc-8.1.0]# vim /etc/ld.so.conf

include ld.so.conf.d/*.conf
include /usr/lib64/

[root@linux-01 gcc-8.1.0]# ldconfig

[root@linux-01 gcc-8.1.0]# ldconfig -v |grep libis
ldconfig: 无法对 /libx32 进行 stat 操作: 没有那个文件或目录
ldconfig: 多次给出路径“/usr/lib”
ldconfig: 多次给出路径“/usr/lib64”
ldconfig: 无法对 /usr/libx32 进行 stat 操作: 没有那个文件或目录
	libisl.so.15 -> libisl.so.15

2.2.3 重新进行编译

代码语言:javascript
复制
[root@linux-01 gcc-8.1.0]# ./configure -enable-checking=release -enable-languages=c,c++ -enable-multilib   --prefix=/usr/local/gcc8.1.0  --with-gmp=/usr/local/gmp6.1.0 --with-mpfr=/usr/local/mpfr3.1.4/  --with-isl=/usr/local/isl0.18  --with-mpc=/usr/local/mpc1.0.3/

[root@linux-01 gcc-8.1.0]#  make -j4

[root@linux-01 gcc-8.1.0]#  make install 

[root@linux-01 gcc-8.1.0]# /usr/local/gcc8.1.0/bin/gcc  -v
使用内建 specs。
COLLECT_GCC=/usr/local/gcc8.1.0/bin/gcc
COLLECT_LTO_WRAPPER=/usr/local/gcc8.1.0/libexec/gcc/x86_64-pc-linux-gnu/8.1.0/lto-wrapper
目标:x86_64-pc-linux-gnu
配置为:./configure -enable-checking=release -enable-languages=c,c++ -disable-multilib --prefix=/usr/local/gcc8.1.0 --with-gmp=/usr/local/gmp6.1.0 --with-mpfr=/usr/local/mpfr3.1.4/ --with-isl=/usr/local/isl0.18 --with-mpc=/usr/local/mpc1.0.3/
线程模型:posix
gcc 版本 8.1.0 (GCC)

3. 安装Python3.9

代码语言:javascript
复制
[root@linux-01 gcc-8.1.0]# cd ../Python-3.9.0/

[root@linux-01 Python-3.9.0]# mv /usr/bin/gcc  /usr/bin/gcc4.8.5


[root@linux-01 Python-3.9.0]# ln -s /usr/local/gcc8.1.0/bin/gcc  /usr/bin/gcc

root@linux-01 Python-3.9.0]# ./configure --prefix=/usr/local/python3 --enable-optimizations --with-ssl
root@linux-01 Python-3.9.0]# make -j4
root@linux-01 Python-3.9.0]# make install 

…………
Traceback (most recent call last):
  File "<frozen zipimport>", line 520, in _get_decompress_func
ModuleNotFoundError: No module named 'zlib'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<frozen zipimport>", line 568, in _get_data
  File "<frozen zipimport>", line 523, in _get_decompress_func
zipimport.ZipImportError: can't decompress data; zlib not available

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<string>", line 6, in <module>
  File "/root/Python-3.9.0/Lib/runpy.py", line 206, in run_module
    mod_name, mod_spec, code = _get_module_details(mod_name)
  File "/root/Python-3.9.0/Lib/runpy.py", line 147, in _get_module_details
    return _get_module_details(pkg_main_name, error)
  File "/root/Python-3.9.0/Lib/runpy.py", line 111, in _get_module_details
    __import__(pkg_name)
  File "<frozen zipimport>", line 241, in load_module
  File "<frozen zipimport>", line 709, in _get_module_code
  File "<frozen zipimport>", line 570, in _get_data
zipimport.ZipImportError: can't decompress data; zlib not available
Traceback (most recent call last):
  File "/root/Python-3.9.0/Lib/runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/root/Python-3.9.0/Lib/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/root/Python-3.9.0/Lib/ensurepip/__main__.py", line 5, in <module>
    sys.exit(ensurepip._main())
  File "/root/Python-3.9.0/Lib/ensurepip/__init__.py", line 210, in _main
    return _bootstrap(
  File "/root/Python-3.9.0/Lib/ensurepip/__init__.py", line 129, in _bootstrap
    return _run_pip(args + [p[0] for p in _PROJECTS], additional_paths)
  File "/root/Python-3.9.0/Lib/ensurepip/__init__.py", line 38, in _run_pip
    return subprocess.run([sys.executable, "-c", code], check=True).returncode
  File "/root/Python-3.9.0/Lib/subprocess.py", line 524, in run
    raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command '['/root/Python-3.9.0/python', '-c', '\nimport runpy\nimport sys\nsys.path = [\'/tmp/tmp4xj56co0/setuptools-49.2.1-py3-none-any.whl\', \'/tmp/tmp4xj56co0/pip-20.2.3-py2.py3-none-any.whl\'] + sys.path\nsys.argv[1:] = [\'install\', \'--no-cache-dir\', \'--no-index\', \'--find-links\', \'/tmp/tmp4xj56co0\', \'--root\', \'/\', \'--upgrade\', \'setuptools\', \'pip\']\nrunpy.run_module("pip", run_name="__main__", alter_sys=True)\n']' returned non-zero exit status 1.
make: *** [install] 错误 1
  • 如上的报错,是少依赖的报错,可以使用yum安装依赖,安装python3所需要的依赖
代码语言:javascript
复制
[root@linux-01 Python-3.9.0]# yum install zlib-devel bzip2 bzip2-devel readline-devel sqlite sqlite-devel openssl-devel xz xz-devel libffi-devel
# 安装过错省略
[root@linux-01 Python-3.9.0]# make install
  • 安装过依赖后,使用make install 进行编译发现python3已经安装完成。
在这里插入图片描述
在这里插入图片描述
  • 图中的警告信息需要把/usr/local/python3/bin/目录添加到系统的环境变量即可。
代码语言:javascript
复制
[root@linux-01 Python-3.9.0]# echo 'expoct PATH=$PATH:/usr/local/python3/bin ' >> /etc/profile
[root@linux-01 Python-3.9.0]# source /etc/profile
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-11-26 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.系统环境
    • 1.1 gcc版本
      • 1.2 centos版本
        • 1.3 下载Python3.9版本
          • 1.4 python安装报错
          • 2. 安装gcc依赖以及gcc
            • 2.1 安装gcc所需要的依赖
              • 2.1.1 安装gmp6.1.0
              • 2.1.2 安装isl
              • 2.1.3 安装mpfr
              • 2.1.4 安装mpc
            • 2.2 安装GCC
              • 2.2.1 下载gcc 8.1.0
              • 2.2.2 编译安装gcc
              • 2.2.3 重新进行编译
            • 3. 安装Python3.9
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档