Skip to content
Explore the learning library ☟
SanchitGurukulTechnology · Education · Innovation Take a quiz

Python Installation on Linux: Comprehensive Package Manager, Source, and pyenv Methods

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.

Python Installation on Linux system

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.

Command
sudo apt update # For Debian-based systems (e.g., Ubuntu)
Command
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:

Command
sudo apt install python3

To install Python 2 (if needed):

Command
sudo apt install python2
  • For Red Hat-based Systems (e.g., CentOS, Fedora):

To install Python 3:

Command
sudo yum install python3

To install Python 2 (if needed):

Command
sudo yum install python2

Step 3: Verify the Installation

After installation, verify the installed version of Python.

Command
python3 --version

For Python 2:

Command
python2 --version

Method 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:
Command
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:
Command

Step 2: Download Python Source Code

Download the source code of the desired Python version from the official Python website.

Command
wget https://www.python.org/ftp/python/3.x.x/Python-3.x.x.tgz # Replace x.x with the desired version

Step 3: Extract the Downloaded File

Extract the tarball.

Command
tar -xvzf Python-3.x.x.tgz

Step 4: Compile and Install Python

Navigate to the extracted directory and compile the source code.

Command
cd Python-3.x.x ./configure --enable-optimizations make -j 4 # Adjust the number to the number of cores in your CPU

After the compilation is complete, install Python.

Command
sudo make altinstall

Note: Using make altinstall is recommended to prevent overwriting the default system Python.

Step 5: Verify the Installation

Check the installed Python version.

Command
python3.x --version # Replace x with the minor version number

Method 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:
Command
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:
Command
sudo yum install @development zlib-devel bzip2 bzip2-devel readline-devel sqlite sqlite-devel openssl-devel xz xz-devel libffi-devel git

Step 2: Install pyenv

Clone the pyenv repository and set up the environment.

Command
curl https://pyenv.run | bash

Add pyenv to your shell’s startup file (.bashrc, .zshrc, etc.):

Command

Step 3: Install Python Using pyenv

List available Python versions and install the desired one.

Command
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 version

Step 4: Verify the Installation

Check the installed Python version.

Command
python --version

Summary – 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?

0 reactions

SanchitGurukul — Technology • Education • Innovation

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.

This field is not used to determine whether a comment is safe.

Plain text only. File uploads, scripts, executable code, abuse and spam are not accepted.


Discover more from SanchitGurukul

Subscribe to get the latest posts sent to your email.

Keep exploring

Related guides

Namaste! I’m SG Saarthi 👋Need help learning or finding something?