Tutorial

How To Install Python 3 and Set Up a Programming Environment on Rocky Linux 8

Published on January 3, 2023

Senior DevOps Technical Writer

How To Install Python 3 and Set Up a Programming Environment on Rocky Linux 8
Not using Rocky Linux 8?Choose a different version or distribution.
Rocky Linux 8

Introduction

A versatile programming language, Python can be used for many different programming projects. Inspired by the British comedy group Monty Python, the development team behind Python wanted to make a language that was fun to use. An increasingly popular language with many different applications, Python is a great choice for beginners and experienced developers alike.

This tutorial will guide you through installing Python 3 on a Rocky Linux 8 server and setting up a programming environment via the command line.

Prerequisites

You will need a Rocky Linux 8 server with a non-root superuser account.

To set this up, you can follow our Initial Server Setup Guide for Rocky Linux 8.

Step 1 — Preparing the System

Before we begin with the installation, let’s make sure to update the default system applications to ensure we have the latest versions available.

We will be using the open-source package manager tool DNF, which stands for Dandified YUM the next-generation version of the Yellowdog Updater, Modified (that is, yum). DNF is a package manager that is now the default package manager for Red Hat based Linux systems like Rocky Linux. It will let you install, update, and remove software packages on your server.

Let’s first make sure that our package manager is up to date by running this command:

  1. sudo dnf update -y

The -y flag is used to alert the system that we are aware that we are making changes, preventing the terminal from prompting us to confirm.

Once everything is installed, our setup is in place and we can go on to install Python 3.

Step 2 — Installing and Setting Up Python 3

Rocky Linux is derived from RHEL (Red Hat Enterprise Linux), which has stability as its primary focus. Because of this, upstream packages sources tend to favor tested and stable versions of applications. By default, the version of Python you install may not be the most recent release, but Python versions are usually supported for several years at a time.

  1. sudo dnf install python3 -y

When this process is complete, we can check to make sure that the installation was successful by checking for its version number with the python3 command:

  1. python3 -V

With a version of Python 3 successfully installed, we will receive the following output:

Output
Python 3.6.8

Next, we’ll install the Rocky Linux Development Tools, which are used to allow you to build and compile software from source code:

  1. sudo dnf -y groupinstall development

With that installed, we’ll go over how to set up Python development projects in the next section.

Step 3 — Setting Up a Virtual Environment

With Python installed and our system set up, we can go on to create our programming environment with venv.

Virtual environments enable you to have an isolated space on your computer for Python projects, ensuring that each of your projects can have its own set of dependencies that won’t disrupt any of your other projects.

Setting up a programming environment provides us with greater control over our Python projects, as well as over different packages and versions. This is especially important when working with third-party packages.

You can set up as many Python programming environments as you would like. Each environment is essentially a directory or folder on your server that has a few scripts to set it up as an environment.

Choose which directory you would like to put your Python programming environments in, or create a new directory with mkdir, as in:

  1. mkdir environments
  2. cd environments

Once you are in the directory where you would like the environments to live, you can create an environment by running the following command. You should use an environment name that makes sense for you, here we are calling it my_env.

  1. python3 -m venv my_env

Essentially, pyvenv sets up a new directory that contains a few items which we can view with the ls command:

  1. ls my_env
Output
bin include lib lib64 pyvenv.cfg

Together, these files work to isolate your Python work from the broader context of your local machine, so that system files and project files don’t mix. This is good practice for version control and to ensure that each of your projects has access to the particular packages that it needs.

To use this environment, you need to activate it, which you can do by typing the following command that calls the activate script in the bin directory:

  1. source my_env/bin/activate

Your prompt will now be prefixed with the name of your environment, in this case it is called my_env:

This prefix lets us know that the environment my_env is currently active, meaning that when we create programs here they will use only this particular environment’s settings and packages.

The Python package manager pip is already installed. A tool for use with Python, we will use pip to install and manage programming packages we may want to use in our development projects. You can install Python packages by typing:

  1. pip install package_name

Here, package_name can refer to any Python package or library, such as Django for web development or NumPy for scientific computing. So if you would like to install NumPy, you can do so with the command pip install numpy.

Note: Within the virtual environment, you can use the command python instead of python3, and pip instead of pip3. If you use Python 3 or pip3 on your machine outside of an environment, you will need to use the python3 and pip3 commands exclusively.

After following these steps, your virtual environment is ready to use.

Step 4 — Creating a “Hello, World!” Program

Now that we have our virtual environment set up, let’s create the traditional “Hello, World!” program to test our installation. This will make sure that our environment is working and gives us the opportunity to become more familiar with Python if we aren’t already.

The default text editor that comes with Rocky Linux 9 is vi. vi is an extremely powerful text editor, but it can be somewhat obtuse for users who lack experience with it. You might want to install a more user-friendly editor such as nano to facilitate editing configuration files on your Rocky Linux 9 server:

  1. sudo dnf install nano

Using nano or your favorite text editor, create a new file:

  1. nano hello.py

Add a single line to the file:

hello.py
print("Hello, World!")

Save and close the file. If you are using nano, press Ctrl+X, then when prompted, Y and the Enter.

Once you exit out of nano and return to your shell, let’s run the program:

  1. python hello.py

The hello.py program that you just created should cause the terminal to produce the following output:

Output
Hello, World!

To leave the environment, type the command deactivate and you’ll return to your original directory.

Conclusion

Congratulations! At this point you have a Python 3 programming environment set up on your Rocky Linux 8 server and can begin a coding project!

With your machine ready for software development, you can continue to learn more about coding in Python by following along with our How To Code in Python series, or downloading the free HowTo Code in Python eBook.

To explore machine learning projects in particular, refer to our Python Machine Learning Projects eBook.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar

Senior DevOps Technical Writer

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
1 Comments


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Hi,

In the last section of part 3 you have the following:

sudo pip install package_name

why are you using sudo when installing a package in a virtual environment ?

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.