The Virtualenv is used to create an isolated environment for Python application. It provides separate environment to application that system environment. This tutorial will help you to create a Python virtual environment on Ubuntu 20.04 LTS, 19.10, 18.04 LTS and 16.04 LTS systems.
Before We Start
Step 1 – Install Virtualenv Module
First of all, you need to install Virtualenv Python module on your system. Use pip3 to install virtualenv Python module. This will also install other required modules on your system.
pip3 install virtualenv Collecting virtualenv Downloading https://files.pythonhosted.org/packages/f7/69/9a07/virtualenv-16.7.4-py2.py3-none-any.whl (3.3MB) 100% |████████████████████████████████| 3.3MB 448kB/s Installing collected packages: virtualenv Successfully installed virtualenv-20.0.14
Step 2 – Create Virtual Environment
Once the installation is finished. Let’s create an isolated Python environment for your application.
Find the python binary location required to run for your application. For this tutorial we are using recently installed Python 3.8. Check the Python 3 binary file location using which command.
which python3.8 /usr/bin/python3.8
You can choose any other Python version as per your application requirements.
Now, Create a separate environment for your Application. You can change the environment directory name (isoEnv used in below example) as per your choice. You can also define the environment directory on a different location.
Then create Python isolated environment with the below command:
virtualenv -p /usr/bin/python3.8 isoEnv
created virtual environment CPython3.8.2.final.0-64 in 479ms creator CPython3Posix(dest=/home/rahul/myPythonApp/isoEnv, clear=False, global=False) seeder FromAppData(download=False, pip=latest, setuptools=latest, wheel=latest, via=copy, app_data_dir=/home/rahul/.local/share/virtualenv/seed-app-data/v1.0.1) activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator
This command creates a local copy of your environment specific to this website. While working on this website, you should activate the local environment in order to make sure you’re working with the right versions of your tools and packages.
Step 3 – Activate Virtual Environment
Once you create the Python virtual environment on you Ubuntu system. You need to activate it by running the following command.
Now, you will see the Python isolated environment prompt. Check the python binary location with below command:
(isoEnv) [email protected]:~/myPythonApp$which python /home/rahul/myPythonApp/isoEnv/bin/python
You will see that binary location is inside isolated Python environment. Also the binary name is just “python” instead of python3.8.
Let’s check the Python version inside isolated environment.
(isoEnv) [email protected]:~/myPythonApp$python -V Python 3.8.2
Any package that you install using pip is now placed in the virtual environments project folder, isolated from the global Python installation.
Use pip3 to install a module. To install most commonly used ‘requests’ module execute below command.
(isoEnv) [email protected]:~/myPythonApp$pip3 install requests Collecting requests Downloading requests-2.23.0-py2.py3-none-any.whl (58 kB) |████████████████████████████████| 58 kB 4.8 MB/s Collecting urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 Downloading urllib3-1.25.9-py2.py3-none-any.whl (126 kB) |████████████████████████████████| 126 kB 24.1 MB/s Installing collected packages: urllib3, requests Successfully installed requests-2.23.0 urllib3-1.25.9
All the installed modules files are placed at isoEnv/lib/python3.8/site-packages directory.
Step 4 – Exit from Virtual Environment
After finishing your work inside the virtual environment, just run “deactivate” command to exit from isolated environment. Then, You will get the users to default shell.
(isoEnv) [email protected]:~/myPythonApp$deactivate
Step 5 – Delete Virtual Environment
To Delete the Python virtual environment from your application. Simply delete the isoEnv directory from your application folder.
rm -rf isoEnv
In this tutorial, you have learned to create Python virtual environment on Ubuntu Linux system,.