ubuntu system: Install python on ubuntu22.04 and set system environment variables

Install python3.9.0 at once

$~ sudo apt update  #更新软件源

$~ sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev 

$~ wget https://www.python.org/ftp/python/3.9.0/Python-3.9.0.tgz     
#官网下载压缩包, 要换版本,只需要修改3.9.0

$~ sudo tar -xvzf Python-3.9.0.tgz  #解压

$~ cd Python-3.9.0   #进入python文件夹

$~ ./configure --with-ssl  prefix=/usr/local/python39   #指定python安装位置

$~ make  #编译

$~ sudo make install #安装

2. Add the python interpreter to the environment variable

1. Enter /usr/local/python39/bin,

cd /usr/local/python39/bin
sudo cp python3.9 python
sudo cp pip3 pip
#在bin目录下复制python3.9 名字改为python
#在bin目录下复制pip3 名字改为pip

2. You can query the directories added to the system environment, the command is as follows:

echo $PATH

/home/xxx/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin

2.1. Method 1

1. Copy the python in /usr/local/python39/bin to /usr/local/bin

sudo cp /usr/local/python39/bin/python /usr/local/bin/python

Notice:

1. Because the system puts the /usr/local/bin directory in the system environment by default, all python commands can be found.

2. However, /usr/local/bin is not put at the front, so it may be intercepted by other configured directories

2.2. Method 2.

1. Add the /usr/local/python39/bin directory to the system environment

sudo vi /etc/profile

#在最后添加
PATH=/usr/local/python39/bin:$PATH 

#添加后,就重启虚拟机或服务器

Notice:

1. Here is to add the /usr/local/python39/bin directory to the system environment,

2. Because the configuration file is modified, the server or virtual machine needs to be restarted

Test, enter python in the terminal

$ python
Python 3.9.0 (default,Apr 27 2023,17:24:11)

test-pip

pip  list
pip3 list

Note: In many cases, the two methods will be done together, probably for stability.

Guess you like

Origin blog.csdn.net/weixin_46371752/article/details/130412857