Share on Social Media

Learn how to install Python on Rocky Linux 8 with this comprehensive guide. Follow step-by-step instructions to set up Python on your system easily and efficiently. #centlinux #linux #python

What is Python?

Python is a high-level, interpreted programming language known for its readability and simplicity. Created by Guido van Rossum and first released in 1991, Python has become one of the most popular programming languages in the world. It supports multiple programming paradigms, including procedural, object-oriented, and functional programming.

Key features of Python include:

  1. Readability and Simplicity: Python’s syntax is designed to be intuitive and easy to read, which makes it an excellent choice for beginners.
  2. Extensive Standard Library: Python comes with a large standard library that includes modules and packages for a wide range of tasks, from web development to data analysis.
  3. Cross-Platform Compatibility: Python is available on many operating systems, including Windows, macOS, and various distributions of Linux.
  4. Dynamic Typing and Memory Management: Python handles memory management automatically and supports dynamic typing, which means variables can change types.
  5. Community and Support: Python has a large and active community, which contributes to a wealth of third-party libraries and frameworks, extensive documentation, and numerous tutorials and guides.

Python is used in various applications, including web development (with frameworks like Django and Flask), data science and machine learning (using libraries like pandas, NumPy, and TensorFlow), automation, scripting, and more. Its versatility and ease of use make it a valuable tool for both beginners and experienced developers.

What is Python programming used for?

Python programming is used for a wide variety of applications due to its versatility, ease of use, and extensive libraries. Some of the key areas where Python is commonly used include:

  1. Web Development: Python frameworks like Django and Flask allow developers to build robust, scalable web applications quickly.
  2. Data Science and Analytics: Python is a popular choice for data analysis and visualization, with libraries like pandas, NumPy, and Matplotlib. It’s also widely used in machine learning and artificial intelligence with frameworks such as TensorFlow, Keras, and Scikit-learn.
  3. Automation and Scripting: Python’s simplicity makes it ideal for writing scripts to automate repetitive tasks, manage system operations, and perform file manipulations.
  4. Scientific Computing: Researchers and scientists use Python for simulations, statistical analysis, and complex scientific computations, utilizing libraries like SciPy and SymPy.
  5. Software Development: Python is often used for backend development, API development, and creating prototypes. It’s also a common choice for writing software development tools and applications.
  6. Game Development: While not as common as some other languages in the gaming industry, Python can be used for game development with libraries like Pygame.
  7. Networking: Python provides modules like Twisted and asyncio for developing network applications, handling protocols, and managing network connections.
  8. Education: Python’s readability and simplicity make it a popular language for teaching programming concepts in schools and universities.
  9. Desktop Applications: Python can be used to create cross-platform desktop applications using frameworks like Tkinter, PyQt, or Kivy.
  10. Cybersecurity: Python is used for writing tools and scripts for penetration testing, network scanning, and other security-related tasks.

Python’s flexibility and extensive ecosystem of libraries and frameworks make it a go-to language for a broad range of applications across various industries.

Installing Python Prerequisites

Connect with your Rocky Linux server as root user by means of a ssh client.

Install Python 3.10 prerequistes by using dnf command.

# dnf install -y curl gcc openssl-devel bzip2-devel libffi-devel zlib-devel tar wget make

Install Python on Rocky Linux 8

Download tarball of Python 3.10 from their official website. You can use wget or curl command for this purpose.

# cd /tmp
# wget https://www.python.org/ftp/python/3.10.4/Python-3.10.4.tar.xz
--2022-04-23 21:46:50--  https://www.python.org/ftp/python/3.10.4/Python-3.10.4.tar.xz
Resolving www.python.org (www.python.org)... 199.232.44.223, 2a04:4e42:48::223
Connecting to www.python.org (www.python.org)|199.232.44.223|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 19342692 (18M) [application/octet-stream]
Saving to: ‘Python-3.10.4.tar.xz’

Python-3.10.4.tar.x 100%[===================>]  18.45M   751KB/s    in 20s

2022-04-23 21:47:11 (947 KB/s) - ‘Python-3.10.4.tar.xz’ saved [19342692/19342692]

Extract downloaded tarball as follows.

# tar -xf Python-3.10.4.tar.xz -C /opt/

Change to extracted directory of Python and configure the downloaded Python source according to your Linux operating system.

# cd /opt/Python-3.10.4
# ./configure --enable-optimizations

Install Python 3.10 by using following commands.

# make -j 2
# make altinstall

Verify the version of installed Python as follows.

# python3.10 -V
Python 3.10.4

If you want to develop some amazing projects in Python Programming Language, then you should start with Python Crash Course, 2nd Edition (PAID LINK) by Eric Matthews. Or you can buy an Online Course: Complete Python Tutorial for Beginnersshow?id=oLRJ54lcVEg&bids=1060093 to speedup your learning process.

How to run a Python Script?

Running a Python script in Linux using the bash terminal is straightforward. Here’s a step-by-step guide:

Step 1: Write Your Python Script

  1. Open a text editor (e.g., nano, vim, or gedit).
  2. Write your Python code.
  3. Save the file with a .py extension, e.g., script.py.

For example, using nano:

nano script.py

Step 2: Open the Terminal

If you’re not already in the terminal, open it.

Step 3: Navigate to the Directory Containing the Script

Use the cd command to change the directory to where your Python script is located. For example, if your script is in the Documents folder:

cd ~/Documents

Step 4: Run the Python Script

You can run the Python script by typing python or python3 followed by the script’s filename. The exact command depends on your Python installation.

If you have Python 3 installed:

python3 script.py

If you have Python 2 installed (less common as Python 2 is deprecated):

python script.py

Step 5: Make the Script Executable (Optional)

If you want to run the script without typing python or python3, you can make it executable and add a shebang line at the top of the script.

  1. Add a shebang line to the top of your script:
   #!/usr/bin/env python3
  1. Make the script executable:
   chmod +x script.py
  1. Run the script directly:
   ./script.py

Example

Here’s a complete example:

  1. Create a script called hello.py:
   nano hello.py
  1. Add the following Python code to hello.py:
   #!/usr/bin/env python3
   print("Hello, World!")
  1. Save and exit (Ctrl + X, then Y, then Enter).
  2. Make the script executable:
   chmod +x hello.py
  1. Run the script:
   ./hello.py

By following these steps, you can easily run a Python script in Linux using bash.

How to uninstall Python?

Uninstalling Python in Rocky Linux 8 can be done using the package manager dnf. However, you should proceed with caution because Python is a critical dependency for many system tools and utilities. Removing the system-installed version of Python can cause system instability. Instead, if you need to remove a specific Python version that you installed manually, it’s generally safer.

Here are the steps to uninstall Python safely:

Uninstalling a Manually Installed Python Version

If you’ve installed Python from source or using an alternative method (e.g., pyenv), you should follow the uninstallation steps specific to that method.

Uninstalling a Python Version Installed via pyenv

If you installed Python using pyenv, you can uninstall it as follows:

List the installed Python versions.

pyenv versions

Uninstall the specific version.

env uninstall <version>

Uninstalling System Python Packages (Use with Caution)

To uninstall Python packages that were installed via dnf, follow these steps:

Identify the Python packages: First, list the installed Python packages to identify what you need to remove

dnf list installed | grep python

Uninstall the Python packages: Use dnf remove to uninstall the packages. For example, to remove Python 3.

sudo dnf remove python3

Check for dependencies: The package manager will show you the dependencies that will be removed along with Python. Carefully review this list to ensure that no critical system components are affected.

Important Considerations

  • System Integrity: Ensure that you are not removing the default Python version that is essential for system operations.
  • Use Virtual Environments: To avoid conflicts and the need to uninstall system Python, consider using virtual environments (using venv or virtualenv) for your projects. This allows you to manage different Python versions and dependencies independently of the system Python.

By following these steps carefully, you can manage your Python installations on Rocky Linux 8 without compromising system stability.

Python Cheat Sheet

Here is an effective Python Cheat Sheet from DataCamp.com for print and your handy use.

Video: How to install Python on Rocky Linux 8

YouTube player

Final Thoughts

Installing Python on Rocky Linux 8 is a straightforward process that opens up a world of programming possibilities. Whether you’re a beginner looking to start your coding journey or an experienced developer setting up a new environment, these steps will help you get Python up and running smoothly.

If you need further assistance or prefer to have a professional handle the installation and setup for you, feel free to check out my services on Fiverr. I’m here to help with all your Python and Linux needs!

Leave a Reply

Your email address will not be published. Required fields are marked *