Open In App

How to Install Python on Linux

Last Updated : 18 Jan, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

This guide explains how to install Python on Linux machines. Python has become an essential programming language for developers, data scientists, and system administrators. It’s used for various applications, including web development, data science, automation, and machine learning.

This comprehensive guide will walk you through the process of installing Python on your Linux system, covering everything from basic package management to advanced installation methods.

How-to-Install-Python-on-Linux

All Methods to Install Python on Linux

Major Versions of Python

When installing Python, it’s important to understand the two major versions:

  • Python 2: A legacy version no longer maintained. Avoid using it for new projects.
  • Python 3: The actively maintained version with enhanced features. Ideal for modern applications.

Tip: Always prioritize Python 3 for new projects.

Pre-Installation Checklist

Before starting the installation process, ensure you have:

  1. Root Access: Sudo privileges to install software.
  2. Terminal Access: Familiarity with the command line.
  3. Internet Connection: Active internet access for downloading packages.
  4. Disk Space: At least 200MB available.
  5. Command-Line Basics: Understanding of simple terminal commands.

First, Check if Python is Already Installed on Linux

Many Linux distributions come with Python pre-installed. Knowing if Python is pre-installed saves time and avoids duplicate installations.

Note: If Python is installed, the terminal will display its version number. If not, the terminal will return a “command not found” error, indicating that Python needs to be installed.

Step 1: Open Terminal

Open your terminal window using the shortcut key Ctrl + Alt + T

Step 2: Check for Python Version

For Python 3

Use the following command to verify the Pre-Installed Python 3 Version in your Linux Laptiop.

python3 --version

For Python 2

Use the following command to check the Pre-Installed Python 2 Version in your Linux Environment.

python --version

Method 1: Installing Python using Package Managers (For Beginners)

This the simplest and most common method of installing Python. Package managers are essential tools in Linux distributions that handle the installation, upgrading, and removal of software packages. Each Linux distribution has its preferred package manager.

Installing Python For Ubuntu/Debian Based Systems using APT

APT (Advanced Package Tool) is the default package manager for Ubuntu and Debian-based systems, known for its Extensive package repository, Strong security features, Stable package versions, and Regular security updates.

Step 1: Update Package Repository

Note: Always start with this step to prevent version conflicts.

The following command will refreshes your system’s package database to ensure you get the latest version.

sudo apt update

Step 2: Upgrade Existing Packages

Note: This step prevents compatibility issues by updating all installed software.

Updates all installed packages to their latest versions.

sudo apt upgrade

Step 3: Install Python 3

This commands Installs the default Python 3 version for your.

sudo apt install python3

Step 4: Install Python Package Manager (pip)

Pip is essential for managing Python libraries and dependencies. This command Installs pip for managing Python packages and dependencies.

sudo apt install python3-pip

Step 5: Install Additional Tools for Development

Provides tools for creating virtual environments and compiling Python extensions.

sudo apt install python3-dev python3-venv build-essential

Errors and Fixes

Permission Denied Errors

This command will Fixe common permission issues with pip installations.

sudo chown -R $USER:$USER ~/.local

Package Lock Errors

This command Resolves package manager lock issues.

sudo rm /var/lib/apt/lists/lock
sudo rm /var/cache/apt/archives/lock

Set up Python For Fedora Linux Systems using DNF

DNF (Dandified Yum) is the next-generation package manager for Red Hat-based systems, including Fedora, CentOS, and RHEL. Known for its enhanced performance, DNF is particularly effective for managing dependencies and ensuring system stability.

Step 1: Update System Package List

The following command will refreshes your system’s package database to ensure you get the latest version.

sudo dnf update

Step 2: Install Python 3

This commands Installs the default Python version with necessary dependencies.

sudo dnf install python3

Step 3: Install Development Tools

This commands Installs compilers and libraries required for Python development.

sudo dnf groupinstall "Development Tools"

Step 4: Install pip

This command installs Installs the Python package manager.

sudo dnf install python3-pip

Erros and Fixes: Red Hat/Fedora Systems

Slow Downloads: If DNF is slow

This command Improves download speed by increasing parallel connections.

echo 'max_parallel_downloads=10' | sudo tee -a /etc/dnf/dnf.conf

Repository Issues on CentOS

This Resolves issues with outdated or misconfigured repositories.

sudo yum clean all
sudo yum repolist

How to Install Latest Python on Arch Linux using Pacman

Pacman is the default package manager for Arch Linux, offering a rolling release model, Latest software versions, Simple and fast package management, and Minimal overhead.

Step 1: Update System

Synchronizes package repositories and updates your system.

sudo pacman -Syu

Step 2: Install Python

Installs the latest available Python version.

sudo pacman -S python

Step 3: Install pip

Adds pip for Python library management.

sudo pacman -S python-pip

Errors and Fixes: Red Hat/Fedora Systems

Package Conflicts

Removes conflicting packages safely.

sudo pacman -Rns conflicting-package

How to install Python on CentOS and Red Hat Linux

YUM offers Enterprise-grade stability, Long-term support, Extensive documentation, Commercial support options.

Step 1: Update System

Updates the package database and installed packages.

sudo yum update

Step 2: Enable EPEL Repository

Enables Extra Packages for Enterprise Linux repository.

sudo yum install epel-release

Step 3: Install Python 3

Installs Python 3 from the repository.

sudo yum install python3

Errors and Fixes: CentOS/RHEL Using YUM

Repository Issues: Cleans and verifies repository configuration.

sudo yum clean all
sudo yum repolist

Installing Newest Python on openSUSE using Zypper

Zypper provides Advanced package management, Strong dependency resolution, Integration with YaST, and Enterprise support options.

Step 1: Update Repositories

Updates package repository information.

sudo zypper refresh

Step 2: Install Python

Installs Python 3 and basic dependencies.

sudo zypper install python3

Step 3: Install Development Tools

Installs Python development files.

sudo zypper install python3-devel

Fixes and Errors: openSUSE Using Zypper

Repository Conflicts: Resolves repository and cache issues.

sudo zypper clean
sudo zypper refresh

Best Practices When Intsalling Python on Linux

  • Always Update First Before installing Python, update your system’s package database and existing packages.
  • After installation, verify Python and pip are correctly installed.

Method 2: Installing Python Using Version Managers (For Developers)

Python Version managers like Pyenv and asdf are for developers who need to work with multiple Python versions. This provide the flexibility to switch between different Python versions easily, creating isolated environments, and managing project-specific dependencies effectively.

Install Latest Python Version with Pyenv

Pyenv is a lightweight Python version management tool which is popular among developers, as it manages multiple Python versions easily.

Step 1: Install Dependencies

It Provides libraries required to compile Python.

sudo apt install -y build-essential libssl-dev zlib1g-dev \

libsqlite3-dev libffi-dev

Step 2: Install Pyenv

Downloads and installs Pyenv on your system.

curl https://pyenv.run | bash

This command installs Pyenv along with useful plugins like pyenv-virtualenv and pyenv-update.

Step 3: Configure Shell Environment

Sets up your shell environment to work with pyenv.

echo ‘export PYENV_ROOT=”$HOME/.pyenv”‘ >> ~/.bashrc

echo ‘command -v pyenv >/dev/null || export PATH=”$PYENV_ROOT/bin:$PATH”‘ >> ~/.bashrc

echo ‘eval “$(pyenv init -)”‘ >> ~/.bashrc

Step 4: Reload Shell Configuration

Activates the new shell configuration.

source ~/.bashrc

Step 5: Install Python

To see a list of available Python versions, run:

pyenv install --list

Choose a version you want to install (e.g., Python 3.13.1) and run:

pyenv install -v 3.13.1

The -v flag enables verbose mode, showing detailed output during installation

Step 6: Set Global or Local Python Version

After installation, you can set the global Python version that will be used by default:

pyenv global <version>

Alternatively, you can set a local version for a specific project directory:

pyenv local <version>

Step 7: Verify Installation

To confirm that the installation was successful, check the installed version of Python:

python --version

Errors and Fixes: Pyenv Installation and Setup for Python Installation

Build Failures: Resolves common build optimization issues.

CFLAGS="-O2" pyenv install 3.12.1

SSL Errors: Fixes SSL-related installation problems.

sudo apt install libssl-dev

Install Python using asdf on Linux System

asdf is a versatile version manager that allows you to manage multiple versions of various programming languages, including Python. Here’s how to install Python using asdf on a Linux system:

Step 1: Install asdf

1. Install Dependencies

Ensure you have git and curl installed on your system. You can install them using your package manager.

sudo apt update
sudo apt install git curl

2. Clone the asdf Repository

Open your terminal and run:

git clone https://github.com/asdf-vm/asdf.git ~/.asdf --branch v0.13.1

Step 2: Configure the Shell

Add the following lines to your shell configuration file (e.g., ~/.bashrc or ~/.zshrc):

. "$HOME/.asdf/asdf.sh"
. "$HOME/.asdf/completions/asdf.bash"  (for bash users)

For Zsh users, replace .bash with .zsh

Step 3: Install the Python Plugin

asdf plugin-add python

Step 4: Reload Your Shell

Run the following command to apply the changes:

source ~/.bashrc  # or source ~/.zshrc for Zsh users

Step 5: Install the Python Plugin

Run the following command to add the Python plugin to asdf:

asdf plugin-add python

Step 6: Install Python Versions

1. List available versions:

asdf list-all python

2. You can now install a specific version of Python (for example, 3.11.4) by running:

asdf install python <version>

3. To install multiple versions at once, you can chain commands:

asdf install python 3.10.15 && asdf install python 3.9.9

Step 7: Set Global or Local Python Version

To set a global version of Python that will be used system-wide, run:

asdf global python 3.13.1

If you want to set a specific version for a project directory, navigate to that directory and run:

cd your_project_directory
asdf local python 3.13.1

Step 8: Verify Installation

To confirm that the installation was successful, check the installed version of Python:

python --version

Installing Python Using Conda and Miniconda on Linux

Miniconda is a lightweight version of Anaconda that includes only Conda and its dependencies, allowing you to install Python and manage packages effectively. Here’s a step-by-step guide to installing Python using Miniconda on a Linux system.

Step 1: Download Miniconda Installer

  • Open your terminal.
  • Download the Miniconda installer for Linux. You can use wget or curl. For example:
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh

Step 2: Verify the Installer (Optional)

It’s a good practice to verify the integrity of the installer. You can find the hash values on the Miniconda website. Use the following command to check:

chmod +x Miniconda3-latest-Linux-x86_64.sh

Compare the output with the hash provided on the website.

Step 3: Run the Installer

Run the installer and Follow the on-screen prompts to complete the installation.

./Miniconda3-latest-Linux-x86_64.sh

Follow the prompts in the terminal:

  • Press Enter to review the license agreement.
  • Type yes to accept the terms.
  • Choose an installation location (the default is usually fine).
  • Opt to initialize Miniconda by allowing it to modify your shell configuration file.

Step 4: Initialize Conda

After installation, you may need to initialize Conda for your shell. If you accepted this during installation, it should be done automatically. If not, you can run:

conda init
  • Modifies your shell configuration file to activate the Conda environment automatically.
  • Ensures Conda commands like conda activate and conda deactivate work without additional configuration.

Then, restart your terminal or run source ~/.bashrc (or source ~/.zshrc if you’re using Zsh).

Step 5: Install Python

Create a new environment with a specific version of Python (for example, Python 3.13)

conda create -n myenv python=<version>

Activate the environment

conda activate myenv

Step 6: Verify Python Installation

To check that Python is installed correctly within your environment, run:

python --version

Method 3: Manual Installation Methods for Python on Linux

Installing Python on Linux can be done through various manual methods, primarily by using a package manager or building from source. Below are the detailed steps for each method.

Building Python from Source Code

If you need a specific version of Python or want to customize the installation, you can build it from source. This method gives you full control over the Python installation and allows customization during the build process:

Step 1: Install Required Development Packages

Before building Python, ensure you have the necessary development tools and libraries installed.

sudo apt install build-essential libssl-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libffi-dev zlib1g-dev

Step 2: Download the Latest Version of Python

Visit the official Python website and download the latest source code tarball. Alternatively, use wget to download it directly:

wget https://www.python.org/ftp/python/X.Y.Z/Python-X.Y.Z.tgz 

Replace <version> with the Python version you want (e.g., 3.13.1). This downloads the source code as a .tgz archive.

Step 3: Extract Tarball and Navigate to the Source Directory

After downloading, extract the tarball:

tar -xvf Python-<version>.tgz

Step 4: Configure the Build Environment

Navigate to the extracted directory and configure the build environment:

cd Python-X.Y.Z
./configure --enable-optimizations 
  • The –enable-optimizations flag can improve performance.
  • This unpacks the source code into a directory for building and installation.

Step 5: Compile and Install

Compile the source code and install it:

This uses all available cores for faster compilation:

make -j $(nproc) 

Use altinstall to prevent overwriting any existing python binary:

sudo make altinstall

Step 5: Verify the Installation

After installation, verify that Python is installed correctly by checking its version:

python3 --version

Replace 3.10 with the installed version to confirm it’s correctly installed.

Installing Python Using the Official Installer on Linux

To install Python on Linux using the official installer, you typically download the source code from the Python website and compile it. Here’s a step-by-step guide to perform this installation.

Step 1: Download the Latest Python Version

Go to python.org to find the latest version of Python.

You can download the latest version as a tarball. For example, use wget in your terminal:

wget https://www.python.org/ftp/python/<version>/Python-<version>.sh

Replace <version> with the version you want to install.

Step 2: Install Required Development Packages

Before compiling Python, ensure that you have the necessary development tools installed.

For Ubuntu/Debian, run:

sudo apt update
sudo apt install build-essential libssl-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libffi-dev zlib1g-dev

For Fedora, you can install development tools with:

sudo dnf groupinstall "Development Tools"

Step 3: Extract the Downloaded Tarball

Once the download is complete, extract the tarball:

tar -xf Python-X.Y.Z.tgz
cd Python-X.Y.Z

Step 4: Configure the Build Environment

Run the configuration script to prepare for building:

./configure --enable-optimizations

The --enable-optimizations flag helps optimize the Python binary for better performance.

Step 5: Compile and Install

Compile the source code and install Python:

This uses all available CPU cores for faster compilation.

make -j $(nproc) 

This prevents overwriting any existing python binary.

sudo make altinstall

Step 6: Verify Installation

Check if Python has been installed correctly by running:

python3 --version

Installing Python Using Docker Containers on Linux

Docker provides a convenient way to run applications in isolated environments, making it easy to manage dependencies and versions. Here’s how to install Python using Docker containers on a Linux system.

Step 1: Install Docker

If you haven’t already installed Docker, you can do so by following these steps:

Update your package index:

sudo apt update

Install Docker:

For Ubuntu/Debian-based systems:

sudo apt install -y docker.io

For Fedora-based systems:

sudo dnf install -y docker

For Arch-based systems:

sudo pacman -S docker

Start and enable Docker:

Start the Docker service and enable it to start automatically on boot:

sudo systemctl start docker
sudo systemctl enable docker

Docker is a containerization tool that runs applications, including Python, in isolated containers.

Step 2: Pull the Python Docker Image

Docker provides official Python images that are lightweight and pre-configured for different versions. Copy the below command to pull the Python Docker image for the desired version:

sudo docker pull python:<version>

Replace <version> with the desired Python version (e.g., 3.10). This downloads the image containing Python.

Step 3: Run Python in a Container

Once the image is downloaded, you can start a container with Python. Follow the below command to Run Python Container:

sudo docker run -it python:<version>
  • The -it option starts an interactive terminal.
  • The Python REPL (interactive shell) will launch within the container.

This launches an interactive Python shell inside a container.

Step 4: Run Python Scripts in a Container

Now you can run a Python script stored on your host machine, use Docker’s volume mounting feature to share the file with the container.

Installing Python on Linux Using Snap Packages

Snap packages provide a convenient way to install and manage software on Linux systems in a containerized format. Here’s a step-by-step guide to installing Python using Snap packages.

Step 1: Install Snap

If Snap is not already installed on your system, you can install it using the following commands based on your Linux distribution:

For Ubuntu/Debian:

sudo apt update
sudo apt install -y snapd

For Fedora:

sudo dnf install snapd

For Arch Linux:

sudo pacman -S snapd

Note: After installation, ensure that the Snap service is enabled:

sudo systemctl enable --now snapd.socket

Step 2: Install Python Using Snap

You can find available Python versions in the Snap store by running:

snap find python

To install the latest version of Python, you can use:

sudo snap install python3

Replace python3 with the specific version you want (e.g., python3.13)

Step 3: Verify the Installation

Once the installation is complete, verify that Python has been installed correctly by checking its version:

python3 --version

This confirms the Snap package installed Python successfully.

Method 4: Installing Python on Linux Using Cloud-Based Solutions

Cloud-based environments provide a flexible way to install and run Python without the need for local setup. Here’s how to install Python using popular cloud platforms like AWS Cloud9 and Google Cloud Compute Engine.

Installing Python Using Cloud Provider Tools on Linux

Most cloud providers, like AWS, Google Cloud, and Azure, offer pre-configured environments with Python.

Step 1: Choose a Cloud Provider

Select a cloud provider that supports Python. Common options:

  • AWS (Amazon Web Services): Use EC2 instances or AWS Lambda.
  • Google Cloud Platform (GCP): Use Compute Engine or App Engine.
  • Microsoft Azure: Use Azure Virtual Machines or Functions.

These providers offer ready-to-use instances with Python pre-installed or easily configurable.

Step 2: Launch a Virtual Machine (VM)

VMs allow you to run Python as if on a physical machine, giving you control over installations.

  1. Log in to the cloud provider’s console.
  2. Create a new VM instance.
  3. Choose an OS and ensure Python is included in the configuration or installable.

Step 3: Install or Use Pre-installed Python

Some cloud VMs come with Python pre-installed. You can verify it using python3 --version. But If Python isn’t pre-installed the follow the below commands:

SSH into the VM:

ssh user@<VM-IP>

Install Python using package managers or version managers:

sudo apt install python3

How to Install Python Using Deadsnakes PPA (Ubuntu Only)

Deadsnakes PPA is used to install newer Python versions which are not available in the default repositories. The deadsnakes PPA is a trusted repository that provides the latest Python versions for Linux-based distributions.

Step 1: Install a tool to add PPAs

sudo apt install software-properties-common

Step 2: Add the Deadsnakes Repository

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update

Step 3: Install a specific Python version (e.g., 3.13)

sudo apt install python3.13

How to Verify Python Installation on Linux

After installation, Confirm that Python and pip were installed correctly. These commands ensure Python and pip are installed and accessible from the terminal.

Check Python Version

python3 --version

Check pip Version

pip3 --version

Conclusion

Installing Python on Linux is a straightforward process when using package managers or trusted repositories like deadsnakes PPA. By following this guide, you’ll have Python and its tools ready for development. Isolating projects with virtual environments and managing Python versions with pyenv ensures a clean and flexible setup for all your projects.

Install Python on Linux – FAQs

How can I check if Python is already installed on my Linux system?

You can check if Python is pre-installed on your Linux distribution by opening the terminal and running the following commands:

python --version  # For Python 2.x
python3 --version # For Python 3.x

If Python is installed, these commands will display the respective version numbers. If not, you’ll typically get a “command not found” error.

The recommended way to install Python on Linux-based distributions is to use the Advanced Packaging Tool (APT) package manager. You can install Python 3 with the following command:

sudo apt install python3

How can I install a specific version of Python on Linux?

To install a specific version of Python, you may need to add a third-party repository like the deadsnakes PPA for Linux-dist. After adding the repository, you can install the desired Python version with a command like:

sudo apt install python3.9  # For Python 3.9

Replace “3.9” with the version number you want to install.

What is pip, and how do I install it on Linux?

pip is the package installer for Python, which allows you to install and manage external Python packages and libraries. On most Linux distributions, you can install pip for Python 3 with the following command:

sudo apt install python3-pip

Alternatively, you can download the get-pip.py script and run it with the respective Python version:

curl https://bootstrap.pypa.io/get-pip.py | python3

How can I install Python packages in a virtual environment on Linux?

It’s generally recommended to use virtual environments to isolate Python packages and dependencies for different projects. You can create and activate a virtual environment on Linux using the venv module:

python3 -m venv myenv       # Create a virtual environment named 'myenv'
source myenv/bin/activate # Activate the virtual environment

With the virtual environment activated, you can install packages using pip without affecting the system-wide Python installation.



Practice Tags :

Similar Reads

three90RightbarBannerImg