SoFunction
Updated on 2024-10-28

Detailed explanation of Ubuntu 16.04 install Python 3.7 and its pip3 and switch to default version

0. Configure the dependency environment, if you don't do this step there may be some problems

There may be extra spaces in the middle, remove them and run it again, usually it can be installed successfully, if not you can first update the sudo apt-get update
sudo apt-get install zlib1g-dev libbz2-dev libssl-dev libncurses5-dev libsqlite3-dev 
libreadline-dev tk-dev libgdbm-dev libdb-dev libpcap-dev xz-utils libexpat1-dev 
liblzma-dev libffi-dev libc6-dev

1. Go directly to the official website to download the version you want

2. Unzip the downloaded package, before that you can mv your package to a specified location to unzip it

jianjiacheng@J-computer:~$ tar zxvf Python-3.7.1

3. Enter the decompression directory

jianjiacheng@J-computer:~$ cd Python-3.7.1/

4. Create a directory for installation

sudo mkdir -p /usr/local/python3

5. Compile and install

This step should be followed by --enable-optimizations, which will automatically install pip3 and optimize the configuration.
# ./configure --prefix=/usr/local/python3 --enable-optimizations
# make
# sudo make install

6. Remove soft links

First run to check the version, if there is proof that the soft link already exists, you need to delete the previous before re-establishing the
// This indicates that I have a softlink for python3 but not for pip3 so I need to delete the python3 softlink and recreate it.
jianjiacheng@J-computer:~/Python-3.7.1$ python3 -V
Python 3.5.2

jianjiacheng@J-computer:~$ pip3 -V
bash: /usr/lib/command-not-found: /usr/bin/python3: bad interpreter: No such file or directory
rm -rf /usr/bin/python3
rm -rf /usr/bin/pip3

7. Create a new soft link to python 3.7

#add softlinks for python3
ln -s /usr/local/python3/bin/python3.7 /usr/bin/python3
# Add pip3 softlink
ln -s /usr/local/python3/bin/pip3.7 /usr/bin/pip3

8. Detect the version

jianjiacheng@J-computer:~$ python3 -V
Python 3.7.1
jianjiacheng@J-computer:~$ pip3 -V
pip 10.0.1 from /usr/local/python3/lib/python3.7/site-packages/pip (python 3.7)

This is the whole content of this article.