ubuntu ordinary users compile and install Python 3 tutorial

1, Background

As we all know, the root user has the supreme power in the linux system and does whatever he wants. So of course, you can't just give people a home root account to do things. Here's the idea of installing and using python by ordinary users. Let's have a look.

2, Preliminary preparation

Update the source and install the corresponding updates

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

Install related compilation AIDS

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

-y means you don't need to confirm. Tell ubuntu you can just install it for me

Source package download

Identify this website, https://www.python.org/downloads/source/ Of course, if you watch carefully, isn't it faster to visit here directly? https://www.python.org/ftp/python/

3, Installation (take 3.8.1 as an example)

Download the corresponding Python source code package. Here we use wget in one step

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

Unzip the downloaded Python source package

tar -zxvf Python-3.8.1.tgz

Here, for the convenience of management, we create a new software in the current user's home directory, indicating the software we want to install. In one step, we will define the target folder for installing python

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

Enter the extracted source directory for compilation and installation

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

Append to current user environment variable

# Open file
vim ~/.bashrc
# Paste the sentence downstairs as the case may be
export PATH=PATH/software/python3.8/bin:$PATH
# Save exit press RSC and enter: wq
# Update to make it effective
source ~/.bashrc

Check it out

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:~$

Here you will find that you have to enter pip3 every time. It's useless to enter pip. You just need to add an alias to the environment variable

# Open file
vim ~/.bashrc

# Post the following words, depending on your personal configuration
alias pip=/home/ataola/software/python3.8/bin/pip3.8

# Save exit press RSC and enter: wq

# Update to make it effective
source ~/.bashrc

That's good

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:~$

I'm not happy. Step by step upstairs. I posted one here bashrc, you just watch it change

# ~/.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

After that, this is the domestic environment. The first thing I think of is to change the source. Otherwise, I will wait half dead every time and waste my time. Choose any one of Ali, Douban and Tsinghua source.

# Upgrade pip
pip install --upgrade pip

# without. pip file creation, if any, will be skipped
mkdir ~/.pip

# Edit profile
vim ~/.pip/pip.conf

Paste the following

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

Note that http is used here, because -- with SSL was not selected at the beginning of compilation. Of course, you can recompile with this SSL/ Configure -- prefix = "/ home / ataola / software / Python 3.8" -- enable optimizations -- with SSL, https can be used later

Try the installation speed and take off

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:~$

4, Use

Enter Python and do whatever you want

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, that is, there are many package libraries. Sometimes we may expect the combination of a and b to do an experiment or development. Sometimes we expect the combination of a and c, so we recommend you use virtualenv

# Install virtualenv
pip install virtualenv

# Create project file and enter
mkdir play-py-a && cd play-py-a

## Create virtual environment
python -m venv venv

Details are as follows:

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$
See here for details, https://virtualenv.pypa.io/en/latest/

Added by rupertbj on Wed, 15 Dec 2021 13:16:48 +0200