Python online course

Install Python on Linux

Learn how to check for Python on Linux and install Python 3 on Debian-based and RPM-based distributions using supported package-management tools.

Python is a programming language and runtime that is included by default on many Linux distributions. Before installing anything, check which Python interpreter is already available. For new programs, use Python 3, the modern major version of Python.

Prerequisites

You should know how to open a Linux terminal, which is a command-line interface for running commands. You should also understand basic command-line navigation, administrative privileges, and the Linux distribution installed on your computer.

The commands in this lesson may use sudo. This command prefix runs an administrative command with elevated privileges. Your user account must be authorized to use it, and Linux may ask for your password.

Check whether Python is installed

Open a terminal and check the default python command:

python --version

The terminal may display a version such as Python 2.x, Python 3.x, or an error indicating that the command is unavailable. The exact version number is not important for this check.

Next, check specifically for Python 3:

python3 --version

A successful response begins with Python 3, followed by the installed version. This is the most useful check when preparing to learn Python or run a modern Python program.

How to interpret the results

  • python --version shows Python 2: The system's default command points to the legacy Python 2 interpreter. Use python3 for new work.
  • python3 --version shows Python 3: Python 3 is installed and ready to use.
  • A command is not found: That interpreter is not installed or is not exposed under that command name.
  • python and python3 show different versions: The commands select different interpreters. Use python3 when you need Python 3.

Install Python 3 on Debian-based distributions

Debian-based distributions use the APT package-management system. Examples include Debian and Ubuntu. A package manager is a distribution-provided tool for finding, installing, updating, and removing software packages.

First, refresh the local package metadata. This lets APT obtain current information about packages available from configured repositories.

sudo apt update

Then install the distribution-maintained Python 3 package:

sudo apt install python3

APT may report that Python 3 is already the newest version. That is a successful result: the package is installed. Otherwise, review the proposed changes and confirm the installation when prompted.

Verify the installation:

python3 --version

The command should return a Python 3 version response, for example Python 3 followed by a version number. Do not require a particular version number; the available version depends on your distribution and its enabled repositories.

Install Python 3 on RPM-based distributions

An RPM-based distribution uses RPM packages, but the user-facing package tool varies by distribution. Fedora and related systems commonly use dnf. SUSE systems commonly use zypper or the YaST administration tool. Use the tool supported by your distribution rather than applying an APT command to an RPM-based system.

SUSE and YaST

YaST is a configuration and software-management tool used by SUSE Linux distributions. To install Python 3 with YaST, open YaST, choose its software-management function, search for the Python 3 package, select it for installation, and apply the changes. The exact package name and interface can vary by SUSE release.

You can also use the SUSE command-line package tool where appropriate. For example, a supported SUSE system may use zypper to search for and install packages. Check the package-management documentation for your particular release before running an installation command.

sudo zypper install python3

If your system does not recognize that command or uses a different package name, use YaST or the distribution's documented software-management tool instead.

Other RPM-family systems

On systems that use dnf, the corresponding installation command is commonly:

sudo dnf install python3

Package commands can differ between releases. If this command is not supported, identify the distribution and use its supported package tool. The important principle is to install Python 3 from the operating system's normal repositories whenever possible.

Python installation commands by distribution family

  • Debian-based systems, such as Ubuntu: Use APT. Run sudo apt update, then sudo apt install python3. Verify with python3 --version.
  • SUSE systems: Use YaST Software Management or the supported SUSE command-line tool, commonly zypper. Verify with python3 --version.
  • Other RPM-based systems: Use the distribution's supported RPM-family tool, such as dnf where applicable. Verify with python3 --version.

Use the correct Python command

Prefer python3 when invoking Python 3. The shorter python command is not consistent across Linux distributions: it may refer to Python 2, refer to Python 3, or be unavailable entirely.

For example, start a Python 3 program explicitly with:

python3 my_program.py

Do not replace, remove, or manually redirect the operating system's managed Python installation just to make python point to a preferred version. This installation is called system Python and may be required by system utilities. Changing it can cause administrative tools or other operating-system components to fail.

Verify the installation with the interactive interpreter

After python3 --version succeeds, you can optionally launch the Python 3 interactive interpreter:

python3

A successful launch displays information about Python and then a prompt containing >>>. Enter a small expression to confirm that the interpreter accepts input:

>>> 2 + 3
5

Exit the interpreter with exit(), or press Ctrl-D on Linux:

>>> exit()

You can learn more about this environment in Python's interactive prompt and continue with running Python code.

Troubleshooting

The python command is not found

The distribution may provide only python3, or Python may not be installed. Run:

python3 --version

If that also fails, install the Python 3 package with your distribution's package manager.

The python command reports Python 2

Python 2 is a legacy major version. Do not select it for new projects. Use python3 instead, and install Python 3 if the command is unavailable.

APT cannot install Python 3

Common causes include stale package metadata, missing administrative privileges, or unavailable repository configuration. Try refreshing the metadata again:

sudo apt update

Make sure you can use sudo and that the normal repositories for your Debian-based distribution are enabled. If the instructions using APT do not match your system, it may not be Debian-based; use its supported package tool instead.

The RPM-based instructions do not work

RPM-based distributions do not all use the same command. Identify your distribution and use its supported tool, such as YaST, zypper, or dnf. Avoid guessing package-manager commands from another distribution family.

Changing the default Python breaks system tools

This usually means the system relies on its managed Python interpreter. Restore the distribution-managed configuration if possible, and invoke your desired interpreter explicitly with python3. Do not remove or overwrite system Python.

Quick reference

  • Check the default interpreter: python --version
  • Check Python 3: python3 --version
  • Refresh Debian-based package metadata: sudo apt update
  • Install Python 3 on Debian-based systems: sudo apt install python3
  • Launch Python 3: python3

Once Python 3 is installed, you can continue with writing your first Python program or learn how to use the interactive prompt.