menu Chancel's blog
rss_feed
Chancel's blog
有善始者实繁,能克终者盖寡。

在Linux下手动编译安装指定的Python版本

作者:Chancel Yang, 创建:2021-06-15, 字数:3459, 已阅:112, 最后更新:2024-03-19

首先安装编译所需的依赖软件

Debian

Bash
sudo apt -y install git gcc libgdbm-dev make patch libssl-dev libsqlite3-dev libbz2-dev libreadline-dev build-essential zlib1g-dev openssl libffi-dev

Cent7 OS

Bash
sudo yum -y install wget xz tar gcc make tk-devel sqlite-devel zlib-devel readline-devel openssl-devel curl-devel tk-devel gdbm-devel xz-devel bzip2-devel

Alpine

Bash
sudo apk add wget curl vim git build-base openssl-dev zlib-dev libffi-dev make automake gcc g++ subversion python3-dev

其他操作系统请自行查询依赖插件,然后前往Python官网选择合适的安装包,我这里选择的是Python3.7.2

Bash
mkdir /tmp/python3.7.2 && cd /tmp/python3.7.2
wget https://www.python.org/ftp/python/3.7.2/Python-3.7.2.tgz
tar -zxvf Python-3.7.2.tar.gz && cd Python-3.7.2

选择Python的安装目录并编译,我这里选择安装在 $HOME/apps/pythons 中,避免使用 root 权限

Bash
mkdir -p $HOME/apps/pythons/3.7.2/lib
./configure --prefix=$HOME/apps/pythons/3.7.2 --enable-shared LDFLAGS="-Wl,-rpath $HOME/apps/pythons/3.7.2/lib" 
make
make install

将以上编译步骤制作成一个简易的bash文件,可供安装Python任意版本时使用

Bash
#!/bin/bash
#author:Chancel.Yang
#date:2022-08-11

# parse arguments
install_deps=false
for arg in "$@"
do
    case $arg in
        --version=*)
        version="${arg#*=}"
        shift
        ;;  
        --path=*)
        python_install_path="${arg#*=}"
        shift
        ;;  
        --proxy=*)
        proxy="${arg#*=}"
        wget_args="--proxy=$proxy"
        shift
        ;;  
        --install-deps)
        install_deps=true
        shift
        ;;  
    esac
done

# check arguments
if [[ -z "$version" ]]
then
    echo "Error: python version is not specified"
    exit 1
fi

if [[ -z "$python_install_path" ]]
then
    python_install_path=$HOME'/apps/pythons'
fi

# install dependencies
if [[ "$install_deps" == true ]]
then
    os_release=$(lsb_release -is)
    case $os_release in
        Ubuntu)
        sudo apt -y install git gcc libgdbm-dev make patch libssl-dev libsqlite3-dev libbz2-dev libreadline-dev build-essential zlib1g-dev openssl libffi-dev
        ;;
        CentOS)
        sudo yum -y install wget xz tar gcc make tk-devel sqlite-devel zlib-devel readline-devel openssl-devel curl-devel tk-devel gdbm-devel xz-devel bzip2-devel
        ;;
        Alpine)
        sudo apk add wget curl vim git build-base openssl-dev zlib-dev libffi-dev make automake gcc g++ subversion python3-dev
        ;;
        *)
        echo "Error: unsupported OS release"
        exit 1
        ;;
    esac
fi

# install python
mkdir -p $python_install_path/bin
cd $python_install_path

# install before clean all files
rm -f Python-$version.tgz
rm -rf Python-$version
rm -rf $python_install_path/$version

wget $wget_args --no-check-certificate https://www.python.org/ftp/python/$version/Python-$version.tgz
tar -zxvf Python-$version.tgz
rm -f Python-$version.tgz
cd Python-$version

mkdir -p $python_install_path/$version/lib
./configure --prefix=$python_install_path/$version --enable-shared LDFLAGS="-Wl,-rpath $python_install_path/$version/lib" 
make
make install

ln -s $python_install_path/$version/bin/python3 -s $python_install_path/bin/python$version
ln -s $python_install_path/$version/bin/pip3 -s $python_install_path/bin/pip$version

rm -rf $python_install_path/Python-$version

你可以这样使用这个脚本

Bash
./script.sh --version=3.9.7 --path=/path/to/install --install-deps

如果下载需要使用网络代理,也可以这样使用网络代理

Bash
./script.sh --version=3.9.7 --path=/path/to/install --install-deps --proxy=http://127.0.0.1:1080

[[replyMessage== null?"发表评论":"发表评论 @ " + replyMessage.m_author]]

account_circle
email
web_asset
textsms

评论列表([[messageResponse.total]])

还没有可以显示的留言...
gravatar
[[messageItem.m_author]] [[messageItem.m_author]]
[[messageItem.create_time]]
[[getEnviron(messageItem.m_environ)]]
[[subMessage.m_author]] [[subMessage.m_author]] @ [[subMessage.parent_message.m_author]] [[subMessage.parent_message.m_author]]
[[subMessage.create_time]]
[[getEnviron(messageItem.m_environ)]]