Detailed tutorial of installing python3.6 under Ubuntu 1 ~ 16.04.9 (installing instance on Tencent cloud server)

Native version information
Because I use Tencent ECs, I use crt to connect to the server.

ubuntu@VM-0-13-ubuntu:~$ cat /proc/version
Linux version 4.4.0-130-generic (buildd@lgw01-amd64-039) (gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.9) ) #156-Ubuntu SMP Thu Jun 14 08:53:28 UTC 2018

1. Enter sudo add apt repository PPA: Jonathan / python-3.6

ubuntu@VM-0-13-ubuntu:~$ sudo add-apt-repository ppa:jonathonf/python-3.6
 A plain backport of *just* Python 3.6. System extensions/Python libraries may or may not work.

Don't remove Python 3.5 from your system - it will break.
 More info: https://launchpad.net/~jonathonf/+archive/ubuntu/python-3.6
Press [ENTER] to continue or ctrl-c to cancel adding it

gpg: keyring `/tmp/tmpo9itxaur/secring.gpg' created
gpg: keyring `/tmp/tmpo9itxaur/pubring.gpg' created
gpg: requesting key F06FC659 from hkp server keyserver.ubuntu.com
gpg: /tmp/tmpo9itxaur/trustdb.gpg: trustdb created
gpg: key F06FC659: public key "Launchpad PPA for J Fernyhough" imported
gpg: Total number processed: 1
gpg:               imported: 1  (RSA: 1)
OK

The prompt "ok" is ok. If the display time-out, please input the command again.

2. Enter sudo apt get update
The code shown in the figure will appear in the update.

ubuntu@VM-0-13-ubuntu:~$ sudo apt-get update
Hit:1 http://mirrors.tencentyun.com/ubuntu xenial InRelease
Hit:2 http://mirrors.tencentyun.com/ubuntu xenial-security InRelease           
Hit:3 http://mirrors.tencentyun.com/ubuntu xenial-updates InRelease            
Get:4 http://ppa.launchpad.net/jonathonf/python-3.6/ubuntu xenial InRelease [18.0 kB]
Get:5 http://ppa.launchpad.net/jonathonf/python-3.6/ubuntu xenial/main amd64 Packages [4,760 B]
Get:6 http://ppa.launchpad.net/jonathonf/python-3.6/ubuntu xenial/main i386 Packages [4,748 B]
94% [Waiting for headers]      

The update command will access each web address in the source list, read the software list, and save it on the local computer. The list of software we see in the xinlide package manager is updated by the update command.
The upgrade command will compare the locally installed software with the corresponding software in the newly downloaded software list. If you find that the installed software version is too low, you will be prompted to update.

Enter the command sudo apt get install python3.6
Install Python 3.6

ubuntu@VM-0-13-ubuntu:~$ sudo apt-get install python3.6
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  libpython3.6-minimal libpython3.6-stdlib python3.6-minimal
Suggested packages:
  python3.6-venv python3.6-doc binfmt-support
The following NEW packages will be installed:
  libpython3.6-minimal libpython3.6-stdlib python3.6 python3.6-minimal
0 upgraded, 4 newly installed, 0 to remove and 198 not upgraded.
Need to get 4,505 kB of archives.
After this operation, 23.1 MB of additional disk space will be used.
Do you want to continue? [Y/n] y                 
Get:1 http://ppa.launchpad.net/jonathonf/python-3.6/ubuntu xenial/main amd64 libpython3.6-minimal amd64 3.6.5-5~16.04.york1 [574 kB]
Get:2 http://ppa.launchpad.net/jonathonf/python-3.6/ubuntu xenial/main amd64 python3.6-minimal amd64 3.6.5-5~16.04.york1 [1,712 kB]
Get:3 http://ppa.launchpad.net/jonathonf/python-3.6/ubuntu xenial/main amd64 libpython3.6-stdlib amd64 3.6.5-5~16.04.york1 [1,989 kB]
Get:4 http://ppa.launchpad.net/jonathonf/python-3.6/ubuntu xenial/main amd64 python3.6 amd64 3.6.5-5~16.04.york1 [230 kB]
Fetched 4,505 kB in 13s (330 kB/s)                                             
Selecting previously unselected package libpython3.6-minimal:amd64.
(Reading database ... 66624 files and directories currently installed.)
Preparing to unpack .../libpython3.6-minimal_3.6.5-5~16.04.york1_amd64.deb ...
Unpacking libpython3.6-minimal:amd64 (3.6.5-5~16.04.york1) ...
Selecting previously unselected package python3.6-minimal.
Preparing to unpack .../python3.6-minimal_3.6.5-5~16.04.york1_amd64.deb ...
Unpacking python3.6-minimal (3.6.5-5~16.04.york1) ...
Selecting previously unselected package libpython3.6-stdlib:amd64.
Preparing to unpack .../libpython3.6-stdlib_3.6.5-5~16.04.york1_amd64.deb ...
Unpacking libpython3.6-stdlib:amd64 (3.6.5-5~16.04.york1) ...
Selecting previously unselected package python3.6.
Preparing to unpack .../python3.6_3.6.5-5~16.04.york1_amd64.deb ...
Unpacking python3.6 (3.6.5-5~16.04.york1) ...
Processing triggers for man-db (2.7.5-1) ...
Processing triggers for mime-support (3.59ubuntu1) ...
Setting up libpython3.6-minimal:amd64 (3.6.5-5~16.04.york1) ...
Setting up python3.6-minimal (3.6.5-5~16.04.york1) ...
Setting up libpython3.6-stdlib:amd64 (3.6.5-5~16.04.york1) ...
Setting up python3.6 (3.6.5-5~16.04.york1) ...
ubuntu@VM-0-13-ubuntu:~$ 

This shows that Python 3.6 is installed successfully.

But you also need to set the priority between python3 and py2 and py3.

Enter python and you will find that py2 is still the default python

ubuntu@VM-0-13-ubuntu:~$ python
Python 2.7.12 (default, Dec  4 2017, 14:50:18) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
ubuntu@VM-0-13-ubuntu:~$ 

Now you should change the default value, Python is Python 2 by default, and python 3 by now

ubuntu@VM-0-13-ubuntu:~$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 100
update-alternatives: using /usr/bin/python2 to provide /usr/bin/python (python) in auto mode
ubuntu@VM-0-13-ubuntu:~$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 150
update-alternatives: using /usr/bin/python3 to provide /usr/bin/python (python) in auto mode

Now take a look at python

ubuntu@VM-0-13-ubuntu:~$ python
Python 3.5.2 (default, Nov 23 2017, 16:37:01) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

Found that Python 3.5 takes precedence over Python 3.6
Now we need to change Python 3.5 to 3.6

ubuntu@VM-0-13-ubuntu:~$ sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.5 1
update-alternatives: using /usr/bin/python3.5 to provide /usr/bin/python3 (python3) in auto mode
ubuntu@VM-0-13-ubuntu:~$ sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 2
update-alternatives: using /usr/bin/python3.6 to provide /usr/bin/python3 (python3) in auto mode

Now check whether the modification is successful.

ubuntu@VM-0-13-ubuntu:~$ python
Python 3.6.5 (default, May  3 2018, 10:08:28) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

You will find that python 3.6 is the default python version.

Keywords: Ubuntu Python sudo Linux

Added by CoffeeOD on Thu, 02 Jan 2020 04:07:45 +0200