Python 3 installing on Rocky Linux 9
The beginning of the text or presentation.
Python, a flexible programming language, can be utilized for diverse programming ventures. The creators of Python, who sought to craft an enjoyable language, drew inspiration from the British comedy troupe Monty Python. As its popularity grows across various domains, Python stands as an excellent choice for both novices and seasoned developers.
This guide will assist you in installing Python 3 on a Rocky Linux 9 server and establishing a programming environment using the command line.
Requirements or conditions that must be met beforehand
To proceed, a non-root superuser account is required on your Rocky Linux 9 server.
To get this started, you can refer to our guide on initiating the server setup for Rocky Linux 9.
First, we need to get the system ready.
Before starting the installation process, we should ensure that the default system applications are updated to have the most recent versions.
We will utilize DNF, the open-source package management tool, which is the advanced version of YUM (Yellowdog Updater, Modified). DNF is the default package manager for Red Hat based Linux systems such as Rocky Linux. It allows for the installation, updating, and removal of software packages on your server.
To begin with, let’s ensure that our package manager is current by executing this command:
- sudo dnf update -y
Using the -y flag notifies the system that we acknowledge the modifications being made, eliminating the need for the terminal to ask for confirmation.
Once all the installation is completed, our setup is ready and we can proceed with installing Python 3.
Step 2 involves the installation and configuration of Python 3.
Rocky Linux originates from RHEL (Red Hat Enterprise Linux), prioritizing stability. Consequently, the sources of upstream packages tend to prefer tried and stable application versions. Though the installed Python version might not be the latest release by default, Python versions are typically supported for extended periods.
- sudo dnf install python3 -y
Once this procedure is finished, we can verify the successful installation by using the python3 command to check its version number.
- python3 -V
After successfully installing Python 3, we will receive the following output:
Python 3.9.10
Afterwards, we will proceed with the installation of the Rocky Linux Development Tools. These tools enable you to build and compile software directly from the source code.
- sudo dnf -y groupinstall development
Once it is installed, we will discuss the process of setting up Python development projects in the following section.
Step 3 involves the establishment of a virtual environment.
Once Python is installed and our system is ready, we can proceed to establish our programming environment using venv.
Virtual environments provide a segregated area on your computer where you can work on Python projects independently. This allows each project to have its own distinct set of requirements without causing any interference with your other projects.
Creating a programming environment grants us enhanced authority over our Python projects, along with various packages and versions. This becomes particularly crucial while working with external packages.
There is no limit to the number of Python programming environments you can create. Each environment is basically a folder or directory on your server, containing scripts that enable its setup as an environment.
You can either select a directory where you want to store your Python programming environments or create a new directory using “mkdir.”
- mkdir environments
- cd environments
Once you have navigated to the desired directory to house the environments, you can generate an environment by executing the subsequent command. It is advisable to choose an appropriate name for your environment, but for demonstration purposes, we will refer to it as “my_env.”
- python3 -m venv my_env
In simple terms, pyvenv creates a new directory with a few items that can be viewed using the ls command.
- ls my_env
bin include lib lib64 pyvenv.cfg
These files function collectively to separate your Python tasks from the wider setting of your local computer, preventing any merging of system files and project files. This is a recommended approach for version control purposes and guarantees that each of your projects can utilize the specific packages it requires.
In order to utilize this environment, you must activate it. This can be achieved by entering the command mentioned below, which triggers the activation script located in the bin directory.
- source my_env/bin/activate
Now, the name of your environment, which is my_env, will be added as a prefix to your prompt.
This prefix indicates that the current active environment is my_env, indicating that any programs created here will solely utilize the settings and packages specific to this environment.
The preinstalled Python package manager, known as pip, will be utilized to install and control programming packages that may be needed for our development projects. To install Python packages, simply enter the command:
- sudo pip install package_name
In this case, “package_name” can be any Python package or library, such as Django for developing websites or NumPy for scientific computations. Therefore, if you wish to install NumPy, you can achieve this by executing the pip install numpy command.
Note
Once you have completed these steps, your virtual environment is now prepared for use.
Step 4 involves the creation of a program that displays the message “Hello, World!”
Once we have successfully configured our virtual environment, we can proceed to develop the traditional “Hello, World!” program as a means to verify our setup and enhance our understanding of Python, in case we are not already familiar with it.
Rocky Linux 9 comes with vi as the default text editor. Although vi is highly capable, it may pose difficulties for inexperienced users. To simplify configuration file editing on your Rocky Linux 9 server, you may consider installing a more user-friendly editor like nano.
- sudo dnf install nano
Create a new file using nano or your preferred text editor.
- nano hello.py
Include a solitary line in the document.
print("Hello, World!")
To save and close the file, press Ctrl+X if you’re using nano, and when asked, press Y followed by Enter.
After closing nano and going back to your shell, execute the program.
- python hello.py
The output that should be displayed in the terminal when the hello.py program is run is as follows:
Hello, World!
If you want to exit the environment, simply enter the command “deactivate” and you will be directed back to your initial directory.
In summary, to conclude.
Well done! You’ve successfully established a Python 3 programming environment on your Rocky Linux 9 server and are now ready to start your coding project.
Once your computer is prepared for software development, you can enhance your understanding of coding in Python either by following our How To Code in Python series or by obtaining the HowTo Code in Python eBook for free.
If you want to delve into machine learning projects, you can consult our eBook on Python Machine Learning Projects.
more tutorials
The top seven Linux distributions for laptops(Opens in a new browser tab)
Partition in Linux Step-by-Step Guide(Opens in a new browser tab)
Installation of Arch Linux(Opens in a new browser tab)