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
目录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
Create a symlink for python3
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 许可协议。转载请注明出处!
相关文章
硬盘空间消失之谜:Linux 服务器存储排查与优化全过程
最近我发现服务器磁盘空间突然占用异常,经过排查,罪魁祸首竟然是 Docker 容器的日志文件!
2024-12-17 · Linux · 约 1 分钟
什么年代了怎么还在用bash啊?现代化shell开箱体验: fish, nu, elvish
Fish开箱即用,智能补全和颜色真不错?Nushell像SQL处理数据,万物皆对象,但学曲线陡?Elvish像编程语言,逻辑严密,社区小众?何必折腾,直接换Shell?
2026-03-17 · Linux · 约 1 分钟
Manjaro-安装docker笔记
Manjaro的官方源里面就有docker,直接安装就行。 sudo pacman -S docker 启用docker服务 sudo systemctl start docker 设置开机自启动 sudo systemctl enable docker 这样安装的docker默认是只有root用户才能用,我们需要把当前用户添加到docker用户组,才可以。 # 如果还没有 docker grou
2021-12-18 · Linux · 约 1 分钟
经历分享,发现挖矿木马后,服务器快速备份与重装(腾讯云平台)
服务器又中木马?备份重装一套走起,coscli搞定。一键脚本压缩上传,硬盘镜像SSH传输,安全加固到位。AI时代更需小心,重装系统省心省力。
2026-03-04 · Linux · 约 1 分钟
讨论
共 0 条加载评论中…