编译安装gcc8

  1. 下载源代码, 并解压。

    1
    2
    
    wget ftp://ftp.lip6.fr/pub/gcc/releases/gcc-8.3.0/gcc-8.3.0.tar.gz
    tar -xvf gcc-8.3.0.tar.gz
  2. 安装相关依赖

    1
    2
    3
    
    cd gcc-8.3.0
    ./contrib/download_prerequisites
    sudo apt install texinfo bison flex
  3. 编译

    1
    2
    3
    4
    5
    
    mkdir build
    cd build
    ../configure --prefix=/usr/local/gcc --enable-bootstrap  --enable-checking=release --enable-languages=c,c++ --disable-multilib
    #4是我机器的cpu核心数
    make -j4
  4. 编译安装并配置环境变量

    1
    2
    3
    
    sudo make install
    echo "PATH=/usr/local/gcc/bin:$PATH" >> ~/.zshrc
    source ~/.zshrc
  5. 查看版本

    1
    
    gcc -v

编译安装python3.7

  1. 安装相关依赖

    1
    2
    
    sudo apt update
    sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev wget
  2. 下载源码并解压

    1
    2
    
    wget https://www.python.org/ftp/python/3.7.3/Python-3.7.3.tgz
    tar -xvf Python-3.7.3.tgz
  3. 编译安装

    1
    2
    3
    4
    5
    6
    7
    
    # 如果编译安装了gcc就要使用下面的export, debian8版本需要
    export LD_LIBRARY_PATH=/usr/local/gcc/lib/:/usr/local/gcc/lib64:$LD_LIBRARY_PATH
    cd Python-3.7.1
    ./configure --enable-optimizations
    make -j 4
    # 不会直接覆盖原来的python3版本
    sudo make altinstall
  4. 新建软链接, 使系统默认使用python3

    1
    2
    3
    4
    
    rm /usr/local/bin/pip
    rm /usr/local/bin/python
    ln -s /usr/local/bin/python3.7 /usr/local/bin/python
    ln -s /usr/local/bin/pip3 /usr/local/bin/pip

注意

  1. 开启–enable-optimizations时编译会报下面错误SystemError: <built-in function compile> returned NULL without setting an error, 如果是debian8的话, 由于gcc版本太低导致的。所以要编译安装gcc8
  2. pip安装软件时报错ModuleNotFoundError: No module named '_ctypes', 这个问题主要是要安装libffi-dev依赖,如果没有安装的话,要先安装,再重新编译安装一次python.

参考

  1. Building Python 3.7 from source on Ubuntu and Debian Linux
  2. Ubuntu16.04 编译安装gcc8.2.0
  3. SystemError: returned NULL without setting an error
  4. How to Install Python 3.7 on Debian 9