Applies to: Ubuntu 26.04 LTS (Python 3.14.4) and Ubuntu 24.04 LTS (Python 3.12.3); tested on both
To create and activate a Python virtual environment on Ubuntu, install the python3-venv package once, then create the environment in your project folder and source its activate script:
sudo apt update sudo apt install python3-venv cd ~/project python3 -m venv .venv source .venv/bin/activate
Your prompt now starts with (.venv), and python and pip point into the environment. Type deactivate to leave it.
Why you need a virtual environment on Ubuntu
On Ubuntu 24.04 and 26.04, pip install outside a virtual environment is blocked so it can’t break the Python that the system itself uses:
$ pip install requests
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.
A virtual environment is a folder with its own copy of pip and its own packages, so each project can have the versions it needs. Don’t get around the message with --break-system-packages; see updating Python and pip without breaking your system for why.
Step by step
- Install venv support. The minimal Ubuntu images we tested didn’t have it. Without it,
python3 -m venvfails:The virtual environment was not created successfully because ensurepip is not available. On Debian/Ubuntu systems, you need to install the python3-venv package using the following command. apt install python3.14-venvOn 24.04 the message namespython3.12-venv. Installingpython3-venvpulls in the right one for your Python version. Delete the half-made folder the failed attempt left behind (rm -rf .venv) and run the command again. - Create the environment:
python3 -m venv .venv. The name.venvis only a convention; any folder name works. Add--prompt shopto show(shop)in the prompt instead. - Activate it:
source .venv/bin/activate(or. .venv/bin/activateinsh). For fish useactivate.fish, for cshactivate.csh. - Install packages without sudo:
(.venv) $ pip install requests (.venv) $ pip freeze > requirements.txt
On another server, recreate the same set withpip install -r requirements.txtinside a new environment. - Leave it with
deactivate. After that,python3is the system Python again: in our testimport requeststhen failed withModuleNotFoundError, because the package only exists inside the environment.
Check that it is active
(.venv) $ command -v python /home/dev/project/.venv/bin/python (.venv) $ pip --version pip 25.1.1 from /home/dev/project/.venv/lib/python3.14/site-packages/pip (python 3.14)
If the path doesn’t contain your .venv folder, the environment isn’t active. Note that plain python only exists inside the environment: outside it Ubuntu has only python3 unless you install python-is-python3.
Common mistakes
- Running the script instead of sourcing it.
./.venv/bin/activategivesPermission denied, andbash .venv/bin/activateactivates a subshell that closes straight away. Always usesource. - Moving or copying the folder. The Python documentation says a virtual environment is “not considered as movable or copyable”: recreate it in the new place from
requirements.txt. Also keep it out of Git. - Scripts, cron jobs and services. You don’t have to activate anything there. Call the environment’s Python by its full path, for example in a crontab line or a systemd
ExecStart=:/home/dev/project/.venv/bin/python /home/dev/project/app.py
virtualenv or venv?
venv is built into Python and is enough for most projects. The separate virtualenv tool does the same job; it is in Ubuntu’s repositories (sudo apt install virtualenv gave version 20.36.1 on 26.04; 24.04 has 20.25.0). Create an environment with virtualenv .venv and activate it the same way. In our test it also added activate scripts for more shells, such as activate.nu for Nushell. Some older guides still use it, which is why both names appear in search results.
New to pip on servers? See installing pip on Debian.
Official documentation: venv: creation of virtual environments.
Ucartz services for this topic
- KVM VPS hosting: NVMe SSD and full root access, with Free Basic Managed Support.
- Unmanaged dedicated servers: full root access and free IPMI KVM, with Free Basic Managed Support.
- Linux server management: hardening, patching, monitoring and automation for Ubuntu, Debian, RHEL and AlmaLinux.
