作者:E4b9a6, 创建:2021-06-15, 字数:3483, 已阅:143, 最后更新:2024-03-19
首先安装编译所需的依赖软件
Debian
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
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
中,避免使用 root
权限
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任意版本时使用
#!/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 sqlite3 libsqlite3-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
你可以这样使用这个脚本
./script.sh --version=3.9.7 --path=/path/to/install --install-deps
如果下载需要使用网络代理,也可以这样使用网络代理
./script.sh --version=3.9.7 --path=/path/to/install --install-deps --proxy=http://127.0.0.1:1080