2025, Nov 03 19:00
Install Selenium 4.2.0 on Ubuntu 24.04 in WSL: skip sudo pip, avoid apt limits, use a virtual environment
Get Selenium 4.2.0 on Ubuntu 24.04 in WSL: avoid sudo pip errors and apt limits by using a Python virtual environment and install selenium==4.2.0 from PyPI
Pinning Selenium to an exact version on Ubuntu 24.04 under WSL can be unexpectedly tricky. A system-wide install with sudo pip is blocked with an externally managed environment error, and apt won’t give you an arbitrary version on demand. Here is a clean, reliable way to get Selenium 4.2.0 (or 4.2.1) without fighting the OS.
The problem
Attempting a global install with sudo pip leads to a hard stop:
$ sudo pip install selenium==4.2.0
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.Switching to apt still doesn’t solve the version pinning:
$ sudo apt-get install python3-selenium=4.2.0
Package python3-selenium is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source
E: Version '4.2.0' for 'python3-selenium' was not foundWhy this happens
Installing packages with sudo into the system interpreter is intentionally blocked. The environment is managed by the OS tooling, so the system refuses direct pip writes. On the other hand, the Debian/Ubuntu repositories ship only one specific build of a library at a time, which rarely aligns with an exact version you need.
The Debian APT repositories only contain a single version of the various libraries, and if you need an exact version it’s likely to not match. Create a virtual environment for your application instead.
The version of Selenium in the Debian/Ubuntu repos is broken... always get it from PyPI.
The fix
Use an isolated Python environment and install from PyPI. This avoids the system interpreter entirely and lets you pin exactly the version you want.
python3 -m venv .env_isolated
source .env_isolated/bin/activate
pip install selenium==4.2.0After activation, pip installs only into this local environment. You get the requested version without touching the OS-managed Python.
Why this matters
Relying on the system interpreter couples your app to distro policies and a single packaged version, which is at odds with precise dependency management. Isolating dependencies keeps the base system stable, enables reproducible setups, and avoids broken or mismatched builds. For Selenium specifically, pulling from PyPI inside a virtual environment is the safe, expected path.
Conclusion
Skip sudo pip and don’t expect apt to satisfy exact Python version pins. Create a virtual environment, activate it, and install selenium==4.2.0 from PyPI. This aligns with how Ubuntu manages Python, gives you the exact version you need, and prevents conflicts with system packages.
The article is based on a question from StackOverflow by user93353 and an answer by Corey Goldberg.