How to create Python virtual environment in Ubuntu?


Gaming Laptop Series

Python virtual environment is a self-contained directory tree that helps to separately maintain the dependencies required by the different projects on a system. Each virtual environment has its own Python binary and can have its own independent set of installed Python packages in its project directories.

In this article, I will discuss how to create Python virtual environment in Ubuntu.

Install and configure Python

It requires Python to be installed on your system, Ubuntu comes preinstalled with Python you can check this by using the given command in your terminal –

python3 --version

python version

If Python3 is installed you will see the current version of it otherwise you can install it by executing the given command in your terminal –

sudo apt install python3 -y

Install Python3-venv package

The advantage of using virtual environments is that they provide isolation that means no environment interacts with another each of them works as an independent unit and its unusual changes do not damage other projects.

The recommended way to create a Python virtual environment is by using Python venv module. Run the given command to install the Python3-venv package to use venv module on your system.

sudo apt install python3-venv -y

Once the installation is complete you can start creating virtual environments.

Create a Python virtual environment

First, move to the directory where you want to store virtual environments –

mkdir env-dir
cd env-dir

Now use the following command to create a virtual environment –

python3 -m venv sample-project-env

Where sample-project-env is the name of the Python virtual environment, a directory with this name will be created inside your current working directory and this will contain a copy of the Python binary, the Pip package manager, and other supporting files.

To start using this virtual environment first you need to activate it, use the following command to activate the created virtual environment –

source sample-project-env/bin/activate

When activated you will see your terminal changed as given in the image below.

activate python virtual environment

Now the environment is activated you can start installing packages using the pip package manager. If pip is not installed on your system you can install it by using the given command –

sudo apt install -y python3-pip

Now let’s run a sample program inside the virtual environment, use the following command to create a file hello.py –

nano hello.py

Add the given line –

print('Hello, World!')

Save the file by pressing ctrl+s and then exit from the editor using ctrl+x.

Run this program using –

python3 hello.py

You will see the given output in your terminal –

hello.py

At any time you can deactivate and exit from the virtual environment by using the given command –

deactivate

Now your terminal will change back to as it was before activating the virtual environment.

deactivate

Conclusion

I hope now you understand how to create and use Python virtual environment in Ubuntu. Now if you have a query then write us in the comments below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.