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.
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.
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
-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.
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:
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.
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
- 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:
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
- 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:
- sudo pip install package_name
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
pip instead of
pip3. If you use Python 3 or pip3 on your machine outside of an environment, you will need to use the
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.
To do this, we’ll open up a command-line text editor such as vi and create a new file:
- 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:
ESC to leave insert mode. Next, type
ENTER to save and exit the file.
We are now ready to run our program:
- python hello.py
The hello.py program that you just created should cause the terminal to produce the following output:
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 CentOS 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.