Linux compile and install Python and pip source change tutorial

Python source code compilation and installation, and pip source change tutorial.

Python series articles: https://blog.zeruns.tech/category/Python/

Compile and install Python

1. Depending on the environment installation

If it is a centos system, replace the apt of the following command with yum

If you log in with the root user, you can remove the sudo of the following command

sudo apt -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel libffi-devel gcc make

2. Download and decompress the Python source package

You can go to the Python official website to select the version source package you want to download, and then replace the download address and file name of the following command. The Python version downloaded by the following command is 3.10. If you don’t need to change to another version, just execute it directly.

Python official website: https://url.zeruns.tech/o7D5h

# 下载源码压缩包
wget https://img.zeruns.tech/down/source/Python-3.10.6.tgz

# 解压
tar -xf Python-3.10.6.tgz

3. Compile and install

# 进入源码目录
cd Python-3.10.6

# 配置,下面的 /opt/python310 是python安装目录,可以自己更改
./configure --prefix=/opt/python310

# 编译和安装
make
sudo make install

# 检查是否成功安装,如果显示出版本就是成功安装
python3.10 -V
pip3.10 -V

This step is compilation, and most source code packages are compiled through this step (of course, some software written in perl or python needs to call perl or python to compile).

If an error occurs during the make process, you have to write down the error code (not just the last line),
and then you can submit a bugreport to the developer (usually there is a submission address in INSTALL),
or your system has less dependencies Libraries, etc., these need to carefully study the error code by yourself.

The function of make is to start compiling the source code and provide some functions. These functions are provided by its Makefile setting file. For example, make install generally
means to install, and make uninstall means to uninstall. Without parameters, it is the default source. The code compiles.

make is a control program for automatic compilation in the Linux development suite.
It uses the compilation specification written in the Makefile to automatically call gcc, ld and run some required programs to compile the program.
In general, the Makefile control code used by him is generated by the configure script according to the given parameters and system environment.

This command is used to install (of course, some software needs to run make check or make test first for some tests),
this step generally requires you to have root privileges (because you need to write files to the system)

4. Configure environment variables

# 安装nano编辑器并编辑环境变量配置文件
sudo apt install nano && sudo nano /etc/profile
# 在最后一行输入下面这句话,其中的 /opt/python310 是你的python安装目录,可以更改,输入完成后按 Ctrl+O 来保存,然后按 Ctrl+X 退出编辑器
PATH="/opt/python310/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin"

# 刷新配置使之生效
source /etc/profile

5. Test whether it is in normal use

pip change source

The default pip source server is foreign, and the speed is very slow. The following is the mirror source replaced by the University of Science and Technology of China.

# 创建.pip文件夹
mkdir ~/.pip
# 编辑pip配置文件
nano ~/.pip/pip.conf
# 在文件中添加如下内容并保存:
[global]
index-url = https://pypi.mirrors.ustc.edu.cn/simple/
[install]
trusted-host = https://pypi.mirrors.ustc.edu.cn/

If the pip version is greater than 10, you can directly execute the following command to change the source, without the troublesome operation above

pip3 config set global.index-url https://pypi.mirrors.ustc.edu.cn/simple/

Install requests test to see the installation speed

pip3 install requests

recommended reading

Guess you like

Origin blog.csdn.net/u012513463/article/details/126640200