How to Create a Python Virtual Environment on Ubuntu 20.04

how to create a python virtual environment on ubuntu 20.04
create a python virtual environment on ubuntu 20.04

In this tutorial, we will discuss the installation and usage of Python virtual environment on Ubuntu 20.04. Python virtual environment is a directory that contains a complete Python installation for a specific version of Python, including a number of additional packages and modules.

This allows users to have an unlimited number of different Python versions and modules, independent of the main version of Python installed on the system. This means that Python-based applications such as Odoo, Django or Flask, can use different Python versions and modules on a same server.

The installation and activation of Python virtual environment is a pretty fast and straightforward process and it can be done in less than 10 minutes.

Prerequisites

  • Ubuntu 20.04 VPS
  • System user with sudo or root privileges
  • SSH access to the VPS

Login and update the system

In order to start with the creation of Python virtual environments, login to the Ubuntu 20.04 VPS via SSH

ssh root@IP_Address -p Port_Number

Don’t forget to replace IP_Address and Port_Number with your server’s actual IP address and the SSH port number. We logged in as user root, but you can use any other system user with sudo privileges.

Once you are in, run the following commands to update the package index and upgrade all installed packages to the latest available version

apt update && apt upgrade

Install and activate Python virtual environment

Starting from Python version 3.6 we can use the venv Python module to create virtual environments. Before Python 3.6 the pyvenv module was used for the same purpose. In Ubuntu 20.04, the Python version included in the base system is Python 3.8. We can confirm this by checking the version of Python installed on our Ubuntu 20.04 VPS.

python3 -V
Python 3.8.10

To find which package is containing the necessary venv module, we can search the Ubuntu repositories using the following command

apt-cache search venv

The output will display all available packages in the Ubuntu 20.04 repositories

apt-venv - apt virtual environment
elpa-pyvenv - Python virtual environment interface
libmaven-enforcer-plugin-java - Maven build rule execution framework
python3-venv - pyvenv-3 binary for python3 (default python3 version)
python3.8-venv - Interactive high-level object-oriented language (pyvenv binary, version 3.8)
python3.8-full - Python Interpreter with complete class library (version 3.8)

We will be using the python3-venv package which will install the required venv module, along with some dependencies. You can use any version of Python you want available in the Ubuntu repository

apt -y install python3-venv

After the installation of the Python venv module is completed, our Ubuntu 20.04 VPS is ready for creating Python new virtual environments.

Create and activate Python virtual environment

To create your first Python virtual environment change the current working directory to the one you will use for your Python project. In our case, we will use the /opt/my_first_venv directory. To create the virtual environment run the following command

python3 -m venv /opt/my_first_venv

The command above will create a new /opt/my_first_venv directory and all necessary directories inside it containing a copy of the Python interpreter, the standard Python library, and several additional supporting files.

ls /opt/my_first_venv/
bin include lib lib64 pyvenv.cfg share

To start working inside the newly created virtual environment change the current working directory and activate it with the following commands

cd /opt/
source my_first_venv/bin/activate

When the virtual environment is activated it will change your shell’s prompt to display the name of the used virtual environment, as shown below

(my_first_venv) #

After the activation of the Python virtual environment, we can start working on our project. Or we can install and use any Python application, install or upgrade Python modules, etc. Please note that the Python application will have to be started using the Python’s binary of the newly created environment.

To install a new Python module inside the virtual environment we can use the pip manager. For example, we will install the idna Python module:

(my_first_venv)# pip install idna

Output:

Collecting idna
    Downloading idna-3.2-py3-none-any.whl (59 kB)
    |████████████████████████████████| 59 kB 8.8 MB/s
    Installing collected packages: idna
Successfully installed idna-3.2

After you are done working inside the Python virtual environment, you have to deactivate it. You can do it by running the deactivate command

(my_first_venv)# deactivate

That’s all. You have successfully installed and enabled Python virtual environment on your Ubuntu VPS. For more details about the Python virtual environments, you can check their official documentation.


Of course, you don’t need to install Python virtual environment on Ubuntu 20.04 yourself if you use one of our fully managed VPS Hosting services, in which case you can simply ask our expert Linux admins to install and set this up for you. They are available 24×7 and will take care of your request immediately.

python virtual environment on ubuntu 20.04

P.S. If you liked this post on how to install Python virtual environment on Ubuntu 20.04 please share it with your friends on the social networks by using the share shortcuts below, or simply leave a comment in the comments section. Thanks.

1 thought on “How to Create a Python Virtual Environment on Ubuntu 20.04”

Leave a Comment