menu Chancel's blog
rss_feed
Chancel's blog
秋雨一何碧,山色倚晴空。

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

作者:Chancel Yang, 时间:2022 Sep 09, 阅读:77

安装编译所需程序

Ubuntu2204

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

Cent7 OS

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

在Python官网选择合适的安装包,我这里选择的是Python3.7.2

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 中,避免使用 sudo 权限

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任意版本时使用

#author:Chancel.Yang
#date:2022-08-12

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

mkdir -p $python_install_path/bin
cd $python_install_path

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

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

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

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

rm -rf $python_install_path/Python-$1

使用方法如下

bash install-py.sh 3.7.2

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

account_circle
email
web_asset
textsms

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

还没有可以显示的留言...
[[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)]]