Raspberry Pi

How to Fix “error: externally-managed-environment” on Raspberry Pi Bookworm

Raspberry Pi is a credit card size computer that has revolutionized the electronics and programming world. Its versatility and affordability have made it the most beloved choice among hobbyists, developers, and professionals. The Raspberry Pi foundation has released a new operating system called Raspberry Pi Bookworm that includes several enhancements and powerful features compared to previous Bullseye versions. However, it also brought along a new challenge: “error: externally-managed-environment”. This error occurs when you try to install packages on your Raspberry Pi system from the pip installation command.

This guide will show you:

Why “error: externally-managed-environment” Occurs on Raspberry Pi Bookworm

How to Fix “error: externally-managed-environment” on Raspberry Pi Bookworm

How to Fix Update the Hashes Error on Raspberry Pi Bookworm

Conclusion

Why “error: externally-managed-environment” Occurs on Raspberry Pi Bookworm

Raspberry Pi is a Linux-based operating system, which is well-known for improving system security. Whenever you see a message “error: externally-managed-environment”, it indicates that the Python packages installed on your system cannot be modified or deleted. With these restrictions, no one will be able to manipulate or disrupt the integrity of the operating system. It is understood that pip installs packages into the system site-packages directory. This directory is considered as a part of the externally managed environment, thus whenever you try to disrupt it, you will encounter this error:

How to Fix “error: externally-managed-environment” on Raspberry Pi Bookworm

You can fix “error: externally-managed-environment” on Raspberry Pi Bookworm:

  • By Adding –break-system-packages with pip Command
  • By Adding break-system-packages Inside the pip Configuration File
  • By Removing the EXTERNALLY MANAGED File from the System
  • By Creating a Virtual Environment
  • By Using pipx Instead of pip

How to Fix “error: externally-managed-environment” on Raspberry Pi Bookworm by Adding –break-system-packages with pip Command

The –break-system-packages flag is used with the pip command to override the default behavior of pip. It prevents users from installing packages into the system site-packages repository if the environment is marked as externally managed. If you are experiencing the “error: externally-managed-environment” error while running the pip command on Raspberry Pi Bookworm, you can simply add –break-system-packages right after the packages name with a space, as shown below:

pip install <package_name> --break-system-packages

Or

pip3 install <package_name> --break-system-packages

How to Fix “error: externally-managed-environment” on Raspberry Pi Bookworm by Adding break-system-packages Inside the pip Configuration File

Besides adding –break-system-packages with each command, you can open the pip configuration file and add the break-system-packages with a true value. This will enable the break-system-packages with each pip command and fix the “error: externally-managed-environment” error on the system. You can use the following steps to complete adding break-system-packages inside the pip configuration file:

Step 1: Check the Location of pip Configuration File

The location of the pip configuration file may be different based on your system. You can find the location of pip configuration file on Raspberry Pi Bookworm using the following command:

python3 -m pip config debug

From the above command, you will be able to retrieve the location of the pip configuration file. Though there are multiple locations, the real location is the one with the True status, which is /etc/pip.conf in my case.

Step 2: Open pip Configuration File

Now open the pip configuration file on your Raspberry Pi Bookworm terminal using the following command:

sudo nano /etc/pip.conf

Don’t forget to replace /etc/pip.conf with the configuration file location you get from the above command in Step 1 if you have a different location.

Step 3: Add break-system-packages Keyword Inside the pip Configuration File

Inside the pip configuration file, you must add break-system-packages keyword with the value true, as shown below:

break-system-packages = true

Step 4: Save the pip Configuration File

After adding the break-system-packages, you must save the file using CTRL+X, add Y and enter. This enables the break-system-packages with the pip command.

Step 5: Run the pip Command Again

After successfully installing pip, you can use pip or pip3 command again on your Raspberry Pi Bookworm terminal to install a package:

pip install <package_name>

Or

pip3 install <package_name>

How to Fix “error: externally-managed-environment” on Raspberry Pi Bookworm by Removing the EXTERNALLY MANAGED File from the System

The EXTERNALLY MANAGED is a file present in the Python source directory that indicates a Python specific tool like pip. It permits users to install or remove packages into the interpreter default package environment. Removing this file from your Raspberry Pi system will allow you to install packages through pip without any restriction. You can remove the EXTERNALLY MANAGED file from Raspberry Pi Bookworm by using the following command:

sudo rm /usr/lib/python3.11/EXTERNALLY_MANAGED

After removing the file, you can then be able to install the package through pip or pip3.

Note: In case you want to retrieve the changes back to where they are, you can simply create the file at the given location using the touch command with the file name, as given below:

sudo touch /usr/lib/python3.11/EXTERNALLY_MANAGED

How to Fix “error: externally-managed-environment” on Raspberry Pi Bookworm By Creating a Virtual Environment

Creating a virtual environment is an effective way that helps you isolate program-specific packages from the system and allows an organized installation of packages. By creating a virtual environment on Raspberry Pi Bookworm, you can fix the “error: externally-managed-environment” on your system.

You can create a virtual environment on your Raspberry Pi system using the following steps:

Step 1: Create a Virtual Environment

First, create a virtual environment with the name venv by running the following command in the terminal:

python3 -m venv venv

Step 2: Activate the Virtual Environment

After creating the virtual environment on Raspberry Pi, you can activate it from the following command:

source venv/bin/activate

Once the virtual environment is created, you can then be able to run the pip command on Raspberry Pi and install packages on your system.

How to Fix “error: externally-managed-environment” on Raspberry Pi Bookworm By Using pipx Instead of pip

pipx is an effective package manager that allows you to install Python packages similar to the one installed through pip. However, it uses an isolated environment for package installation, making it easy for you to run on your system. If you don’t like using the above-mentioned methods, you can install pipx on your Raspberry Pi system. Then use it as your default package manager to install Python packages. You will not encounter the “error: externally-managed-environment” error while running it on your system. For a complete guide on how to install and use pipx on Raspberry Pi, read here.

Note: You can also try removing the pip source directory and install pip again to fix the installation. However, that won’t be an effective solution when you have other ways to do so. You can remove the pip source directory from Raspberry Pi Bookworm using the following command:

sudo rm -rf /usr/bin/pi

How to Fix Update the Hashes Error on Raspberry Pi Bookworm

After changing the pip settings and running the pip commands again for installation on Raspberry Pi Bookworm, you may encounter “hashes update error”, as shown below:

This error will prevent you from installing a package through pip. The simple solution to fix this error is to update the pip or pip3 on Raspberry Pi using the following command:

python -m pip install --upgrade pip

After that, you can install a python package from pip on Raspberry Pi without encountering any error.

Conclusion

The “error: externally-managed-environment” is a common error encountered by the users of Raspberry Pi Bookworm. This error prevents users from installing Python packages from the pip installation command. This error can be fixed by adding –break-system-packages after the pip command or adding break-system-packages inside the pip configuration file. You can also remove the EXTERNALLY MANAGED file from the system, create a virtual environment or use pipx instead of pip to install Python packages on Raspberry Pi system. The complete details for all the methods to fix

“error: externally-managed-environment” is provided in the above section of this guide.

About the author

Awais Khan

I'm an Engineer and an academic researcher by profession. My interest for Raspberry Pi, embedded systems and blogging has brought me here to share my knowledge with others.