Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Installing Python on Linux
Python is a versatile programming language that can be installed on Linux systems through multiple methods. Whether you choose to compile from source or use your distribution's package manager, installing Python on Linux gives you access to the latest features and libraries.
Method 1: Installing from Source Code
Installing Python from source code provides maximum flexibility and control over features ?
Prerequisites
Ensure you have essential build tools installed ?
sudo apt-get update sudo apt-get install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev wget
Download and Install
Follow these steps to download and compile Python from source ?
- Open a web browser and go to https://www.python.org/downloads/
- Download the source code tarball for your desired Python version
- Extract the downloaded file
- Configure the build (optional: edit Modules/Setup for customization)
- Compile and install Python
wget https://www.python.org/ftp/python/3.11.0/Python-3.11.0.tgz tar -xf Python-3.11.0.tgz cd Python-3.11.0 ./configure --enable-optimizations make -j 8 sudo make altinstall
This installs Python at /usr/local/bin and libraries at /usr/local/lib/pythonXX where XX is the version number.
Method 2: Using Package Manager
Most Linux distributions include Python in their package repositories for easier installation ?
Ubuntu/Debian
sudo apt update sudo apt install python3 python3-pip
CentOS/RHEL/Fedora
sudo yum install python3 python3-pip # OR for newer versions sudo dnf install python3 python3-pip
Setting Environment Variables
The operating system uses the PATH environment variable to locate executable programs. This variable contains a list of directories that the OS searches when you run a command.
To add Python to your PATH permanently, edit your shell's configuration file ?
echo 'export PATH="/usr/local/bin:$PATH"' >> ~/.bashrc source ~/.bashrc
Verifying Installation
Check if Python was installed successfully ?
python3 --version which python3
Comparison
| Method | Advantages | Disadvantages |
|---|---|---|
| Source Code | Latest version, customizable features | Time-consuming, requires build tools |
| Package Manager | Quick, automatic dependencies | May not have latest version |
Conclusion
Installing Python from source gives you the latest features but requires more setup time. Using your distribution's package manager is faster and handles dependencies automatically. Choose based on your specific needs and time constraints.
