How to install Python on Linux (Ubuntu) system

The Linux system was born for programming, so most Linux distributions (Ubuntu, CentOS, etc.) come with Python by default. Some Linux distributions even come with two versions of Python. For example, the latest version of Ubuntu comes with Python 2.x and Python 3.x.

Open the built-in terminal (Terminal) of the Linux distribution, and enter the python command to check whether Python is installed and which version is installed, as shown below:

[c.biancheng.net@localhost ~]$ python
Python 2.7.5 (default, Jun 17 2014, 18:11:42)
[GCC 4.8.2 20140120 (Red Hat 4.8.2-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

It can be seen that the python command can run normally and output the version information of Python, which indicates that the current Linux distribution has its own Python 2.7.5.

In addition, the Python command prompt >>> appears at the end of the execution result, which means that we have entered the Python interactive programming environment, where we can directly enter the code and view the running results, as shown below:

[c.biancheng.net@localhost ~]$ python
Python 2.7.5 (default, Jun 17 2014, 18:11:42)
[GCC 4.8.2 20140120 (Red Hat 4.8.2-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print("C语言中文网的网址是:http://c.biancheng.net")
C语言中文网的网址是:http://c.biancheng.net
>>> a=100
>>> b=4
>>> a*b
400
>>> exit()
[c.biancheng.net@localhost ~]$ 

exit() is used to exit the Python programming environment and return to the Linux command line.

Most Linux distributions will come with Python 2.x, but not necessarily Python 3.x. To check whether Python 3.x is installed in the current Linux distribution, you can enter the python3 command in the terminal (Terminal), as follows Shown:

[c.biancheng.net@localhost ~]$ Python3
Python 3.6.4 (default , Nov 18 2018 , 13:02:36)
[GCC 4.8.2 20140120 (Red Hat 4.8.2-16)] on linux2
Type "help","copyright","credits" or "license" for more information.
>>>

If the python3 command runs successfully, and the Python prompt >>> appears, it indicates that the current Linux distribution has installed the Python 3 development environment, and you only need to execute the python3 command to start the Python 3 development environment.

If the current Linux distribution does not have Python 3 installed, or you feel that the existing Python 3 version is not new enough, then you need to update the Python version. In this section, we take Ubuntu as an example to demonstrate.

Update the Python version

Execute the following two commands on the Ubuntu terminal to update the Python version:

$sudo apt-get update
$sudo apt-get install python3.8

Description of the command:
The first command is used to update the source addresses listed in /etc/apt/sources.list and /etc/apt/sources.list.d, so as to ensure the latest installation package.

The second command is used to specify the installation of Python 3.8, which is the latest version of Python.

Wait for the execution of the above two commands to complete, and enter the python3 command in the terminal again, and you can see that the Python interactive programming environment has been updated to Python 3.8.

reinstall python

The above update method is only valid if Python has been installed in Ubuntu. If there is no Python environment in your Ubuntu, or if you want to reinstall, then you can download the source code from the official website and compile it yourself.

1. Download source code
Python official download address: https://www.python.org/downloads/

Open the link and you can see the various versions of Python:

Click the version number or the "Download" button in the above picture to enter the download page of the corresponding version, and scroll to the end to see the Python installation packages for each platform.

Find the source package address

Click the right mouse button on the "Gzipped source tarball" and select "Copy link address" from the pop-up menu to get the address of the source tarball in .tgz format.

Then execute the following command:

$ wget https://www.python.org/ftp/python/3.8.1/Python-3.8.1.tgz

Unzip the source package:

$ tar -zxvf Python-3.8.1.tgz

2. Compile
Use the make tool to compile:

$ ./configure  --prefix=/usr/local
$ make&&sudo make install

Here --prefix=/usr/local is used to specify the installation directory (recommended). If not specified, the default installation directory will be used.

After the above commands, we have installed Python, then we can enter the terminal, enter the Python command, and verify whether the installation is successful.

Guess you like

Origin blog.csdn.net/weixin_44617651/article/details/127787842