linux安装python(Installing Python for Linux)

虚拟机的环境

环境:centos7

centos6.5 自带的 python 版本是 2.6.6,需要重新安装 2.7;

centos7 自带的 python 版本是 2.7.5

安装依赖环境

sudo yum install -y gcc make cmake zlib-devel bzip2 bzip2-devel readline-devel sqlite sqlite-devel openssl-devel xz xz-devel libffi-devel

(1)Ubuntu/Debian下需安装的依赖:

sudo apt-get install -y gcc make cmake build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev python-openssl

(2)Fedora/CentOS/RHEL(aws ec2)下需安装的依赖:

sudo yum install gcc make cmake zlib-devel bzip2 bzip2-devel readline-devel sqlite sqlite-devel openssl-devel xz xz-devel libffi-devel

安装python

  • 下载Python3安装包
wget https://www.python.org/ftp/python/3.7.2/Python-3.7.2.tgz

查看Python不同版本
2. 创建安装目录
个人习惯安装在/usr/local/python3

mkdir -p /usr/local/python3
  • 解压安装包
tar -zxvf Python-3.7.2.tgz
  • 进入解压后的目录,编译安装(编译安装前需要安装编译器 sudo apt install gcc)
    (1)进入解压后的目录
    (2)执行./configure
./configure --prefix=/usr/local/python3   #/usr/local/python3为安装目录

执行完configure命令后,configure 命令执行完之后,会生成一个 Makefile 文件,这个 Makefile主要是被下一步的 make 命令所使用( Linux 需要按照Makefile 所指定的顺序来构建 (build) 程序组件)。

(3)执行make指令

make

make实际就是编译源代码,并生成执行文件。

(4)再执行make install 命令

make install

make install实际上是把生成的执行文件拷贝到之前configure命令指定的目录/usr/local/python3下。

到这里安装已经结束,下面是配置环境。

  • 建立python3的软链
ln -s /usr/local/python3/Python-3.7.2/bin/python3 /usr/bin/python3
  • 将/usr/local/python3/Python-3.7.2/bin加入PATH
sudo vim /etc/profile

然后在文件末尾添加

export PATH=$PATH:/usr/local/python3/bin

按ESC,输入:wq回车退出。

修改完后,还需要让这个环境变量在配置信息中生效,执行命令:

source /etc/profile

可以让profile文件立即生效。

  • 测试是否安装成功
$ python3 -V
Python 3.7.2
$ pip3 -V
pip 18.1 from /usr/local/python3/lib/python3.7/site-packages/pip (python 3.7)

如果输出如上,证明已成功安装!

如果pip3 -V找不到,可以尝试创建一下pip3的软链接:

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

Virtual machine environment

Environment: centos7

centos6. 5. The built-in Python version is 2.6.6, and 2.7 needs to be reinstalled;

The python version of CentOS 7 is 2.7.5

Installation dependent environment

sudo yum install -y gcc make cmake zlib-devel bzip2 bzip2-devel readline-devel sqlite sqlite-devel openssl-devel xz xz-devel libffi-devel

(1) Dependencies to be installed under Ubuntu / Debian:

sudo apt-get install -y gcc make cmake build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev python-openssl

(2) Dependencies to be installed under Fedora / CentOS / RHEL (AWS EC2):

sudo yum install gcc make cmake zlib-devel bzip2 bzip2-devel readline-devel sqlite sqlite-devel openssl-devel xz xz-devel libffi-devel

安装python

  • 下载Python3安装包
wget https://www.python.org/ftp/python/3.7.2/Python-3.7.2.tgz

View different versions of Python
2. Create installation directory
Personal habits are installed in / usr / local / Python 3

mkdir -p /usr/local/python3
  • Unzip the installation package
tar -zxvf Python-3.7.2.tgz
  • Enter the extracted directory, compile and install (the compiler sudo apt install GCC needs to be installed before compilation and installation)
    (1) Enter the extracted directory
    (2) Execution/ configure
./configure --prefix=/usr/local/python3   #/usr/local/python3为安装目录

After the configure command is executed, a makefile will be generated after the configure command is executed. This makefile is mainly used by the make command in the next step (Linux needs to build program components in the order specified in the makefile).

(3) Execute make instruction

make

Make is actually compiling the source code and generating the execution file.

(4) Then execute the make install command

make install

Make install actually copies the generated executable file to the directory / usr / local / python3 specified by the configure command.

Here the installation is finished. Here is the configuration environment.

  • 建立python3的软链
ln -s /usr/local/python3/Python-3.7.2/bin/python3 /usr/bin/python3
  • 将/usr/local/python3/Python-3.7.2/bin加入PATH
sudo vim /etc/profile

Then add at the end of the file

export PATH=$PATH:/usr/local/python3/bin

Press ESC and enter: WQ enter to exit.

After modification, you also need to make this environment variable effective in the configuration information and execute the command:

source /etc/profile

You can make the profile file take effect immediately.

  • Test whether the installation is successful
$ python3 -V
Python 3.7.2
$ pip3 -V
pip 18.1 from /usr/local/python3/lib/python3.7/site-packages/pip (python 3.7)

If the output is as above, it proves that the installation has been successful!

If PIP3 – V cannot be found, you can try to create a soft link for PIP3:

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