Senior DevOps Technical Writer
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.
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.
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:
- 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.
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.
- 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:
- python3 -V
With a version of Python 3 successfully installed, we will receive the following output:
OutputPython 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:
- sudo dnf -y groupinstall development
With that installed, we’ll go over how to set up Python development projects in the next section.
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:
- mkdir environments
- 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
.
- 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:
- ls my_env
Outputbin 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:
- 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:
- 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.
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:
- sudo dnf install nano
Using nano
or your favorite text editor, create a new file:
- nano hello.py
Add a single line to the file:
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:
- python hello.py
The hello.py program that you just created should cause the terminal to produce the following output:
OutputHello, World!
To leave the environment, type the command deactivate
and you’ll return to your original directory.
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.
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:
why are you using sudo when installing a package in a virtual environment ?