Centos7 Python3.10 完整环境搭建

目录


概要

拉了一个centos7+jdk18的Docker 镜像,基础环境可以说是精简的很,搭建 Python 3.10 这一套完整可用的环境陆陆续续搞了3天,最后终于可以愉快的跑起 pytest 了。

以下步骤我在Linux上跑了一遍,在 Docker 内又跑了一遍,都完美成功!

所以不论你是直接在 Linux 上安装还是在镜像里安装,都可以参考哦。大家共勉~

安装清单

  • openssl-1.1.1
  • python-3.10.5
  • setuptools-65.3.0
  • pip-1.5.4
  • vim (不必需,但有用)
  • sudo(不必需,但有用)

趟过的坑

  • python make 不通过,SSL module is not available、函数xxx、找不到 libssl.a,libcrypto.a
  • pip 不能用
  • pip install 报错 SSL 不可用、使用 requests 库SSL 不可用 
  • 等等

安装步骤

一定要先安装 openssl,再安装 python,不然后面python make 报错还要再安一遍 python 麻烦

1.安装 openssl-1.1.1

openssl 1.1.1 下载地址 https://www.openssl.org/source/old/1.1.1/

按照百度的文章下载的 OpenSSL 1.1.1-pre8 (beta) 20 Jun 2018 版本,python make 时报错函数什么的,又去百度怎么解决这个问题,掉坑里了,去下载1.1.1x 这种版本就没有问题了

# 安装 openssl
cd /home
wget https://www.openssl.org/source/openssl-1.1.1p.tar.gz
tar -xzvf openssl-1.1.1p.tar.gz 
cd /home/openssl-1.1.1p
./config --prefix=/home/openssl
./config -t
make
make install
# 安装动态链接库
./config shared --prefix=/home/openssl
make clean
make
make install

# 建立软链接
mv /usr/bin/openssl /usr/bin/openssl_bak
mv /usr/include/openssl /usr/include/openssl_bak
mv /usr/lib64/libssl.so /usr/lib64/libssl.so_bak
ln -s /home/openssl/bin/openssl /usr/bin/openssl
ln -s /home/openssl/include/openssl /usr/include/openssl
ln -s /home/openssl/lib/libssl.so.1.1 /usr/lib64/libssl.so.1.1
ln -s /home/openssl/lib/libcrypto.so.1.1 /usr/lib64/libcrypto.so.1.1

# 查看版本,是否配置成功
openssl version
# OpenSSL 1.1.1p  21 Jun 2022

# python make 时报错找不到 libssl.a、libcrypto.a 文件,其实这两个文件在 /openssl/lib 下面
# 而 python make 时找的是 /openssl/lib/lib ,差了一层,所以我们手动新建 lib文件夹,把那两个文件copy过去
cd /home/openssl/lib
mkdir lib
cp /home/openssl/lib/libcrypto.a ./lib/libcrypto.a
cp /home/openssl/lib/libssl.a ./lib/libssl.a

2.安装 python-3.10.5

下载地址:Index of /ftp/python/

# 安装依赖
yum install -y gcc patch libffi-devel python-devel  zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel

# 安装 python
cd /home
wget https://www.python.org/ftp/python/3.10.5/Python-3.10.5.tgz
tar -zxvf Python-3.10.5.tgz
cd /Python-3.10.5
./configure --prefix=/home --with-openssl=/home/openssl
make
make install
ln -s /home/Python-3.10.5/python /usr/bin/python3

python3 -V

3.安装 pip、setuptools

# 安装 setuptools
cd /home
wget https://files.pythonhosted.org/packages/cc/83/7ea9d9b3a6ff3225aca2fce5e4df373bee7e0a74c539711a4fbfda53374f/setuptools-65.3.0.tar.gz
tar -xf setuptools-65.3.0.tar.gz
cd /home/setuptools-65.3.0
python3 setup.py install



# 安装 pip
cd /home
wget https://pypi.python.org/packages/source/p/pip/pip-1.5.4.tar.gz#md5=834b2904f92d46aaa333267fb1c922bb --no-check-certificate
tar -xf pip-1.5.4.tar.gz
cd /home/pip-1.5.4
python3 setup.py install
# 建立软链接
ln -s /home/bin/pip /usr/bin/pip
ln -s /home/bin/pip3 /usr/bin/pip3
# 查看版本
pip3 -V

Docker 镜像构建(达到真正开箱即用)

我使用的基础镜像是 centos7+jdk18 ,如果不需要 jdk 环境,可以用其他镜像

新建一个用于构建镜像的文件夹:/home/docker

在该文件夹下添加以下文件:

Python-3.10.5.tgz  # 自己下载
pip.conf     # 配置 pip 镜像源的配置文件,代码见下方
Dockerfile   # 构建镜像的文件,代码见下方

pip.conf

[global]
index-url = http://mirrors.aliyun.com/pypi/simple
[install]
trusted-host = mirrors.aliyun.com

Dockerfile 

# 基础镜像
FROM eclipse-temurin:18-jdk-centos7
# python包下载的有点慢,所以我先下载好了放在构建目录下
COPY Python-3.10.5.tgz /home/Python-3.10.5.tgz
# 安装 python 依赖
RUN yum install -y gcc patch libffi-devel python-devel  zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
# 安装 vim,sudo
RUN yum install -y vim
RUN su
RUN yum -y install sudo

WORKDIR /home
RUN tar -zxvf /home/Python-3.10.5.tgz

# 安装 openssl
RUN wget https://www.openssl.org/source/openssl-1.1.1p.tar.gz
RUN tar -xzvf /home/openssl-1.1.1p.tar.gz 
WORKDIR /home/openssl-1.1.1p
RUN ./config --prefix=/home/openssl
RUN ./config -t
RUN make
RUN make install
RUN ./config shared --prefix=/home/openssl
RUN make clean
RUN make
RUN make install

# 建立软链接
RUN mv /usr/bin/openssl /usr/bin/openssl_bak
RUN mv /usr/include/openssl /usr/include/openssl_bak
RUN mv /usr/lib64/libssl.so /usr/lib64/libssl.so_bak
RUN ln -s /home/openssl/bin/openssl /usr/bin/openssl
RUN ln -s /home/openssl/include/openssl /usr/include/openssl
RUN ln -s /home/openssl/lib/libssl.so.1.1 /usr/lib64/libssl.so.1.1
RUN ln -s /home/openssl/lib/libcrypto.so.1.1 /usr/lib64/libcrypto.so.1.1
RUN openssl version

WORKDIR /home/openssl/lib
RUN mkdir lib
RUN cp /home/openssl/lib/libcrypto.a ./lib/libcrypto.a
RUN cp /home/openssl/lib/libssl.a ./lib/libssl.a

# 安装 python
WORKDIR /home/Python-3.10.5
RUN sudo ./configure --prefix=/home --with-openssl=/home/openssl
RUN make
RUN make install
RUN ln -s /home/Python-3.10.5/python /usr/bin/python3

# 安装 setuptools
WORKDIR /home
RUN wget https://files.pythonhosted.org/packages/cc/83/7ea9d9b3a6ff3225aca2fce5e4df373bee7e0a74c539711a4fbfda53374f/setuptools-65.3.0.tar.gz

RUN wget "https://pypi.python.org/packages/source/p/pip/pip-1.5.4.tar.gz#md5=834b2904f92d46aaa333267fb1c922bb" --no-check-certificate
RUN tar -xf pip-1.5.4.tar.gz
RUN tar -xf setuptools-65.3.0.tar.gz

# 安装 pip
WORKDIR /home/pip-1.5.4
RUN python3 setup.py install
WORKDIR /home/setuptools-65.3.0
RUN python3 setup.py install

RUN ln -s /home/bin/pip /usr/bin/pip
RUN ln -s /home/bin/pip3 /usr/bin/pip3

# 添加 pip 源配置文件
RUN mkdir ~/.pip
COPY pip.conf ~/.pip/pip.conf

构建镜像

1.在 /home/docker 目录下执行

docker build -t mypy-3.10 .

构建时间有点长…… 构建完成大概 2.15 G

2.运行容器

docker run -d -it --name mypython-3.10 mypy-3.10 bash

3.进入容器验证环境

docker exec -it mypython-3.10 bash
[root@b573a7f61ca0 /]# python3 -V
Python 3.10.5

[root@b573a7f61ca0 /]# pip3 -V
pip 22.0.4 from /home/lib/python3.10/site-packages/pip (python 3.10)

[root@b573a7f61ca0 /]# openssl version
OpenSSL 1.1.1p  21 Jun 2022

pip3 install requests  也成功

完结撒花 🎉

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
xiaoxingxing的头像xiaoxingxing管理团队
上一篇 2023年6月25日
下一篇 2023年6月25日

相关推荐