返回文章列表文章索引阅读全文
STARBLOG / 阅读手记
Linux

Compiling and Installing Python 3.6 on Linux, Theoretically Universal Across Distros

Preface Recently I deployed a Django project to a new server, and this server was given a CentOS 6 system. The official repos are very old, and ops even broke the repos, so there was no way to install Python 3.6 and pip from the repos. I had no choice but to use the most primitive method: compile from source! Notes If Python 2 is already installed on the machine, try not to touch it. Just use python3 to run Python scripts, because some programs may depend on the current Python 2 environment. Don't mess with the existing Python 2 environment! Downloaded from the official site

曦远 (DealiAxy)约 11 分钟
目录13 个章节

Preface

Recently I deployed a Django project to a new server, and this server was given a CentOS 6 system. The official repos are very old, and ops even broke the repos, so there was no way to install Python 3.6 and pip from the repos. I had no choice but to use the most primitive method: compile from source!

Notes

If Python 2 is already installed on the machine, try not to touch it. Just use python3 to run Python scripts, because some programs may depend on the current Python 2 environment. Don't mess with the existing Python 2 environment! The Python source package downloaded from the official site already includes the pip tool. If you still don't have it after installation, refer to the method at the end of this article to install pip manually!

Install the dependency environment

Install dependencies

Compiling from source also requires some libraries, which all need to be installed.

yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel

Download the Python 3.6 source package

Visit: https://www.python.org/downloads/ to check the version you need to install. Mine here is 3.6.

wget https://www.python.org/ftp/python/3.6.7/Python-3.6.7.tgz

Create the installation directory

I personally prefer installing to /usr/local/python3 (the exact install location is a matter of personal preference) Create the directory:

 mkdir -p /usr/local/python3

Extract the source

Extract the downloaded Python-3.x.x.tgz package

The exact package name differs depending on the specific Python version you downloaded. For example, I downloaded Python 3.6.7, so here it's Python-3.6.7.tgz

tar -zxvf Python-3.6.7.tgz

Compile the source

Enter the extracted directory and compile and install.

cd Python-3.6.7
./configure --prefix=/usr/local/python3
make
make install
ln -s /usr/local/python3/bin/python3 /usr/bin/python3

And add /usr/local/python3/bin to PATH

Edit the .bash_profile file

nano ~/.bash_profile

Change the contents to:

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin:/usr/local/python3/bin
export PATH

After making the changes, remember to run the following command to make the previous modification take effect:

source ~/.bash_profile

Check whether Python 3 and pip3 are working properly:

python3 -V
pip3 -V

If pip is not accessible, create a symlink for pip3 as well

ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3

Manually install pip and setuptools

You need to install setuptools first before installing pip

For the specific version to download, please go to the official site yourself and copy the latest download link.

wget --no-check-certificate  https://pypi.python.org/packages/source/s/setuptools/setuptools-19.6.tar.gz#md5=c607dd118eae682c44ed146367a17e26
tar -zxvf setuptools-19.6.tar.gz
cd setuptools-19.6
python3 setup.py build
python3 setup.py install

If you didn't set up the environment properly earlier, you're in for a rough time: Error: RuntimeError: Compression requires the (missing) zlib module We need to install the zlib-devel package on Linux to add support.

yum install zlib-devel

You need to recompile and reinstall Python 3.6.

cd python3.6.7
make && make install

Another long compile-and-install process.

Reinstall setuptools:

python3 setup.py build
python3 setup.py install

Install pip

Use the download link and version provided on the official website.

wget --no-check-certificate  https://pypi.python.org/packages/source/p/pip/pip-8.0.2.tar.gz#md5=3a73c4188f8dbad6a1e6f6d44d117eeb
tar -zxvf pip-8.0.2.tar.gz
cd pip-8.0.2
python3 setup.py build
python3 setup.py install

If you run into this error:

pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.

You need to install the dependencies:

yum install openssl
yum install openssl-devel

Then recompile Python 3.6

cd python3.6.7
make && make install

_EOF_

Compiling and Installing Python 3.6 on Linux, Theoretically Universal Across Distros

本文作者:
程序设计实验室
版权声明:
本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!

相关文章

讨论

共 0 条

良言一句三冬暖,恶语伤人六月寒。

加载评论中…