This tutorial will teach you, step-by-step, how to install Python 3 and set up a programming environment on a CentOS 8 server.
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 CentOS 8 cloud server and setting up a programming environment via the command line.
Before you can install Python 3 you will need a CentOS 8 server with a non-root superuser account.
To set this up, you can follow our Initial Server Setup Guide for CentOS 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 CentOS. 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.
Step 2 — Installing and Setting Up Python 3
CentOS is derived from RHEL (Red Hat Enterprise Linux), which has stability as its primary focus. Because of this, tested and stable versions of applications are what is most commonly found on the system and in downloadable packages, so using the CentOS package manager you will find earlier versions of Python than the most recent release.
$ 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 -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 CentOS 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.
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:
$ 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
In this case the environment is my_env, and this new directory contains a few items that we can display if we use the
ls command in that directory:
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:
$ 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:
(my_env) [sammy@centosserver environments]$
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:
(my_env) [sammy@centosserver environments]$ sudo 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
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 — Now Python 3 is Installed on Your CentOS 8 Server, Try 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.
To do this, we’ll open up a command-line text editor such as vi and create a new file:
(my_env) [sammy@centosserver environments]$ vi hello.py
Once the text file opens up in our terminal window, we will have to type [i] to enter insert mode, and then we can write our first program:
Now press [ESC] to leave insert mode. Next, type [:x] then [ENTER] to save and exit the file.
We are now ready to run our program:
(my_env) [sammy@centosserver environments]$ 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.
Now you know how to install Python 3 and set up a programming environment on a CentOS 8 server, why not look at some of our other CentOS 8 guides:
- Initial Server Setup with CentOS 8
- How to Install the Apache Web Server on CentOS 8
- How to Secure Apache with Let’s Encrypt on CentOS 8
- How to Install Nginx on CentOS 8
- How To Set Up Multi-Factor Authentication for SSH on CentOS 8
Original Content by Lisa Tagliaferri and edited by the author of this post according to the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.