To create a Python virtual environment on Ubuntu you can use venv
the module . Here are the steps:
-
First, make sure you have Python installed. You can check if Python is installed
python3 --version
by If you see a version number, Python is already installed. If not, you need to install Python first. -
Install
venv
the module . On Ubuntu, you can use the following command to install:
sudo apt-get install python3-venv
- Create a virtual environment. First, navigate to the directory where you wish to create your virtual environment, then run the following command (where
env
is the name of your virtual environment):
python3 -m venv env
- Activate the virtual environment. Run the following command:
source env/bin/activate
You should now be in a new Python virtual environment. You'll notice that your command prompt has changed and should now contain the name of the virtual environment (in this case it is env
). You can now start installing any required Python packages.
- If you want to exit the virtual environment, just type:
deactivate
NOTE: When you use pip
packages , they are only available in that environment. This is a great way to avoid package version conflicts and keep your global environment clean.