centos8 升级 python 版本

  |  

经常遇到给linux升级python的需求,这里记录一下

linux版本:centos8

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# init
# 修改centos8的yum源
find /etc/yum.repos.d/ -type f | xargs -i sed -i 's#http://mirror.centos.org/$contentdir/$releasever/#https://mirrors.aliyun.com/centos/8-stream/#g' {}

sudo sed -i 's/SELINUX=\w*/SELINUX=disabled/' /etc/selinux/config
sudo dnf -y install epel-release
sudo dnf install -y bash-com* bzip2-devel bind-utils cmake dkms dos2unix drpm expect gcc gcc-c++ gdb gdm git kernel-devel kernel-headers libffi libffi-devel lrzsz make ncurses-devel nfs-utils nmap openssl-devel rsync sqlite-devel sysstat tar telnet tmux traceroute tree unzip vim wget yum-utils zlib-devel xz-devel
sudo touch /etc/profile.d/custom.sh

# 安装rust
sudo curl https://sh.rustup.rs -sSf | sh

# 安装python
python_version="3.9.1"
wget https://www.python.org/ftp/python/${python_version}/Python-${python_version}.tar.xz
sudo dnf install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel gcc libffi make libffi-devel -y
tar Jxvf Python-${python_version}.tar.xz
cd Python-${python_version}/
sudo ./configure --prefix=/usr/local/python3 --enable-shared --enable-optimizations --with-ssl
sudo make
sudo make altinstall

sudo bash -c 'echo "/usr/local/python3/lib" > /etc/ld.so.conf.d/python3.conf'
sudo ldconfig
sudo ln -s /usr/local/python3/bin/python3 /usr/bin/python
sudo ln -s /usr/local/python3/bin/pip3 /usr/bin/pip
python -V
pip -V

user="aaron"
home_path="/home/${user}/"
root_path="/root/"

sudo bash -c 'echo -e "[global]\nindex-url = https://pypi.tuna.tsinghua.edu.cn/simple\n" > /etc/pip.conf'
sudo pip install --upgrade pip
sudo pip install ipython
sudo ln -s /usr/local/python3/bin/ipython /usr/bin/
sudo pip install virtualenv
sudo ln -s /usr/local/python3/bin/virtualenv /usr/bin/
sudo pip install virtualenvwrapper
sudo ln -s /usr/local/python3/bin/virtualenvwrapper /usr/bin/
sudo mkdir ${home_path}.virtualenvs ${root_path}.virtualenvs
grep -q '^if \[ ! -d "$HOME/.virtualenvs" ];then$' /etc/profile.d/custom.sh || sudo sh -c 'echo -e "if [ ! -d \"\$HOME/.virtualenvs\" ];then\n mkdir \$HOME/.virtualenvs\nfi" >> /etc/profile.d/custom.sh'
grep -q '^export WORKON_HOME=$HOME/.virtualenvs$' /etc/profile.d/custom.sh || sudo sh -c 'echo -e "export WORKON_HOME=\$HOME/.virtualenvs" >> /etc/profile.d/custom.sh'
grep -q '^export PROJECT_HOME=$HOME/.workspaces$' /etc/profile.d/custom.sh || sudo sh -c 'echo -e "export PROJECT_HOME=\$HOME/.workspaces" >> /etc/profile.d/custom.sh'
grep -q '^source /usr/local/python3/bin/virtualenvwrapper.sh$' /etc/profile.d/custom.sh || sudo sh -c 'echo -e "source /usr/local/python3/bin/virtualenvwrapper.sh" >> /etc/profile.d/custom.sh'

# vimrc
sudo mkdir -p ${home_path}.undodir ${root_path}.undodir ${home_path}.vim ${root_path}.vim
sudo chown -R ${user}: ${home_path}
# 参考vimrc
vim ${home_path}.vim/vimrc
sudo cp ${home_path}.vim/vimrc ${root_path}.vim/
grep -q '^export EDITOR=/usr/bin/vim$' /etc/profile.d/custom.sh && sudo sed -i 's/^export EDITOR.*$/export EDITOR=/usr/bin/vim/' /etc/profile.d/custom.sh || sudo bash -c 'echo "EDITOR=/usr/bin/vim/" >> /etc/profile.d/custom.sh'
grep -q '^if \[ ! -d "$HOME/.undodir" ];then$' /etc/profile.d/custom.sh || sudo sh -c 'echo -e "if [ ! -d \"\$HOME/.undodir\" ];then\n mkdir \$HOME/.undodir\nfi\n" >> /etc/profile.d/custom.sh'
# 参考init.sh
sudo vim /etc/init.sh
ln -s /etc/init.sh ${home_path}.init.sh
sudo ln -s /etc/init.sh ${root_path}.init.sh
grep -q '^if \[ -f ~/.init.sh ]; then$' ${home_path}.bash_profile || sh -c 'echo -e "if [ -f ~/.init.sh ]; then\n . ~/.init.sh login\nfi\n" >> ${home_path}.bash_profile'
sudo grep -q '^if \[ -f ~/.init.sh ]; then$' ${root_path}.bash_profile || sudo sh -c 'echo -e "if [ -f ~/.init.sh ]; then\n . ~/.init.sh login\nfi\n" >> ${root_path}.bash_profile'
# 参考ssh_banner
sudo vim /etc/ssh_banner
sudo grep -q '^Banner .*$' /etc/ssh/sshd_config || sudo sh -c 'echo -e "Banner /etc/ssh_banner" >> /etc/ssh/sshd_config'

# docker
sudo dnf remove docker docker-common docker-selinux docker-engine -y
sudo dnf config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
sudo dnf install device-mapper-persistent-data lvm2 -y
sudo dnf install docker-ce --nobest -y
sudo systemctl enable docker
sudo systemctl start docker
sudo usermod -aG docker ${user}
docker version
docker info

# 注意修改权限
sudo chown -R ${user}: ${home_path}


sudo pip install jupyter
ll ${home_path}.jupyter/jupyter_notebook_config.py
sudo ln -s /usr/local/python3/bin/jupyter-notebook /usr/bin/
jupyter-notebook --generate-config
mkdir -p ${home_path}project/ipython
vim ${home_path}.jupyter/jupyter_notebook_config.py
c.NotebookApp.ip = '192.168.100.10'
c.NotebookApp.notebook_dir = '/home/aaron/project/ipython'
c.NotebookApp.open_browser = False
# jupyter
c.NotebookApp.password = 'argon2:$argon2id$v=19$m=10240,t=10,p=8$ZyZnmpT3p/nmdXpgVV3mQw$JWkHVEJos0t/XQNix9uwvQ'
c.NotebookApp.password_required = True
c.NotebookApp.port = 8880

# 添加系统systemd
sudo vim /usr/lib/systemd/system/jupyter-notebook.service
文章目录