Skip to main content

Installing Python on Fedora

Hello guys welcome to this tutorial on installing Python on Fedora, one of the most popular Linux distributions known for its robustness and cutting-edge features. Python is a high-level, interpreted programming language known for its readability and simplicity, making it an excellent choice for beginners and experts alike. It's widely used in web development, data analysis, artificial intelligence, scientific computing, and more.

Fedora typically comes with Python pre-installed, but it might not be the latest version, or you might need to set up a fresh environment for development purposes. This tutorial will walk you through the process of installing Python, managing versions, and setting up your development environment step by step.

Prerequisites

Before we begin, ensure you have the following:.

  • A system running Fedora.
  • Access to the terminal, which can be opened by pressing Ctrl + Alt + T.
  • Basic knowledge of using the command line interface (CLI).

Step 1: Checking the Default Python Installation

First, let's check if Python is already installed on your Fedora system and which version it is.

python --version
python3 --version

You might see output similar to this:.

Python 3.8.10
Python 3.9.1

This indicates that both Python 3.8 and Python 3.9 are installed. Fedora uses python for Python 2.x (which is deprecated) and python3 for Python 3.x.

Step 2: Updating the System

It's always a good idea to update your system's package index and upgrade existing packages before installing new software.

sudo dnf update

This command will refresh the package index and upgrade all your system packages to their latest versions.

Step 3: Installing Python 3

Even though Fedora comes with Python pre-installed, you might want to install a different version or ensure that you have the latest stable release.

sudo dnf install python3

This will install the latest Python 3 version available in the Fedora repositories. If you need a specific version, you can specify it as follows:.

sudo dnf install python39

Replace 39 with the version number you wish to install.

Step 4: Installing Development Tools

For development purposes, you'll likely need the Python development headers and a compiler. You can install these with the following command:.

sudo dnf groupinstall development
sudo dnf install python3-devel

This will install the necessary tools and libraries for compiling Python modules.

Step 5: Setting Up a Virtual Environment

Virtual environments are crucial for managing dependencies for different projects. To create a virtual environment, you'll need the venv module, which is included in Python's standard library.

python3 -m venv myenv

This command creates a new directory called myenv that contains a local Python 3 environment. To activate the virtual environment, run:.

source myenv/bin/activate

Your command prompt should now indicate that you are working inside the myenv virtual environment. To deactivate it, simply run:.

deactivate

Step 6: Installing pip

pip is the package installer for Python. You can verify if it's installed and check its version with:.

pip3 --version

If pip is not installed, you can install it with:.

sudo dnf install python3-pip

Step 7: Managing Python Versions with pyenv (Optional)

If you need to manage multiple versions of Python, pyenv is an excellent tool for the job.

First, install the dependencies required for pyenv:.

sudo dnf install -y git zlib-devel bzip2 bzip2-devel readline-devel sqlite sqlite-devel openssl-devel xz xz-devel libffi-devel findutils

Then, install pyenv:.

curl https://pyenv.run | bash

Add the following lines to your ~/.bashrc or ~/.bash_profile:.

export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init --path)"
eval "$(pyenv init -)"

After sourcing your profile with source ~/.bashrc or source ~/.bash_profile, you can install different versions of Python:.

pyenv install 3.10.2

And set the global default Python version:.

pyenv global 3.10.2

Step 8: Verifying the Installation

To verify that Python is installed correctly, run:.

python3 --version

You should see the version of Python that you installed or set as the global default.

Step 9: Running a Python Script

Create a simple Python script to test your installation:.

# test.py
print("Hello, Fedora!")

Run the script with:.

python3 test.py

You should see the output:.

Hello, Fedora!

Troubleshooting Common Issues

  • Permission Denied: If you encounter permission issues, make sure you're using sudo where necessary, and you have the correct permissions for the directories you're working in.
  • Python Not Found: Ensure that Python is correctly installed and that the python3 binary is in your PATH.
  • SSL Certificate Errors: If you face SSL certificate errors while installing packages with pip, you might need to update your certificate store with sudo update-ca-trust.

You now have a comprehensive understanding of how to install Python on Fedora, manage different versions, and set up a development environment. Thanks.