MakeUseOf logo

How to Create a Python Virtual Environment on Ubuntu

Beige Python on Brown Branch of Tree
No attribution required (Image from Pexels)
David is a freelance writer with a background in print journalism, and a love of Free and Open Source Software. He has been using Linux since the early 2000s, and is a regular contributor to Linux Format magazine in the UK. He runs a range of sites and services from a Raspberry Pi perched precariously atop his living room couch, and never passes up a chance to take a stray edX course to better his understanding of technology, humanity, and other, related matters.

David is a terrible guitar player, and he spends his free time touring the British Isles, off-grid, with his caravan and dogs. Occasionally, he writes books. No-one likes them. See what he's up to at davidrutland.com
Sign in to your MakeUseOf account

Python is one of the most popular and versatile programming languages in the world—powering tens of thousands of apps for Linux, Windows, and macOS.

While Ubuntu releases prior to 23.04 were able to install Python packages with a single command, more recent versions require that you install Python packages in a virtual environment. Here's one easy way to create and use a Python virtual environment on Ubuntu.

Why Use Python Virtual Environments on Ubuntu?

[画像:pip install error on ubuntu 23.04]
Screenshot by David Rutland - no attribution

Traditionally, to install a Python project on Linux, you would first make sure you have Pip and Python installed, then use the following command:

pip install some_app

Pip (Pip Installs Packages) will choose the most recent version of the package from the Python Package Index, automatically install any dependencies, and configure the package to work with your Python environment.

Problems can arise due to conflicting dependencies, and Python can also fall out with Ubuntu's Advanced Package Tool (APT).

You can get around these issues by using isolated virtual environments for Python projects, containing Pip and Python. You can then use these to install Python packages.

Starting with Ubuntu 23.04, the pip install and pip3 install commands won't work at all, and you'll see an "externally-managed-environment" error.

The accompanying message will go on to recommend installing the package with APT, but in most cases, this simply won't be an option.

How to Create a Python Virtual Environment on Ubuntu

[画像:install python3-venv on ubuntu]
Screenshot by David Rutland - no attribution

To create Python virtual environments on Ubuntu, you need the python3-env package. Install it by entering the following command in your terminal:

sudo apt install python3-venv

You can now use python3-venv to create virtual environments:

python3 -m venv ~/cool_python_apps

This command will create a directory called "cool_python_apps" in your home directory, containing everything you need to install any Python package. Pip, Pip3, and Pip3.11, along with equivalently versioned Python binaries are located in the bin subdirectory.

To install any Python app, you will need to use a specific binary from within the virtual environment.

For instance, you can install the excellent Castero terminal podcast app with:

~/cool_python_apps/bin/pip3 install castero

The binary will also be placed in the bin subdirectory of the virtual environment.

Use Pip the Old Way on Ubuntu

If you're running Ubuntu 23.04 or later and want the same user experience as before the policy change, create a new virtual environment for all your Python and Pip packages:

python3 -m venv ~/cool_python_apps

Create an alias for the pip3 command with:

echo 'alias pip3="~/cool_python_apps/bin/pip3"' >> .bashrc
source .bashrc

Now add the new bin directory to your path:

export PATH=~/cool_python_apps/bin:$PATH

You will once again be able to install packages using:

pip3 install package_name

Repeat as required for pip, pip3.11, and Python.

There Are Other Ways to Install Apps on Linux

Many projects come packaged in a variety of formats. If you don't like the idea of Python packages installed in virtual environments, see if they're available in one of the other great Linux software repositories or app stores.

AltStyle によって変換されたページ (->オリジナル) /