Home >System Tutorial >LINUX >Easily learn to install and configure Python under Linux

Easily learn to install and configure Python under Linux

王林
王林forward
2024-02-11 13:30:27398browse

In the Internet era, Python is a very popular programming language. It is widely used in data analysis, machine learning, artificial intelligence and other fields. In the Linux operating system, the installation and configuration of Python is not as simple as in Windows or MacOS. Therefore, this article will introduce in detail how to install and configure Python in a Linux system so that readers can learn it easily.

Easily learn to install and configure Python under Linux

Step-by-step instructions for installing the latest Python on Linux, replacing or alongside older versions.

Python is now the most popular and commonly used programming language. Python’s simple syntax and low learning curve make it the ultimate choice for beginners and professional developers alike. Python is also a very versatile programming language. From web development to artificial intelligence, it is used almost everywhere except mobile development.

If you use Python, there's a good chance you're a developer (or want to be one), and Linux is a great platform for creating software. However, when you use Python every day, sometimes you want to use the latest version. You probably don't want to replace the default Python installation just to test your system with the latest version, so this article explains how to install the latest version of Python 3 on Linux without replacing the version provided by your distribution.

Use the python –version terminal command to check whether Python is installed, and if so, which version. If Python is not installed on your Linux system or you want to install a newer version, follow the steps below.

Step-by-step installation instructions

Step 1: First, install the development packages required to build Python
On Debian

$ sudo apt update
$ sudo apt install build-essential zlib1g-dev \
libncurses5-dev libgdbm-dev libnss3-dev \
libssl-dev libreadline-dev libffi-dev curl

On Fedora:

$ sudo dnf groupinstall development

Step 2: Download the latest stable version of Python 3
Visit the official Python website and download the latest version of Python 3. Once the download is complete, you will have a .tar.xz archive ("tarball") containing the source code for Python.

Step 3: Unzip the tarball
After the download is complete, use a decompression program or the Linux tar command to decompress the compressed package, for example:

$ tar -xf Python-3.?.?.tar.xz

Step 4: Configuration script
After unzipping the Python compressed package, enter the directory where the configure script is located and execute the script using the following command in the Linux terminal:

$ cd Python-3.*
./configure

Configuration may take some time. Wait until successful completion before continuing.

Step 5: Start the build process
If you already have a version of Python installed on your system and want to install a new version of Python at the same time, use the following command:

$ sudo make altinstall

The build process may take some time.

If you want to use this version to replace the current version of Python, you should use a package manager (such as apt or dnf) to uninstall the current Python package and then install:

$ sudo make install

However, it is usually best to install software as a package (such as a .deb or .rpm file) so that the system can track and update it for you. Because this article assumes that the latest Python has not yet been packaged, you may not have this option. In this case, you can use altinstall to install Python as recommended, or refactor the existing Python package with the latest source code. This is an advanced topic and specific to your distribution, so it's outside the scope of this article.

Step 6: Verify installation
If you don’t encounter any errors, you now have the latest Python installed on your Linux system. To verify, enter one of the following commands in the terminal:

python3 --version

If the output shows Python 3.x, then Python 3 has been successfully installed.

Create a virtual environment (optional)

Python provides a package called venv (virtual environment) that helps you isolate a program directory or package from other directories or packages.

To create a virtual environment, enter the following in the Python terminal (in this example, it is assumed that your installed Python version is the 3.8 series):

python3.8 -m venv example

This command creates a new directory (I named it example) with some subdirectories.

To activate the virtual environment, enter:

$ source example/bin/activate
(example) $

Please note that your terminal prompt ($) now starts with the environment name.

To deactivate a virtual environment, use the deactivate command:

(example) $ deactivate

This article introduces the installation and configuration methods of Python under Linux. By studying the content of this article, you can quickly master the skills of installing and configuring Python under Linux systems, making it easier for you to develop and run Python programs. At the same time, we also provide some practical commands and tips, hoping to help you use Python better.

The above is the detailed content of Easily learn to install and configure Python under Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:lxlinux.net. If there is any infringement, please contact admin@php.cn delete