• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar
  • Skip to footer
WebSetNet

WebSetNet

Technology News

  • Technology News
    • Mobile
    • Games
  • Internet Marketing
  • System Admin
    • Windows 11
    • Linux
    • Mac & Apple
    • Website Scripts
      • Wordpress
You are here: Home / System Admin / Linux / Handling Externally Managed Environment Error With Pip in Ubuntu 23.04

Handling Externally Managed Environment Error With Pip in Ubuntu 23.04

May 28, 2023 by bartez64

Handling Externally Managed Environment Error With Pip in Ubuntu 23.04

I recently upgraded to Ubuntu 23.04. Things are mostly smooth. However, recently I encountered an issue that was not present in the previous version.

I was trying to install a package using Pip, a command line based Python package manager. It works great usually but it threw an error this time:

error: externally-managed-environment

Handling Externally Managed Environment Error With Pip in Ubuntu 23.04

Here's the complete error message if you want to read it:

× This environment is externally managed
╰─> To install Python packages system-wide, try apt install
    python3-xyz, where xyz is the package you are trying to
    install.
    
    If you wish to install a non-Debian-packaged Python package,
    create a virtual environment using python3 -m venv path/to/venv.
    Then use path/to/venv/bin/python and path/to/venv/bin/pip. Make
    sure you have python3-full installed.
    
    If you wish to install a non-Debian packaged Python application,
    it may be easiest to use pipx install xyz, which will manage a
    virtual environment for you. Make sure you have pipx installed.
    
    See /usr/share/doc/python3.11/README.venv for more information.

note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages.
hint: See PEP 668 for the detailed specification.

And if you read the details, you'll understand why you see this error.

Reason behind the 'Externally Managed Environment' Error

Ubuntu 23.04, Fedora 38 and probably other recent distribution versions are implementing this enhancement on the use of Python packages.

The change has been done to avoid the "conflict between OS package managers and Python-specific package management tools like pip. These conflicts include both Python-level API incompatibilities and conflicts over file ownership."

More details can be find on this page.

PEP 668 – Marking Python base environments as “externally managed” | peps.python.orgPython Enhancement Proposals (PEPs)Handling Externally Managed Environment Error With Pip in Ubuntu 23.04peps.python.org

What are your options?

You can do three things when you try to install a Python package and see this error.

  1. Install the native package
  2. Create virtual environments in Python
  3. Use Pipx (recommended)

Let's see them one by one.

Option 1: Go for native package

I understand that Pip gives a comfortable way of installing Python packages. However, some Python applications are also packaged as APT or other native packages. Search for it in your distribution's repositories and install it from there if it is available.

For example, I was trying to install WoeUSB-ng. If I was using Arch Linux, the same package is available from AUR.

Option 2: Use Python virtual environment

If you must use a Python package, you have to isolate it in Python virtual environment.

With the help of virtual environments, you can use different versions of package dependencies and Python. This way, you avoid any conflicts between the packages.

📋This method is suitable for software developers and programmers working on Python projects.

Let's quickly see how you do it. Usually, Python should already have the tool for creating virtual environments.

Use the command below to create a virtual environment for your project. Replace project_name with your project's name, of course.

python3 -m venv .venv/project_name

If you see venv errors related, you may have to install it.

sudo apt install python3-venv

Now, you'll see a directory named .env in your home directory and inside the .env, you'll have the project directory.

Here's the exciting part. Each project directory will have its own copy of Python and Pip in it.

Handling Externally Managed Environment Error With Pip in Ubuntu 23.04

That's your virtual Python environment. You can use this 'local binary' for installing Python packages using Pip inside this virtual environment like this:

.venv/project_name/bin/pip install package_name
Handling Externally Managed Environment Error With Pip in Ubuntu 23.04

Remember that the installed Python package won't be available system wide.

3. Use Pipx tool (recommended)

What you saw above involves manual work. Pipx automates it.

It automatically creates a new virtual environment for each app you install. Not only that. It also creates a link to it in .local/bin. This way, the user who installed the package can run it from anywhere in the command line.

I guess that's what most desktop Linux users want here.

Install pipx on Ubuntu using this command:

sudo apt install pipx

It will probably install a huge number of dependencies:

Handling Externally Managed Environment Error With Pip in Ubuntu 23.04

Now add it to the PATH so that you can run from anywhere.

pipx ensurepath
Handling Externally Managed Environment Error With Pip in Ubuntu 23.04

✋You must close the terminal and log back in for the changes to occur.

Great! Now you can install Python packages using Pipx instead of Pip:

pipx install package_name

Here's an example.

Handling Externally Managed Environment Error With Pip in Ubuntu 23.04

💡To remove a package installed with pipx, use pipx uninstall package_name command.

Conclusion

Pip is a good tool for getting Python packages on the system. I think it was always meant for Python programmers, not for the end users. Clearly, it cannot be used as a replacement for the native distribution packages and the Python devs have made it clear.

The good thing is that alternatives exist for both programmers and end users.

I hope this tutorial helped you understand and overcome the externally-managed-environment error with Pip in Linux.

Please let me know if you have questions or suggestions.

Original Article

Related posts:

  1. How to Install Pip on Ubuntu
  2. How to Install Python on Windows
  3. Problem Solved: How To Check Your Python Version
  4. Monitor Linux CPU temperature, frequency, power in a graphical way
  5. How to set up and learn Python coding on a Mac
  6. Setting Up Python Environments In Linux and Unix Systems
  7. How to Save a Word Document as a JPEG
  8. Fixing “Command ‘python’ not found” Error in Ubuntu Linux
  9. Best Python IDEs for Linux
  10. Fix: There was an error opening this document

Filed Under: Linux

Primary Sidebar

Popular posts

  • 5 Ways to Fix “Your SIM sent a Text Message” Issue on iPhone
  • 3 Ways to Disable GetApps on Xiaomi, Redmi, and Poco Phones Running MIUI
  • GeForce Experience not finding games? Fix it fast
  • How To Extract & Install tar.gz Files In Ubuntu
  • Discord Stream Has No Sound? 6 Ways to Fix
  • How to Highlight Duplicates in Google Sheets
  • How to check if your Android device supports Widevine DRM
  • 8 Best Sites to Read Manga Online for Free
  • 3 Ways to Hide Tabs in Google Chrome
  • Exclamation Mark on Network Signal, Mobile Data Not Working? 8 Ways to Fix
  • How to find a lost Apple Pencil using your iPad (1st and 2nd gen)
  • How to Track a Stolen or Lost Nintendo Switch
  • How to Fix YouTube Server Connection Error [400] on Android
  • What is Android System Intelligence, and why is it on your phone?
  • How to Change the Last Modified Date, Creation Date, and Last Accessed Date for Files and Folders
  • How To Search On Google Using Image or Video
  • Microsoft Edge's newest feature? Shopping in Microsoft Edge

Footer

Tags

Amazon android Apple Asus available download: edge feature features first free from galaxy Game games gaming gets google install Intel iPhone launches linux Microsoft more OnePlus phone release released review: samsung series support this Ubuntu update using video watch what will windows with xbox your

Archives

  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org