WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv

8 min read

Thank you for the warning! It is indeed good practice to use a virtual environment when using pip. Virtual environments provide a way to isolate Python environments and avoid conflicts with the system package manager.

To create a virtual environment, follow these steps:

  1. Install the virtualenv package if you haven't already:

    $ pip install virtualenv
    
  2. Navigate to the directory where you want to create the virtual environment.

  3. Create the virtual environment:

    $ virtualenv env
    

    This command creates a directory named env containing the isolated Python environment.

  4. Activate the virtual environment:

    • On Unix or Linux:

      $ source env/bin/activate
      
    • On Windows:

      $ .\env\Scripts\activate
      

    After activation, your terminal prompt will change to indicate that you are working within the virtual environment.

  5. Install packages using pip:

    $ pip install package_name
    
  6. To exit the virtual environment, use the deactivate command:

    $ deactivate
    

By using a virtual environment, you can ensure that the installation and management of Python packages are contained within the environment, avoiding system-level conflicts.