Detailed tutorial on installing python3.6 under ubuntu1~16.04.9 (installation example on Tencent Cloud server)

Detailed tutorial on installing python3.6 under ubuntu1~16.04.9 (installation example on Tencent Cloud server)

1. Enter sudo add-apt-repository ppa:jonathonf/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

After the prompt is ok, there is no problem. If the timeout is displayed, please enter the command just again.

2. Enter sudo apt-get update and it will appear as shown in the code shown 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 visit each URL in the source list, read the software list, and save it on the local computer. The software list we see in Synaptic Package Manager is updated through 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 to install python3.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 python 3.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 python3.6 is installed successfully.

But you also need to set the priority between python3 and the priority between 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:~$ 

The default value should be changed now, python defaults to Python2, now it is modified to Python3

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 python3.5 is preferred to python3.6. Now we need to change python3.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 python3.6 is the default python version.

Supplementary question

After installing python3.6, I found some problems with pip installation.

Enter the command sudo apt-get install python-pip to install pip This problem is because python 3.6 is installed now, so it should be sudo apt-get install python3-pip

Server used:

No threshold for new customers to receive vouchers with a total value of up to 2860 yuan

Cloud server 30% off event

Reference: https://cloud.tencent.com/developer/article/1527011 ubuntu1~16.04.9 install python3.6 detailed tutorial (installation example on Tencent Cloud server)-Cloud + Community-Tencent Cloud