December 22, 2024

How to Create Python Virtual Environments in Linux Ubuntu from Command Line

In this Linux and Python tutorial, we explain how to create Python virtual environments in Linux Ubuntu from the Linux command line. We also explain how to install Python packages in the virtual environment and how to run Python code from the virtual environment. The YouTube tutorial accompanying this webpage tutorial is given below.

Open a Linux terminal, and type

sudo apt-get update 
sudo apt-get upgrade

Then type

which python3

As an output you should get a path to the python executable file. Let us investigate the Python version

python3 --version

In my case, the Python version is

Python 3.10.12

Let us also install Gedit for editing the files:

sudo apt-get install gedit

Then, create a workspace folder. In my case, the folder will be located in my home folder

cd ~
mkdir test1
cd test1

Then, inside test1 folder we will create a Python virtual environment. First, we need to make sure that the virtual environment command is installed:

sudo apt install python3.10-venv

Here, make sure that the Python version is correct (see the previous command we typed python3 –version).

We create the Python virtual environment by typing in the command line:

python3 -m venv env1

The name of the environment is “env1”. The command “python -m venv” is used to create an environment. After that, type

ls -la

And you should see a new folder that contains the virtual environment:

total 12
drwxrwxr-x  3 aleksandar aleksandar 4096 Jul 11 23:23 .
drwxr-x--- 55 aleksandar aleksandar 4096 Jul 11 23:05 ..
drwxr-xr-x  5 root       root       4096 Jul 11 23:23 env1

The folder “env1” contains the new environment.

Next, we need to activate the environment. We do that by typing

source env1/bin/activate

This will execute the script file called “activate” and that will activate the environment. After executing this file, you will see

(env1) aleksandar@alex:~/test1$

The name in the parenthesis “(env1)” means that we have activated the environment and that we are currently in the environment. Now, if you type

which python3

You should see a path to the python executable file in the virtual environment:

/home/aleksandar/test1/env1/bin/python3

Next, let us create a python file and try to execute it in this environment. Type

gedit test1.py

This will open Gedit and create the file test1.py. Next, let us type the code given bellow

import numpy as np
print("My first virtual environment")
a=np.pi/4
b=np.pi/6
c=np.sin(a)+np.cos(b)
print(c)

Save this file and exit Gedit. Then go back to the terminal and type

python3 test1.py

You will get this error

Traceback (most recent call last):
  File "D:\test1\test1.py", line 1, in <module>
    import numpy as np
ModuleNotFoundError: No module named 'numpy'

This is because numpy package is not installed in our virtual environment. Note that this package might be installed in the global Python environment that is called from the command line (without activating the virtual environment). To install NumPy, type

pip install -U pip
pip install numpy

Next, try to run

python3 test1.py

The output is

My first virtual environment
1.5731321849709863

To see all the installed packages in the environment simply type

pip list

or

pip freeze

To deactivate the environment, type

deactivate

To erase the environment, type

rm -r env1