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 许可协议。转载请注明出处!
相关文章
CentOS6安装Nginx,Linux编译安装Nginx
前言 最近笔者把一个django项目部署到一个新的服务器上,而这个服务器给的是CentOS6版本的系统,官方源很旧,yum管理器被运维搞坏了,没办法加载epel源,所以想要用nginx就只能自己编译安装咯~ 下载nginx代码 首先到Nginx的官网下载安装文件。 链接:http://nginx.org/ 我用的版本是:nginx-1.12.1.tar.gz 安装编译所需环境 安装gcc 安装 n
2022-06-17 · Linux · 约 1 分钟
为团队配置Linux环境,简单高效的项目共享方案
配置Linux多用户共享环境?无非是组权限和SGID设置。SSH公钥认证,共享conda环境,复制进度用rsync。这些操作,简单高效,但何必如此折腾?
2024-08-11 · Linux · 约 1 分钟
什么年代了怎么还在用bash啊?现代化shell开箱体验: fish, nu, elvish
Fish开箱即用,智能补全和颜色真不错?Nushell像SQL处理数据,万物皆对象,但学曲线陡?Elvish像编程语言,逻辑严密,社区小众?何必折腾,直接换Shell?
2026-03-17 · Linux · 约 1 分钟
Config-Software-mirrors-source-for-OpenSuSE-42-3
I just wrote a shell file to finish it. # Disable the system source sudo zypper mr -da # Add aliyun mirrors sudo zypper addrepo -f http://mirrors.aliyun.com/opensuse/update/leap/42.3/non-oss/ openSUS
2022-06-17 · Linux · 约 1 分钟
讨论
共 0 条加载评论中…