How To Check If Python Is Installed Linux – Running Python Version Command

Python installation on Linux is confirmed by running `python3 –version` in the terminal and checking for a version number. If you’re wondering how to check if python is installed linux, this guide walks you through every method, from the simplest command to advanced checks. Whether you’re a beginner or a seasoned developer, you’ll find clear steps to verify Python’s presence on your system.

Python is a core tool for many Linux tasks, from scripting to data science. Knowing if it’s installed saves you time and prevents errors. Let’s get started with the most straightforward approach.

How To Check If Python Is Installed Linux

The quickest way to check for Python is using your terminal. Open it with Ctrl+Alt+T or search for “Terminal” in your app menu. Then type one of these commands:

  1. python3 --version
  2. python --version

If Python is installed, you’ll see output like Python 3.10.12. If not, you’ll get an error like command not found: python3. That’s your first clue.

Many Linux distributions include Python by default, but not always. Ubuntu, Fedora, and Debian usually have it pre-installed. Minimal installs or older systems might not.

Why Check Both Python And Python3

Some systems use python for Python 2, while others link it to Python 3. Checking both ensures you catch any version. For example, on Arch Linux, python points to Python 3, but on older Ubuntu, it might point to Python 2.

If you get a version number from either command, Python is installed. If both fail, it’s likely not present.

Using Which Command To Locate Python

The which command shows the path to Python’s executable. Type:

which python3

If Python is installed, you’ll see something like /usr/bin/python3. If not, no output appears. This method confirms not just existence but also location.

You can also try which python. This helps if you’re unsure which version is default. The path usually ends in /bin/python3 or /usr/local/bin/python3.

Checking With Whereis Command

whereis is broader than which. It searches for binary, source, and man pages. Run:

whereis python3

Output might be python3: /usr/bin/python3 /usr/lib/python3 /etc/python3. If nothing shows, Python isn’t installed. This command is handy for finding all related files.

Verifying Python Installation With Type Command

The type command tells you how the shell interprets a command. Use:

type python3

If installed, you’ll see python3 is /usr/bin/python3. If not, you get bash: type: python3: not found. This is a shell built-in, so it works even without external tools.

It’s a quick sanity check, especially when troubleshooting path issues.

Checking Python Version Details

Once you confirm Python exists, check its full version. Run:

python3 --version

Or for more detail:

python3 -V

Both show the version number. For extended info, use:

python3 -c "import sys; print(sys.version)"

This prints the full version string, including build date and compiler. It’s useful when you need exact details for compatibility.

Checking Python 2 Specifically

Some legacy scripts need Python 2. Check with:

python2 --version

If not found, Python 2 is absent. Most modern systems don’t include it by default. You can install it via your package manager if needed.

Using Package Manager To Confirm Installation

Your Linux distribution’s package manager can list installed Python packages. Here are commands for common managers:

  • Debian/Ubuntu (apt): dpkg -l | grep python3
  • Fedora (dnf): rpm -qa | grep python3
  • Arch (pacman): pacman -Q | grep python
  • openSUSE (zypper): zypper se --installed-only python3

These commands show installed packages containing “python3” in their name. If you see entries like python3 or python3-minimal, Python is installed.

This method is reliable because it checks the system’s database, not just the PATH.

Checking For Python Development Headers

If you plan to compile Python extensions, you need development headers. Check with:

dpkg -l | grep python3-dev

Or on RPM-based systems:

rpm -qa | grep python3-devel

If missing, install them via your package manager. This step is often overlooked but critical for some workflows.

Testing Python Interpreter Directly

You can also test by running the Python interpreter interactively. Type:

python3

If installed, you’ll enter the Python shell with a >>> prompt. Type exit() to leave. If not, you get a command not found error.

This method confirms the interpreter works, not just the binary exists. It’s the most thorough test.

Running A Simple Python Script

Create a test script to verify functionality. Use:

echo 'print("Python is working")' > test.py
python3 test.py

If you see “Python is working”, everything is fine. If errors appear, Python might be installed but broken.

This step catches issues like missing libraries or permission problems.

Common Issues When Checking Python

Sometimes Python is installed but not in your PATH. This happens if it’s installed in a custom location. Use find to search:

sudo find / -name "python3" -type f 2>/dev/null

This searches the entire filesystem. It might take a while but finds any Python binary.

Another issue is multiple Python versions. Use ls /usr/bin/python* to list all Python executables. You might see python3.8, python3.10, etc.

Permission Denied Errors

If you get “Permission denied” when running Python, check file permissions:

ls -l /usr/bin/python3

It should show -rwxr-xr-x. If not, fix with sudo chmod +x /usr/bin/python3. This is rare but can happen after manual installations.

Installing Python If Missing

If Python isn’t installed, install it using your package manager. For Ubuntu/Debian:

sudo apt update
sudo apt install python3

For Fedora:

sudo dnf install python3

For Arch:

sudo pacman -S python

After installation, verify with python3 --version. You can also install python3-pip for package management.

Installing Python From Source

If you need a specific version, compile from source. Download from python.org, then:

tar -xf Python-3.x.x.tar.xz
cd Python-3.x.x
./configure --enable-optimizations
make
sudo make install

This gives you full control but takes longer. Most users prefer the package manager method.

Checking Python In Virtual Environments

If you use virtual environments, Python might be installed globally but not in your current environment. Activate the environment first:

source myenv/bin/activate
python --version

If the environment uses a different Python version, you’ll see that. Deactivate with deactivate to return to system Python.

This is important for projects with specific version requirements.

Using Pyenv To Manage Versions

Pyenv lets you install and switch between Python versions. Check installed versions with:

pyenv versions

If pyenv is installed but no versions show, you need to install one via pyenv install 3.10.0. This tool is great for development.

Automating The Check With A Script

You can write a bash script to check Python automatically:

#!/bin/bash
if command -v python3 &> /dev/null; then
    echo "Python3 is installed"
    python3 --version
else
    echo "Python3 is not installed"
fi

Save as check_python.sh, make executable with chmod +x check_python.sh, and run it. This is useful for deployment scripts.

You can expand it to check for specific versions or packages.

Checking Python In Docker Containers

If you’re inside a Docker container, Python might be minimal. Use the same commands: python3 --version. If missing, you’ll need to install it in the Dockerfile.

Many base images like python:3.10-slim include Python. Check the image documentation.

Verifying Python In WSL

Windows Subsystem for Linux (WSL) behaves like a standard Linux distro. Open your WSL terminal and run python3 --version. If not installed, use sudo apt install python3.

WSL often includes Python by default, but not always.

Frequently Asked Questions

What if python3 –version returns nothing?

If no output appears, Python is likely not installed. Try python --version as a fallback. If both fail, install Python via your package manager.

Can I check Python installation without terminal?

Yes, you can check via your package manager’s GUI or by looking in /usr/bin/ for Python files. However, the terminal is fastest.

How do I check if Python is installed on a remote Linux server?

SSH into the server and run python3 --version. If you don’t have SSH access, ask the server administrator or check deployment scripts.

What does “command not found” mean?

It means the shell can’t locate the Python executable in your PATH. Either Python isn’t installed, or it’s installed in a non-standard location.

Is Python 2 still available on Linux?

Most modern distros don’t include Python 2 by default. You can install it manually, but it’s deprecated and not recommended for new projects.

Final Thoughts On Checking Python

Knowing how to check if python is installed linux is a fundamental skill. The terminal commands are simple but powerful. Start with python3 --version, then use which or whereis for more detail.

If Python is missing, installation is straightforward with your package manager. Always verify after installation to ensure everything works. With these methods, you’ll never be stuck wondering if Python is available on your system.

Remember to check both Python 2 and 3 if you work with legacy code. And consider using virtual environments to manage different versions for different projects. Happy coding!