How to install Python virtualenv or virtualenvwrapper on EO-Lab

Virtualenv is a tool used to create isolated Python environments. It is mainly used to mitigate problems with dependencies and versions. It also does not include setting up permissions. Virtualenvwrapper is similar to an extension for virtualenv, and contains wrappers which are used for creating and deleting environments.

For purposes of this guide we will use virtual machine vm01 with the operating system Ubuntu 20.04 LTS.

Connect to your VM, check the python version (it should be preinstalled). The next step is the installation of pip, the python package manager:

python3 --version Python 3.8.10
sudo apt install python3-pip

Confirm the pip3 installation by invoking this command:

pip3 -V pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.8)

Install virtualenvwrapper using pip:

pip3 install virtualenvwrapper

it is recommended to create a new directory to store your virtual environments, for example:

mkdir .virtualenvs

Now we are going to modify the .bashrc file by adding a row that will adjust every new virtual environment to use Python 3. We will point virtual environments to the directory we created above (.virtualenvs) and we will also point to the locations of the virtualenv and virtualenvwrapper. Open the .bashrc file using an editor, VIM for example:

vim ~/.bashrc

Navigate to the bottom of the .bashrc file and add the following rows:

#virtualenvwrapper settings:
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
export VIRTUALENVWRAPPER_VIRTUALENV=/home/eouser/.local/bin/virtualenv
source ./.local/bin/virtualenvwrapper.sh

After that save the .bashrc file.

Now we have to reload the bashrc script, to do it execute the command:

source ~/.bashrc

If everything is set up properly, you should see the following lines:

virtualenvwrapper.user_scripts creating /home/eouser/.virtualenvs/premkproject
virtualenvwrapper.user_scripts creating /home/eouser/.virtualenvs/postmkproject
virtualenvwrapper.user_scripts creating /home/eouser/.virtualenvs/initialize
virtualenvwrapper.user_scripts creating /home/eouser/.virtualenvs/premkvirtualenv
virtualenvwrapper.user_scripts creating /home/eouser/.virtualenvs/postmkvirtualenv
virtualenvwrapper.user_scripts creating /home/eouser/.virtualenvs/prermvirtualenv
virtualenvwrapper.user_scripts creating /home/eouser/.virtualenvs/postrmvirtualenv
virtualenvwrapper.user_scripts creating /home/eouser/.virtualenvs/predeactivate
virtualenvwrapper.user_scripts creating /home/eouser/.virtualenvs/postdeactivate
virtualenvwrapper.user_scripts creating /home/eouser/.virtualenvs/preactivate
virtualenvwrapper.user_scripts creating /home/eouser/.virtualenvs/postactivate
virtualenvwrapper.user_scripts creating /home/eouser/.virtualenvs/get_env_details

Now, let’s create our first virtual environment ‘test’ using the ‘mkvirtualenv’ command.

mkvirtualenv test
created virtual environment CPython3.8.10.final.0-64 in 581ms
creator CPython3Posix(dest=/home/eouser/.virtualenvs/test, clear=False, no_vcs_ignore=False, global=False)
seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/home/eouser/.local/share/virtualenv)
added seed packages: pip==21.3.1, setuptools==60.2.0, wheel==0.37.1 activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator
virtualenvwrapper.user_scripts creating /home/eouser/.virtualenvs/test/bin/predeactivate
virtualenvwrapper.user_scripts creating /home/eouser/.virtualenvs/test/bin/postdeactivate
virtualenvwrapper.user_scripts creating /home/eouser/.virtualenvs/test/bin/preactivate
virtualenvwrapper.user_scripts creating /home/eouser/.virtualenvs/test/bin/postactivate
virtualenvwrapper.user_scripts creating /home/eouser/.virtualenvs/test/bin/get_env_details

Now you should see the name of your environment in the bracket before your username, which means that you’re working in your virtual environment.

If you would like to exit the current environment, use the deactivate command:

(test) [email protected]:~$ deactivate
[email protected]:~$

To start working on a virtual environment, use the workon command:

[email protected]:~$ workon test
(test) [email protected]:~$

To remove a virtual environment, use rmvirtualenv:

[email protected]01:~$ rmvirtualenv test
Removing test...

To list all virtual environments use workon or lsvirtualenv:

[email protected]:~$ workon
test-1
test-2
test-3
[email protected]:~$ lsvirtualenv
test-1
======
test-2
======
test-3
======