In this guide, we explore the common issue faced by Ubuntu 24.04 users when attempting to use pip3 and encountering the "externally-managed-environment" error. We'll discuss the causes of this error, why it's becoming more common in modern Ubuntu releases, and provide step-by-step solutions to help you manage Python packages in an externally managed environment. Whether you're a beginner or an experienced developer, this article will help you get back on track with using pip3 on your Ubuntu system.
15 min
Edited:30-09-2024
When using Ubuntu 24.04, many developers encounter an error when trying to install Python packages with pip3. The error message, "externally-managed-environment," can be confusing, especially if you're accustomed to using pip3 for managing dependencies in previous Ubuntu versions. This issue arises because Ubuntu 24.04 now implements stricter package management practices
pip3 is the default package management system for Python 3 in many Linux distributions, including Ubuntu. It enables users to install and manage software packages written in Python from the Python Package Index (PyPI), as well as other repositories. The name pip3 specifically refers to the version of pip used for Python 3 environments, distinguishing it from pip, which may be used for Python 2 environments.
If pip3 is not installed by default, you can install it using:
1. sudo apt update
1. sudo apt install python3-pip
Install a package:
1. sudo pip3 install package-name
Upgrade a package:
1. sudo pip3 install --upgrade package-name
Uninstall a package:
1. sudo pip3 uninstall package-name
Show installed packages:
1. pip3 list
APT (Advanced Package Tool) is the system-wide package manager for Ubuntu, which installs software packages for the operating system, including Python libraries. However, APT typically lags behind in package versions.
pip3 is specifically designed to handle Python packages and installs them into the Python environment (which could be system-wide or within a virtual environment). pip3 gives more control over the versions and packages you need for Python development.
When I run pip install <something> on a Linux machine, you may see a similar error to this:
1. error: externally-managed-environment
2.
3. × This environment is externally managed
4. ╰─> To install Python packages system-wide, try apt install
5. python3-xyz, where xyz is the package you are trying to install.
6.
7. If you wish to install a non-Debian-packaged Python package,
8. create a virtual environment using python3 -m venv path/to/venv.
9. Then use path/to/venv/bin/python and path/to/venv/bin/pip. Make
10. sure you have python3-full installed.
This issue happens because python libraries should be installed on a virtual environment, so you will need to create the virtual environment in order to fix this issue, a great solutions is using pipx
pipx is a tool designed to help you install and run Python applications in isolated environments, allowing you to execute Python packages as standalone command-line tools. Unlike pip, which installs packages system-wide or in a virtual environment, pipx focuses on ensuring that each application you install is run in its own isolated environment, preventing conflicts between different packages.
pipx will provide you with:
Isolated environments: Each application is installed in its own virtual environment, ensuring that dependencies do not interfere with other applications.
Running applications without explicit activation: You can install a tool with pipx and run it immediately without having to activate a virtual environment manually.
Easy uninstallation: pipx makes it simple to uninstall applications, removing all associated files and dependencies.
You can install it by running:
1. sudo apt update
1. apt install pipx
Check the installation:
1. pipx --version
Then you can install your application like this:
1. pipx install python-app
You can install it with pip as well if it is available Install (if pip is available)
1. python3 -m pip install --user pipx
1. python3 -m pipx ensurepath
The ensurepath command ensures that pipx and installed applications are added to your system's PATH, so you can run them directly.
In some cases you can solve the issue by using the argument --break-system-packages, it allows you to bypass certain restrictions imposed on package installations in managed environments. however, this usage should not be abused
You can use APT to install the packages if the issue is not solved
1. sudo apt install package
Use pip in a virtual environment
1. sudo apt install python3-venv
After that create a virtual environment in your project directory:
1. python3 -m venv .venv
Activate your virtual environment using this command (it modifies your PATH environment variable to include .venv/bin/):
1. source .venv/bin/activate
Now, you can use pip install
1. pip install ...
pip remains one of the most essential tools for managing Python packages, but recent changes in Ubuntu 24.04 have introduced new challenges when using pip3, such as the "externally-managed-environment" error. This error stems from Ubuntu’s stricter control over system package management, making it harder to directly install Python packages without proper permissions. Fortunately, by understanding the root of this issue, you can apply simple fixes, such as using the --break-system-packages flag or opting for alternative tools like pipx, which allow you to run Python applications in isolated environments.
These workarounds not only resolve the immediate issues but also encourage better package management practices by isolating dependencies and minimizing conflicts. Whether you choose to stick with pip, leverage pipx for command-line tools, or explore other virtual environment managers, these approaches will ensure a smoother Python development experience on Ubuntu 24.04.
14-10-2024
This article offers a detailed guide on installing and configuring IPTables on an Ubuntu VPS. IPTables is a powerful firewall tool that helps secure your server by controlling inbound and outbound traffic. Learn how to set up rules for traffic filtering, configure basic security policies, and apply custom rules to protect your VPS.
IPtables
security
12 min
This article offers a comprehensive guide on installing and configuring ModSecurity, a powerful web application firewall (WAF), on an Ubuntu VPS. Learn how to secure your server by filtering and monitoring HTTP requests, set up ModSecurity with Nginx or Apache, and apply rules to protect against common web attacks.
Modsecurity
security
10 min
14-10-2024
This article provides a comprehensive guide on installing and configuring PHP-FPM (FastCGI Process Manager) on an Ubuntu VPS. Learn how to optimize PHP performance for your web applications by configuring PHP-FPM with Nginx or Apache, managing pools, and fine-tuning settings for efficient processing of PHP scripts.
PHP-FPM
speed
optimise
12 min