• 编译安装 Python


    1. 编译安装 Python

    在 CentOS 上因为自带的 Python 版本比较旧,所以要安装高版本的 Python 但同时要求不能影响原来的 python3 命令。

    先不急着动手,可以先看看关于多版本的专题。

    1.1. 专题: 同系统多版本 Python 问题

    非常不建议直接覆盖当前系统的 pythonpython3 命令,Python 每个版本之间多少有点不兼容问题,处理起来非常麻烦,万一系统用到了,就是个大问题。

    Building Python: make install can overwrite or masquerade the python3 binary. make altinstall is therefore recommended instead of make install since it only installs exec_prefix/bin/pythonversion.

    If you want it to install to, for example, /usr/bin instead of the default (/usr/local/bin in ubuntu/debian), then instead of ./configure, type ./configure --prefix=/usr when told to use it in the guide.

    For in your $HOME/bin directory, use --prefix=$HOME.

    If it doesn’t exist, add $HOME/bin to your $PATH like this:

    $ export PATH=$HOME/bin:$PATH
    
    • 1

    This may already be in your .bashrc in ubuntu, and others. If it is, when you next log in, $HOME/bin will be added to your $PATH automatically.

    1.1.1. altinstall bininstall maninstall

    Let’s take a look at the generated Makefile!

    First, the install target:

    install:         altinstall bininstall maninstall
    
    • 1

    It does everything altinstall does, along with bininstall and maninstall

    Here’s bininstall; it just creates the python and other symbolic links.

    # Install the interpreter by creating a symlink chain:
    #  $(PYTHON) -> python2 -> python$(VERSION))
    # Also create equivalent chains for other installed files
    bininstall:     altbininstall
            -if test -f $(DESTDIR)$(BINDIR)/$(PYTHON) -o -h $(DESTDIR)$(BINDIR)/$(PYTHON); \
            then rm -f $(DESTDIR)$(BINDIR)/$(PYTHON); \
            else true; \
            fi
            (cd $(DESTDIR)$(BINDIR); $(LN) -s python2$(EXE) $(PYTHON))
            -rm -f $(DESTDIR)$(BINDIR)/python2$(EXE)
            (cd $(DESTDIR)$(BINDIR); $(LN) -s python$(VERSION)$(EXE) python2$(EXE))
            ... (More links created)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    And here’s maninstall, it just creates “unversioned” links to the Python manual pages.

    # Install the unversioned manual pages
    maninstall:     altmaninstall
            -rm -f $(DESTDIR)$(MANDIR)/man1/python2.1
            (cd $(DESTDIR)$(MANDIR)/man1; $(LN) -s python$(VERSION).1 python2.1)
            -rm -f $(DESTDIR)$(MANDIR)/man1/python.1
            (cd $(DESTDIR)$(MANDIR)/man1; $(LN) -s python2.1 python.1)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    TLDR: altinstall skips creating the python link and the manual pages links, install will hide the system binaries and manual pages.

    1.1.2. 兼容性修正

    1.1.2.1. Redhat/CentOS 系

    一般在 /usr/bin/ 下放一个链接,链接到 /usr/local/bin/ 下面:

    $ ln -s /usr/local/bin/pip    /usr/bin/pip
    $ ln -s /usr/local/bin/pip3   /usr/bin/pip3
    $ ln -s /usr/local/bin/pip3.9 /usr/local/bin/pip3
    
    • 1
    • 2
    • 3

    1.1.2.2. Debian/Ubuntu 系

    1.1.3. 快速虚拟化环境工具

    1.2. Step 1: Install Python Dependencies

    Login to your CentOS 8 / CentOS 7 system as root or user with sudo privileges.

    Then do system update

    sudo yum -y install epel-release
    sudo yum -y update
    
    • 1
    • 2

    Reboot after the upgrade before you continue to install dependencies

    sudo reboot
    
    • 1

    Install required software development tools required to build Python 3.9 on CentOS 8 / CentOS 7:

    sudo yum groupinstall "Development Tools" -y
    sudo yum install openssl-devel libffi-devel bzip2-devel -y
    
    • 1
    • 2

    Confirm gcc is available:

    $ gcc --version
    gcc (GCC) 8.5.0 20210514 (Red Hat 8.5.0-4)
    Copyright (C) 2018 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    
    • 1
    • 2
    • 3
    • 4
    • 5

    1.3. Step 2: Download latest Python 3.9 Archive

    Ensure wget is installed:

    sudo yum install wget -y
    
    • 1

    Use wget to download the latest Python 3.9 release.

    wget https://www.python.org/ftp/python/3.9.10/Python-3.9.10.tgz
    
    • 1

    Extract the archive file using tar:

    tar xvf Python-3.9.10.tgz
    
    • 1

    Switch to the directory created from the file extraction:

    cd Python-3.9*/
    
    • 1

    1.4. Step 3: Install Python 3.9 on CentOS 8 / CentOS 7

    Run the command below to configure Python installation.

    ./configure --enable-optimizations
    
    • 1

    Build Python 3.9 on CentOS 8 / CentOS 7:

    sudo make altinstall # altinstall is important, DO NOT use `make install`.
    
    • 1

    Python 安装时 make installmake altinstall 的区别: altinstall skips creating the python link and the manual pages links. altinstall 跳过创建 Python 链接和手册页链接的操作。如果使用 make install,在系统中将会有两个不同版本的 Python 在 /usr/bin 目录中,这将会导致很多问题。

    Be patient as this takes quite some time depending on number of CPU cores in your system.

    Check Python 3.9 installation on CentOS 8 / CentOS 7. Run below command to confirm successful installation of Python 3.9 on CentOS 8 / CentOS 7:

    $ python3.9 --version
    Python 3.9.10
    
    • 1
    • 2

    Pip3.9 must have been installed as well:

    $ pip3.9 --version
    pip 21.2.4 from /usr/local/lib/python3.9/site-packages/pip (python 3.9)
    
    • 1
    • 2

    Upgrade pip

    $ /usr/local/bin/python3.9 -m pip install --upgrade pip
    $ pip3.9 --version
    
    • 1
    • 2
  • 相关阅读:
    算法基础——二分检索
    基于PCA的特征提取和两级匹配的激光雷达SLAM(翻译)
    C++【类型转换】
    react之组件与JSX
    git commit 提交信息规范
    byte buddy字节码增强——输出方法执行时间
    聊 · Flutter
    uniapp中git忽略node_modules,unpackage文件
    linux挖矿病毒kthreaddk横行,如何灭掉它?
    大模型训练之加速篇 -> peft(Lora) -> accelerator -> deepspeed (Zero)
  • 原文地址:https://blog.csdn.net/wan212000/article/details/126015601