阿里云服务器配置

下载最新的Python包

sudo wget --no-check-certificate https://www.python.org/ftp/python/3.7.3/Python-3.7.3.tgz

解压安装包

tar -zxvf Python-3.7.3.tgz

安装源码编译所需的各种依赖库:(Ubuntu16.04环境下)

sudo apt-get install zlib1g-dev libbz2-dev libssl-dev libncurses5-dev libsqlite3-dev libreadline-dev tk-dev libgdbm-dev libdb-dev libpcap-dev xz-utils libexpat1-dev liblzma-dev libffi-dev libc6-dev

配置安装位置 配置优化

cd Python-3.7.3
sudo ./configure --prefix=/usr/local/python3.7.3 --enable-optimizations

编译

sudo make -j 4

在这里的make -j 4可以优化多核、多线程的编译过程。 用make -j带一个参数,可以把项目在进行并行编译,比如在我购买的这样一台双核的服务器上,完全可以用make -j 4,让make最多允许4个编译命令同时执行,这样可以更有效的利用CPU资源。

安装

sudo make install

添加环境变量

sudo PATH=$PATH:$HOME/bin:/usr/local/python3.7.3/bin

查看python命令指向

ls -l /usr/bin | grep python

切换Python版本

sudo su root
mv /usr/bin/python3 /usr/bin/python3.bak
ln -s /usr/local/python-3.7.3/bin/python3.7 /usr/bin/python3
mv /usr/local/bin/pip /usr/local/bin/pip.bak
ln -s /usr/local/python-3.7.3/bin/pip3.7 /usr/local/bin/pip
update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
update-alternatives --install /usr/bin/python python /usr/bin/python3.5 2
update-alternatives --install /usr/bin/python python /usr/bin/python3 3

删除原来的软连接,添加python3和pip3的软链接

rm -rf /usr/bin/python3
rm -rf /usr/bin/pip3
ln -s /usr/local/python3.7.3/bin/python3.7 /usr/bin/python3
ln -s /usr/local/python3.7.3/bin/pip3.7 /usr/bin/pip3