How To

Create Python Virtual Environments on Ubuntu 18.04: venv Guide

Python virtual environment is a self-contained directory that holds an isolated Python installation and its own set of packages. Each virtual environment is fully independent — a package you install for one project does not touch any other project on the same machine.

This solves a real problem. If you work on two projects that need different versions of the same library, or you want to test a new package without touching your system Python, virtual environments make that simple and safe. Ubuntu’s system Python is used by system tools, so installing packages globally with pip can cause unexpected issues.

This guide shows you how to create Python virtual environments on Ubuntu 18.04 using the built-in venv module.

<strong>Prerequisite:</strong>&nbsp;You need sudo access.

Create Python Virtual Environments on Ubuntu

Ubuntu 18.04 ships with Python 3.6 by default. Verify it is available:

bashpython3 -V
Python 3.6.5

Starting with Python 3.6, the recommended way to create virtual environments is the venv module. Install the package that provides it:

bashsudo apt install python3-venv

Navigate to the directory where you want to store your project. Create a new virtual environment:

bashpython3 -m venv my-project-env

This creates a my-project-env directory containing a copy of the Python binary, the pip package manager, and the standard library. The environment is completely isolated from your system Python installation.

Activate the virtual environment:

bashsource my-project-env/bin/activate

Once active, your shell prompt changes to show the environment name:

(my-project-env) $

The virtual environment’s bin directory is now at the front of $PATH. This means python points to the environment’s Python and pip installs packages only into this environment.

Tip: Inside the virtual environment, use python instead of python3 and pip instead of pip3.

Install Packages and Run a Python Script

With the environment active, install packages using pip. As a practical example, install the requests module:

bashpip install requests

Verify the installation:

bashpython -c "import requests"

No output means the import succeeded. Now create a simple script that sends an HTTP request to a test API and prints the response headers:

bashnano testing.py

Paste the following content:

pythonimport requestsr = requests.get('https://httpbin.org/get')print(r.headers)

Run the script:

bashpython testing.py

The output prints a dictionary of response headers from httpbin.org, confirming the requests module is installed and working inside the virtual environment.

Deactivate and Reuse Your Virtual Environments

When you are done working, deactivate the environment:

bashdeactivate

This removes the virtual environment from $PATH and returns you to your normal shell. No files are deleted.

To resume work in the same environment later, just activate it again:

bashsource my-project-env/bin/activate

You can create as many virtual environments as you need. Each project gets its own clean set of packages and dependencies. To see everything installed in the active environment, run pip list.

Python virtual environments are now set up on your Ubuntu 18.04 system. Visit the Python venv documentation to learn about advanced usage including requirements.txt for dependency management. Leave a comment below if you run into any issues.

Cyber Defence

Recent Posts

Best Email APIs for Secure Business Email: Why Developers Are Moving Beyond SMTP

Email is still one of the most important communication channels inside modern applications. Password resets,…

2 hours ago

Nginx Commands in Linux: Start, Stop, Reload, Test, and Log

Nginx is a high-performance web server and reverse proxy trusted by some of the largest…

3 days ago

ufw Command in Linux: Manage Firewall Rules with Examples

ufw (Uncomplicated Firewall) sits on top of iptables (or nftables on newer systems) and replaces…

3 days ago

who Command in Linux: Show All Logged-In Users and Sessions

When you share a server with a team or investigate unexpected activity, the first question…

3 days ago

file Command in Linux: Identify File Types Without Extensions

The file command inspects the actual contents of a file and reports its type — regardless of…

4 days ago

chattr Command in Linux: Set File Attributes with lsattr

chattr sets and removes special file attributes that operate at the filesystem level, separate from standard…

4 days ago