To install Python on a Linux system, follow the steps based on your Linux distribution. Most Linux distributions come with Python pre-installed, but you can install or upgrade it if needed.
For Ubuntu/Debian-based systems:
Step 1: Update packages
sudo apt update
sudo apt upgrade
Step 2: Install Python (3.x)
sudo apt install python3
Step 3: Install pip (Python package manager)
sudo apt install python3-pip
Step 4: Verify installation
python3 --version
pip3 --version
For RedHat/CentOS/Fedora-based systems:
Step 1: Update packages
sudo dnf update # Use `yum` instead of `dnf` for CentOS 7
Step 2: Install Python
sudo dnf install python3
Step 3: Install pip
sudo dnf install python3-pip
Step 4: Verify installation
python3 --version
pip3 --version
If you want to install the latest version from source:
Step 1: Install build dependencies
sudo apt update
sudo apt install -y make build-essential libssl-dev zlib1g-dev \
libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm \
libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev
Step 2: Download and compile Python
cd /usr/src
sudo wget https://www.python.org/ftp/python/3.12.3/Python-3.12.3.tgz
sudo tar xzf Python-3.12.3.tgz
cd Python-3.12.3
sudo ./configure --enable-optimizations
sudo make -j$(nproc)
sudo make altinstall
Step 3: Verify
python3.12 --version