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

Install Drupal on Ubuntu 18.04 with Composer, Nginx, and PHP

Drupal is one of the most widely used open-source CMS platforms in the world. Written in…

2 hours ago

Set Up an FTP Server on Ubuntu 18.04 with vsftpd and SSL

FTP (File Transfer Protocol) is a standard network protocol for transferring files between a local client…

2 hours ago

Install Slack on Ubuntu 18.04: Deb Package Install Guide

Slack is one of the most popular collaboration platforms in the world. Teams use it to…

2 hours ago

Change Hostname on Ubuntu 18.04: hostnamectl and /etc/hosts Guide

The hostname is the label that identifies a machine on a network. It appears in your terminal…

2 hours ago

Install and Configure Redis on Ubuntu 18.04: Remote Access Guide

Redis is an open-source, in-memory data structure store used as a database, cache, and message broker.…

23 hours ago

Upgrade Ubuntu 16.04 to 18.04 Bionic Beaver: Step-by-Step Guide

Ubuntu 18.04 LTS (Bionic Beaver) was released on April 26, 2018, with five years of official…

24 hours ago