Check out the error below:
jbtechnix@jbtechnix:~$ pip install pygame error: externally-managed-environment
× 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.12/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.
Solutions: Use a Virtual Environment: This is the most common and recommended solution. You create a virtual environment where you can freely install packages without affecting the system-wide Python environment.
Steps:
- First, make sure you have python3-venv installed on your system. You can install it via:
sudo apt install python3-venv
- Create a virtual environment:
python3 -m venv myenv
- Activate the virtual environment:
source myenv/bin/activate
- Now, install pygame:
pip install pygame
- When you're done, you can deactivate the virtual environment with:
deactivate
Use pipx: pipx is a tool to run Python applications in isolated environments. It is especially useful for installing and running Python applications that are not packaged as part of the system. Steps:
- First, make sure pipx is installed:
sudo apt install pipx
- Install pygame using pipx:
pipx install pygame
This will install pygame in an isolated environment, ensuring that your system Python remains unaffected.
Use --break-system-packages (Not Recommended): This option bypasses the system restrictions and forces pip to install the package globally. However, using this can potentially break your system's Python installation and package management. It’s generally not recommended unless you know what you’re doing.
To force the installation with this option:
sudo pip install pygame --break-system-packages
The best option is to either create a virtual environment or use pipx. Both will allow you to install and manage pygame independently from the system Python environment.