Command To Install Python In Linux

Command To Install Python In Linux

Reading time1 min
#Python#Linux#Programming#PythonInstallation#CommandLine#LinuxPython

Mastering Python Installation on Linux: The Definitive Command-Line Approach

Python is an essential language powering everything from web development and automation scripts to advanced data science applications. For developers and IT professionals working on Linux systems, having Python set up correctly and efficiently is non-negotiable. Yet, the sea of conflicting tutorials and GUI-focused guides can make installing Python more complex than it needs to be.

Forget vague tutorials and multiple conflicting commands. Discover the straightforward, command-focused method to install Python on any Linux distribution with confidence and control.


Why Command-Line Installation?

The command line offers reliability, speed, repeatability, and the ability to automate installations across machines. Using terminal commands ensures that you are not reliant on fluctuating UI elements or third-party tools, giving you direct control over your environment.


Step-by-Step Guide: Installing Python via CLI on Linux

Step 1: Check Your Current Python Version

Before installing anything new, first verify whether Python is already installed on your machine.

python3 --version

or

python --version

Most modern Linux distros ship with Python 3 pre-installed — usually Python 3.6 or higher. However, sometimes the default might be an older version or even missing entirely.


Step 2: Update Your Package List

Before installation or upgrade, always update your system’s package index. This keeps repositories in sync to ensure you get the latest versions.

For Debian/Ubuntu-based systems:

sudo apt update

For Fedora:

sudo dnf check-update

For CentOS/RHEL:

sudo yum check-update

Step 3: Install Python

Python’s package name varies slightly depending on your distribution.

Debian/Ubuntu-based Distros:

To install Python 3 (latest available in repos):

sudo apt install python3

To also get pip (Python package manager):

sudo apt install python3-pip

Fedora:

sudo dnf install python3 python3-pip

CentOS/RHEL 8+ (with EPEL repository enabled):

sudo yum install python3 python3-pip

Step 4: Verify the Installation

Confirm everything installed correctly by checking versions:

python3 --version
pip3 --version

If both commands return version numbers without errors, you’re all set!


Managing Multiple Python Versions Using update-alternatives (Debian/Ubuntu)

Sometimes you might want a specific version of Python if multiple versions coexist. Here’s how to configure which version runs when you type python or python3.

Example: Suppose you have both Python 2.7 and Python 3.8 installed.

  1. Add alternatives for python:
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.8 2
  1. Choose default version interactively:
sudo update-alternatives --config python

Select the number corresponding to the desired version.

Note: Many modern distros have deprecated python for python2, so you may focus on configuring python3 instead if needed.


Installing Specific Python Versions Not Available in Your Repos

Sometimes repositories lag behind the latest Python releases; in that case, compiling from source or using third-party tools like deadsnakes PPA (Ubuntu) help get newer versions.

Example: Install Python 3.11 on Ubuntu using deadsnakes PPA

sudo apt update && sudo apt install software-properties-common -y

sudo add-apt-repository ppa:deadsnakes/ppa

sudo apt update

sudo apt install python3.11 python3.11-venv python3.11-dev -y

# Set it as default (optional)
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.11 2

sudo update-alternatives --config python3   # choose python3.11 here if needed

# Verify installation:
python3 --version 

Bonus Tip: Use pyenv for Flexible Version Management (Optional)

If you juggle many projects requiring different Python versions, consider using pyenv, a powerful tool allowing easy installation and switching of multiple versions without interfering with system-level packages.

Install pyenv:

curl https://pyenv.run | bash
# Follow post-install instructions (add to shell profile)
exec $SHELL

# Install desired python version via pyenv:
pyenv install 3.11.4 

# Set global/default version:
pyenv global 3.11.4 

# Verify active version:
python --version  

This method isolates versions safely within your user environment.


Final Thoughts

Mastering command-line installation of Python on Linux will save you countless hours troubleshooting ambiguous GUI installers and incompatible distributions down the road.

Remember these key commands tailored for your distro workflow:

DistributionUpdateInstallVerify
Ubuntu/Debiansudo apt updatesudo apt install python3python3 --version
Fedorasudo dnf check-updatesudo dnf install python3python3 --version
CentOS/RHEL 8+sudo yum check-updatesudo yum install python3python3 --version

For more control over versions:

  • Use update-alternatives for Debian-based distros.
  • Consider pyenv for versatile project-specific setups.
  • Use PPAs like deadsnakes if official repos lag behind latest releases.

Now that you have a reliable roadmap with clear commands for any Linux flavor — go ahead and set up your ideal Python environment confidently!

Happy coding!


Got questions or a favorite command-line tip for installing Python? Drop them in the comments below!