How to Create Python 3 Virtual Environment on Ubuntu 20.04

Python virtual environment is used to create an isolated environment for Python project which contains interpreter, libraries, and scripts. You can create any number of virtual environments for your projects with each having its own dependencies.

By using virtual environments you avoid installing packages globally which could break other projects.

Putting it in simple words, a virtual environment helps to properly install the specific versions of the packages required by a python project.

Update system

To keep your Ubuntu 20.04 with latest packages, run the following two apt commands:

$ sudo apt update
$ sudo apt upgrade

Install pip for python3

Before installation virtual environment, lets install pip. Pip is a package manager which helps to install, uninstall and upgrade packages for your projects.

To install pip for python 3 type:

$ apt install python3-pip

Create virtual environment for python 3

Venv command is used in Python to create virtual environment. The venv package is available in Ubuntu repository.

Lets first install venv package using the following command:

$ apt-get install python3-venv

Now, to create a virtual environment, type:

$ python3 -m venv my_env_project

The above command creates a directory named 'my_env_project' in the current directory, which contains pip, interpreter, scripts, and libraries.

oltjano@ubuntu:~$ ls my_env_project/
 bin  include  lib  lib64  pyvenv.cfg  share

You can now activate the virtual environment, type:

$ source my_env_project/bin/activate

Command prompt would change to your environment and will look as shown:

(my_env_project) oltjano@ubuntu:~$

Verify Virtual Environment

Run python command inside virtual environment to open the interpreter:

(my_env_project) oltjano@ubuntu:~$ python
Output
Python 3.8.5 (default, Jul 28 2020, 12:59:40)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.

To install a package inside the virtual environment, for example I am installing NumPy package:

(my_env_project) oltjano@ubuntu:~$ pip install numpy --user

If you are getting below error

"ERROR: Can not perform a '--user' install. User site-packages are not visible in this virtualenv."

Set include-system-site-packages to true in pyvenv.cfg file.

Every time you install a new package inside your virtual environment, you should be able to import it into your project.

(my_env_project) oltjano@ubuntu:~/my_env_project$ python
>>> import numpy

Lets test a math function, type:

>>> import math
>>> print(math.sqrt(16))

To exit from interpreter, type:

>>> quit()

We can create python script and run from inside python 3 virtual environment.

(my_env_project) oltjano@ubuntu:~$ sudo vi script.py
Output
import math
print(math.sqrt(16))

To execute the script, type:

(my_env_project) oltjano@ubuntu:~$ python script.py

The script is being executed inside the virtual environment called named my_env_project.

Delete Virtual environment

To exit from virtual environment use exit or Ctrl+d command. To delete a virtual environment run the following command:

(my_env_project) oltjano@ubuntu:~$ deactivate

The above command won't remove my_env_project directory, simply use rm command to delete it.

Final thoughts

Through this article, you learned how to properly create a virtual environment for your Python 3 projects on Ubuntu 20.04. We have also gone through a practical example on how to install a package and run a script inside the virtual environment.

1 thought on “How to Create Python 3 Virtual Environment on Ubuntu 20.04”... add one

Leave a Comment