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

  1. Install venv support. The minimal Ubuntu images we tested didn’t have it. Without it, python3 -m venv fails:
    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-venv
    On 24.04 the message names python3.12-venv. Installing python3-venv pulls 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.
  2. Create the environment: python3 -m venv .venv. The name .venv is only a convention; any folder name works. Add --prompt shop to show (shop) in the prompt instead.
  3. Activate it: source .venv/bin/activate (or . .venv/bin/activate in sh). For fish use activate.fish, for csh activate.csh.
  4. Install packages without sudo:
    (.venv) $ pip install requests
    (.venv) $ pip freeze > requirements.txt
    On another server, recreate the same set with pip install -r requirements.txt inside a new environment.
  5. Leave it with deactivate. After that, python3 is the system Python again: in our test import requests then failed with ModuleNotFoundError, 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/activate gives Permission denied, and bash .venv/bin/activate activates a subshell that closes straight away. Always use source.
  • 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

Was this answer helpful? 0 Users Found This Useful (0 Votes)