ubuntu普通用户编译安装Python3教程

一、背景

众所周知,root用户在linux系统中拥有至高无上的权力,为所欲为,想干嘛就干嘛。所以当然不能随随便便给人家用root账户去搞事情啊,这里就有了用普通用户安装使用python的想法,一起来看看吧。

二、前期准备

更新下源,并安装相应的更新

sudo apt-get install upgrade && apt-get install update

安装相关的编译辅助工具

sudo apt-get install make build-essential zlib1g-dev  -y

-y表示不用你确认了,告诉ubuntu你直接给我装完就行

源码包下载

认准这个网站哦,https://www.python.org/downloads/source/, 当然你再细心观察下,直接访问这里不是更快吗?https://www.python.org/ftp/python/

三、安装(以3.8.1为例)

下载相应的Python源码包,这里我们用wget一步到位

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

对下载下来的Python源码包进行解压

tar -zxvf Python-3.8.1.tgz

这里为了便于管理,我们在当前用户家目录下新建software表示我们要装的软件,一步到位我们就定义好安装python的目标文件夹了

mkdir -p /home/ataola/software/python3.8

进入到解压后的源码目录进行编译安装

cd Python-3.8.1
./configure --prefix="/home/ataola/software/python3.8"
make && make install 

追加到当前用户环境变量

# 打开文件
vim ~/.bashrc
# 视具体情况粘贴楼下这句话
export PATH=PATH/software/python3.8/bin:$PATH
# 保存退出 按RSC, 然后输入:wq
# 更新使其生效
source ~/.bashrc

验证一下吧

ataola@ataola-ubuntu:~$ python3 -V
Python 3.8.1
ataola@ataola-ubuntu:~$ pip3 -V
pip 19.2.3 from /home/ataola/software/python3.8/lib/python3.8/site-packages/pip (python 3.8)
ataola@ataola-ubuntu:~$ python -V
Python 3.8.1
ataola@ataola-ubuntu:~$ pip -V

Command 'pip' not found, but can be installed with:

apt install python3-pip
Please ask your administrator.

ataola@ataola-ubuntu:~$

这里你会发现每次都要输入pip3,输入pip没用。你只需在环境变量加个别名就好

# 打开文件
vim ~/.bashrc

# 贴下面的话,视个人配置
alias pip=/home/ataola/software/python3.8/bin/pip3.8

# 保存退出 按RSC, 然后输入:wq

# 更新使其生效
source ~/.bashrc

这样不就好了嘛

ataola@ataola-ubuntu:~$ python -V
Python 3.8.1
ataola@ataola-ubuntu:~$ python3 -V
Python 3.8.1
ataola@ataola-ubuntu:~$ pip -V
pip 19.2.3 from /home/ataola/software/python3.8/lib/python3.8/site-packages/pip (python 3.8)
ataola@ataola-ubuntu:~$ pip3 -V
pip 19.2.3 from /home/ataola/software/python3.8/lib/python3.8/site-packages/pip (python 3.8)
ataola@ataola-ubuntu:~$

不高兴按楼上一步一步来,我这里贴了一份.bashrc,你直接看着改吧

# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples

# If not running interactively, don't do anything
[ -z "$PS1" ] && return

# don't put duplicate lines in the history. See bash(1) for more options
# ... or force ignoredups and ignorespace
HISTCONTROL=ignoredups:ignorespace

# append to the history file, don't overwrite it
shopt -s histappend

# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
HISTFILESIZE=10000

# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize

# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"

# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
    debian_chroot=$(cat /etc/debian_chroot)
fi

# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
    xterm-color) color_prompt=yes;;
esac

# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
#force_color_prompt=yes

if [ -n "$force_color_prompt" ]; then
    if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
	# We have color support; assume it's compliant with Ecma-48
	# (ISO/IEC-6429). (Lack of such support is extremely rare, and such
	# a case would tend to support setf rather than setaf.)
	color_prompt=yes
    else
	color_prompt=
    fi
fi

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
unset color_prompt force_color_prompt

# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
    PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
    ;;
*)
    ;;
esac

# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
    test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
    alias ls='ls --color=auto'
    #alias dir='dir --color=auto'
    #alias vdir='vdir --color=auto'

    alias grep='grep --color=auto'
    alias fgrep='fgrep --color=auto'
    alias egrep='egrep --color=auto'
fi

# some more ls aliases
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'

# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
#if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
#    . /etc/bash_completion
#fi

搞完以后,这是国内环境啊,首先想到的是换源啊,不然每次都等个半死,时间都浪费了,阿里、豆瓣、清华源随便选一个吧。

# 升级pip
pip install --upgrade pip

# 如果没有.pip文件创建,有就略过
mkdir ~/.pip

# 编辑配置文件
vim ~/.pip/pip.conf

粘贴如下内容

[global]
index-url=http://mirrors.aliyun.com/pypi/simple
[install]
trusted-host=mirrors.aliyun.com

注意这里是走http的,因为最开始编译的时候没有选--with-ssl, 当然你完全可以重新编译一次带上这个ssl./configure --prefix="/home/ataola/software/python3.8" --enable-optimizations --with-ssl, 后面就可以用https了

试一下安装速度,起飞

ataola@ataola-ubuntu:~$ pip install wordcloud
Looking in indexes: http://mirrors.aliyun.com/pypi/simple
Collecting wordcloud
  Downloading http://mirrors.aliyun.com/pypi/packages/50/5b/f588bbd1a7805a742e8d8316740383822b160c4e36b6c14793b57ebe7360/wordcloud-1.8.1-cp38-cp38-manylinux1_x86_64.whl (371 kB)
     |████████████████████████████████| 371 kB 3.2 MB/s
Collecting matplotlib
  Downloading http://mirrors.aliyun.com/pypi/packages/22/1c/d5e535b36c1de4eef4205656e76ac993c6d01b62cfdcac579edb63cd82e0/matplotlib-3.5.1-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl (11.3 MB)
     |████████████████████████████████| 11.3 MB 129 kB/s
Requirement already satisfied: numpy>=1.6.1 in ./software/python3.8/lib/python3.8/site-packages (from wordcloud) (1.21.4)
Collecting pillow
  Downloading http://mirrors.aliyun.com/pypi/packages/fe/f9/cd8b11ec15e27581c5e7affdf04d618d44fa9524dbeb429e5e728df6dc4c/Pillow-8.4.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.1 MB)
     |████████████████████████████████| 3.1 MB 47.9 MB/s
Requirement already satisfied: python-dateutil>=2.7 in ./software/python3.8/lib/python3.8/site-packages (from matplotlib->wordcloud) (2.8.2)
Collecting packaging>=20.0
  Downloading http://mirrors.aliyun.com/pypi/packages/05/8e/8de486cbd03baba4deef4142bd643a3e7bbe954a784dc1bb17142572d127/packaging-21.3-py3-none-any.whl (40 kB)
     |████████████████████████████████| 40 kB 8.1 MB/s
Collecting cycler>=0.10
  Downloading http://mirrors.aliyun.com/pypi/packages/5c/f9/695d6bedebd747e5eb0fe8fad57b72fdf25411273a39791cde838d5a8f51/cycler-0.11.0-py3-none-any.whl (6.4 kB)
Collecting fonttools>=4.22.0
  Downloading http://mirrors.aliyun.com/pypi/packages/7d/05/5c446faf632f1a9c386bd9a56555cbcbe6c71e6b523025a4fbde396e9d39/fonttools-4.28.3-py3-none-any.whl (884 kB)
     |████████████████████████████████| 884 kB 50.6 MB/s
Collecting kiwisolver>=1.0.1
  Downloading http://mirrors.aliyun.com/pypi/packages/d0/2c/c3cba6c1ec54c82bfc56204c712dd2e9b069e2590f78a18841dafbdf2ced/kiwisolver-1.3.2-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl (1.2 MB)
     |████████████████████████████████| 1.2 MB 47.2 MB/s
Collecting pyparsing>=2.2.1
  Downloading http://mirrors.aliyun.com/pypi/packages/a0/34/895006117f6fce0b4de045c87e154ee4a20c68ec0a4c9a36d900888fb6bc/pyparsing-3.0.6-py3-none-any.whl (97 kB)
     |████████████████████████████████| 97 kB 10.9 MB/s
Requirement already satisfied: six>=1.5 in ./software/python3.8/lib/python3.8/site-packages (from python-dateutil>=2.7->matplotlib->wordcloud) (1.16.0)
Installing collected packages: pyparsing, pillow, packaging, kiwisolver, fonttools, cycler, matplotlib, wordcloud
Successfully installed cycler-0.11.0 fonttools-4.28.3 kiwisolver-1.3.2 matplotlib-3.5.1 packaging-21.3 pillow-8.4.0 pyparsing-3.0.6 wordcloud-1.8.1
ataola@ataola-ubuntu:~$

四、使用

输入Python,随便打点什么吧

ataola@ataola-ubuntu:~$ python
Python 3.8.1 (default, Dec 11 2021, 19:38:06)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
>>>

python吧,就是包库多,有的时候我们可能期望 a和b组合做一个实验或者开发, 有的时候期望是a和c组合,那么这里推荐你用virtualenv

# 安装virtualenv
pip install virtualenv

# 创建项目文件并进入
mkdir play-py-a && cd play-py-a

## 创建虚拟环境
python -m venv venv

详细信息如下:

ataola@ataola-ubuntu:~/play-py-a/venv/bin$ ls
activate  activate.csh  activate.fish  Activate.ps1  easy_install  easy_install-3.8  pip  pip3  pip3.8  python  python3  python3.8
ataola@ataola-ubuntu:~/play-py-a/venv/bin$ pwd
/home/ataola/play-py-a/venv/bin
ataola@ataola-ubuntu:~/play-py-a/venv/bin$

具体的可以看这里,https://virtualenv.pypa.io/en/latest/

posted @ 2021-12-13 00:04  ataola  阅读(1243)  评论(0编辑  收藏  举报