Menu
in , , ,

Installing Python and Virtualenv On CentOS Stream 9

Installing python and virtualenv on centos stream

We will try to install Python, Virtualenv the Python Virtual Environment tool, and Virtualenvwrapper extensions on CentOS Stream 9 Server.

Update CentOS Stream Server

And we will start with updating our Server, and installing the required packages as the following

# dnf update
# dnf groupinstall 'development tools'
# dnf install bzip2-devel libffi-devel

Installing Python 

The Centos appstream repository currently contains Python3.9 as the default Python package, and it will be automatically installed as a dependency package for the “Development tools” group, but anyway we can install it directly from appstream using the command

# dnf install python

Installing PIP

Directly using appstream repository by running the following command to install PIP, the package installer from Python.

# dnf install pip

Installing Virtualenv

We will install Virtualenv Using PIP as the following

# pip install virtualenv

Collecting virtualenv
  Downloading virtualenv-20.13.3-py2.py3-none-any.whl (8.7 MB)
     |████████████████████████████████| 8.7 MB 4.5 MB/s
Collecting filelock<4,>=3.2
  Downloading filelock-3.6.0-py3-none-any.whl (10.0 kB)
Requirement already satisfied: six<2,>=1.9.0 in /usr/lib/python3.9/site-packages (from virtualenv) (1.15.0)
Collecting distlib<1,>=0.3.1
  Downloading distlib-0.3.4-py2.py3-none-any.whl (461 kB)
     |████████████████████████████████| 461 kB 57.6 MB/s
Collecting platformdirs<3,>=2
  Downloading platformdirs-2.5.1-py3-none-any.whl (14 kB)
Installing collected packages: platformdirs, filelock, distlib, virtualenv
Successfully installed distlib-0.3.4 filelock-3.6.0 platformdirs-2.5.1 virtualenv-20.13.3

Installing Virtualenvwrapper Extensions

And we can install Virtualenvwrapper extensions scripts using PIP too by running

# pip install virtualenvwrapper

Create A Project Virtual Environment

Go to your working project directory, for example # cd /root/projects/web_crawler directory and start creating your project Virtualenv by running the command

# virtualenv env
created virtual environment CPython3.9.10.final.0-64 in 1570ms
  creator CPython3Posix(dest=/root/projects/web_crawler/env, clear=False, no_vcs_ignore=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/root/.local/share/virtualenv)
    added seed packages: pip==22.0.4, setuptools==60.9.3, wheel==0.37.1
  activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator

You can choose your environment name by replacing env with yours, and If you installed multiple different Python versions on your CentOS Stream you can pass--python optional argument to virtualenv command, to specify the targeted Python version of your created environment.

# virtualenv --python=python3.6.8 env

And to check the current Virtual Environment Python version of your working project, from the project directory run the command # ls env/lib and your Python project version will be listed as

# ls env/lib
python3.9

Activate Virtualenv

In your working project directory run the command

# source env/bin/activate

And so you have activated and applied an isolated environment for the current project.

Deactivate a Virtual Environment

To deactivate and exit the virtual environment, just from the working project directory run the following command

# deactivate

For CentOS Stream 8 you can apply the same steps with more appstream Python versions as we have talked about in the article Installing Python On Linux Centos Stream 8.

Leave a Reply

Exit mobile version