Verified and Tested 04/04/21
This article covers the installation of Python 3.9 on a CentOS 7 operating system and how to create a Virtual Environment(venv) with pyvenv for which Python 3 can run. It is essential to note the directories that we are installing is Python 3.9. CentOS 7.x is dependent on Python 2.x to function correctly, do not overwrite the Python 2 installation accidentally. If you still need to install Python 2, follow our how-to on this here.
In This Article
You’ll need to yum install the following packets to meet all the Python 3 and PIP dependencies:
– You need a CentOS 7 server. If you do not have a server already, you can spin up a dependable virtual private server from Atlantic.Net in under 30 seconds. See our VPS hosting price.
– “Development Tools” “Development Libraries”
Installing Python 3 from source on CentOS 7
Install build tools
yum groupinstall "Development Tools" "Development Libraries"
Install readline-devel so that your keyboard arrow keys work when entering the Python3 interactive terminal
yum -y install readline-devel
Install OpenSSL-devel for pip3
yum -y install openssl-devel
Download Latest source code from Python, at the time of writing
Unpack and enter Python-3.9.0 directory
tar -xvzf Python-3.9.0.tgz
Build compile environment
./configure --prefix=/opt/Python-3.9.0 make && make install
Creating a Virtual Environment in CentOS 7
You’ll see that Python3 has been installed into /opt/Python3.9.0. Note that by installing Python3, you have not altered anything with your original Python. This is done purposely if you install Python3 on top of your current Python2.x installation, you’ll break your operating system. This is why we are going to build a Virtual Environment for which we can run python3. This allows you to make changes to your Python3 installation without altering the OS’s version of python3. This is useful if different apps require different versions of Python. It also keeps you from accidentally overwriting your OS’s Python2 files.
Use the Python3 command to create your environment. We’ll make our environment in /home/ and call it py3venv; it will be created automatically if it doesn’t already exist.
python3 -m venv /home/py3venv
To start, enter into your virtual Python3 environment execute the following. Your command shell will change to reflect that you are in your virtual environment.
source /home/py3venv/bin/activate (py3venv) [root@centos7 Python-3.9.0]#
Once inside the virtual environment, you may use PIP to install any additional packages you may require without affecting the system-wide instance of your Python 3 installation.
To exit the virtual environment perform the following to drop back into your normal shell:
(py3venv) [root@centos7 Python-3.9.0] deactivate [root@centos7 Python-3.9.0]#
Thank you for following along and feel free to check back with us for further updates or learn more about our reliable VPS hosting servers.