Overview: Python Installation on Linux
Python Installation on Linux system can be done through various methods, depending on your specific needs and the Linux distribution you are using.

Here’s a step-by-step guide to Python Installation on Linux using different methods:
Method 1: Installing Python Using the Package Manager
Most Linux distributions come with Python pre-installed. However, if you need to install a specific version or if Python is not installed, you can use the package manager of your distribution.
Step 1: Update Package List
Before installing any new packages, it is a good practice to update the package list to ensure you have the latest information about available packages.
sudo apt update # For Debian-based systems (e.g., Ubuntu)sudo yum update # For Red Hat-based systems (e.g., CentOS)Step 2: Install Python
- For Debian-based Systems (e.g., Ubuntu):
To install Python 3:
sudo apt install python3To install Python 2 (if needed):
sudo apt install python2- For Red Hat-based Systems (e.g., CentOS, Fedora):
To install Python 3:
sudo yum install python3To install Python 2 (if needed):
sudo yum install python2Step 3: Verify the Installation
After installation, verify the installed version of Python.
python3 --versionFor Python 2:
python2 --versionMethod 2: Installing Python from Source
If you need a specific version of Python or the latest version not available through the package manager, you can install Python from source.
Step 1: Install Dependencies
First, install the necessary build dependencies.
- For Debian-based Systems:
sudo apt install build-essential checkinstall nsudo apt install libreadline-gplv2-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev libffi-dev zlib1g-dev- For Red Hat-based Systems:
Step 2: Download Python Source Code
Download the source code of the desired Python version from the official Python website.
wget https://www.python.org/ftp/python/3.x.x/Python-3.x.x.tgz # Replace x.x with the desired versionStep 3: Extract the Downloaded File
Extract the tarball.
tar -xvzf Python-3.x.x.tgzStep 4: Compile and Install Python
Navigate to the extracted directory and compile the source code.
cd Python-3.x.x ./configure --enable-optimizations make -j 4 # Adjust the number to the number of cores in your CPUAfter the compilation is complete, install Python.
sudo make altinstallNote: Using make altinstall is recommended to prevent overwriting the default system Python.
Step 5: Verify the Installation
Check the installed Python version.
python3.x --version # Replace x with the minor version numberMethod 3: Using Pyenv to Manage Python Versions
pyenv is a popular tool to manage multiple Python versions on the same system.
Step 1: Install Dependencies
Install the necessary dependencies for building Python.
- For Debian-based Systems:
sudo apt update sudo apt install -y make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev python-openssl git- For Red Hat-based Systems:
sudo yum install @development zlib-devel bzip2 bzip2-devel readline-devel sqlite sqlite-devel openssl-devel xz xz-devel libffi-devel gitStep 2: Install pyenv
Clone the pyenv repository and set up the environment.
curl https://pyenv.run | bashAdd pyenv to your shell’s startup file (.bashrc, .zshrc, etc.):
Step 3: Install Python Using pyenv
List available Python versions and install the desired one.
pyenv install --list # List available versions npyenv install 3.x.x # Replace x.x with the desired version npyenv global 3.x.x # Set the global versionStep 4: Verify the Installation
Check the installed Python version.
python --versionSummary – Python Installation on Linux
IPython Installation on Linux system can be accomplished using various methods, depending on your needs and the distribution you are using. The package manager method is the easiest and quickest way, suitable for most users. If you need a specific version of Python, installing from source is a good option. For managing multiple Python versions, pyenv is a highly recommended tool. Each method has its own advantages, and choosing the right one depends on your specific requirements.
Python Installation on Linux
Useful Links
Python Installation on Linux: Comprehensive Package Manager, Source, and pyenv Methods
This article provided insights on the topic. For latest updates and detailed guides, stay connected with Sanchit Gurukul.
Your feedback matters
Was this post helpful?
Discussion
0 approved comments
No approved comments yet. You can start the discussion below.
Leave a Comment
No login is required. Name and email are used for moderation/security. Your email is never displayed publicly. All comments require administrator approval.
Discover more from SanchitGurukul
Subscribe to get the latest posts sent to your email.
