Linux systems are built for programming, thus most of Linux distribution (such as Ubuntu, CentOS, etc.) come with Python by default. Some Linux distributions even come with two version of Python. For instance, the latest version of Ubuntu comes with Python 2.x and Python 3.x.
1. Open the terminal which built in the Linux distribution therefore key in “Python” to detect whether Python has installed and what is the installed version of Python.
2. You can see the Python version which indicates that the current Linux distribution comes with Python version. The Python command prompt >>> indicates we have entered the Python interactive programming environment.
3. Key in “exit()” to exit the Python programming environment and return to the Linux command line. Majority of Linux distributions come with Python 2.x instead of Python 3.x. Key in “Python3” in the Terminal to check the current Python version in the Linux distribution.
If the Python3 command runs successfully and symbol >>> appears on the Python prompt, it indicates that the current Linux distribution has installed the Python 3 development environment. Thus, you can start to use Python3.
However, if current Linux does not install Python 3, or you feel that the existing Python 3 version is not the latest version, you can update the Python version. For this case, we use Ubuntu as an example.
1. Update the Python version by executing the following two commands in Ubuntu terminal:
$sudo apt-get update
$sudo apt-get install python3.8
Explanation of two commands:
The first command: used to update the source addresses listed in /etc/apt/sources.list and etc/apt/sources.list.d, it can help to ensure that the latest installation package is obtained.
The second command: used to specify the version of Python that you would like to update to.
Wait for the execution of the above two commands and enter the Python3 command in the terminal again. You can see the Python interactive programming environment has been updated to Python 3.8.
If you do not have Python environment in Ubuntu or you want to reinstall, then you can follow here.
1. Download the source code from the official website.
Python official address: https://www.python.org/downloads/
You can see various versions of Python after clicking on the link.
2. Look for the Python version that you want to install then download it.
3. Click either the Python version number or “Download” button in the figure below in order to enter the download page. Therefore, scroll to the end of the page to look for Python installation packages for each platform.
4. Right-click on the “Gzipped source tarball” and select “Copy link address” from the pop-up menu in order to get the source code archive address in .tgz format.
5. Paste the link address after wget:
$ wget https://www.python.org/ftp/python/3.8.1/Python-3.8.1.tgz
6. Extract the source package. For this case, it is Python-3.8.1.tgz
$ tar -zxvf Python-3.8.1.tgz
6. Compile using the make tool:
$ ./configure --prefix=/usr/local
$ make&&sudo make install
The --prefix=/usr/local is used to specify the installation directory. if nothing mentioned here, therefore the default installation directory will be used.
Finally, we have installed Python, now you can verify again whether the installation is successful using the same method mentioned above.
More Tutorials:Python Installation - Linux (Ubuntu)
More Python Exercises:Python String Exercises