You can support us by downloading this article as PDF from the Link below. Download the guide as PDF

In this short tutorial we’ll show you how to install mkvirtualenv (virtualenvwrapper & virtualenv) on Ubuntu Linux machine. virtualenvwrapper is a set of shell functions defined in Bourne shell compatible syntax. virtualenv is what enables you to create many different Python environments in your Linux / macOS system. With virtualenvwrapper you can automate tests run under the following shells on a Linux or OS X machine.

  • bash
  • ksh
  • zsh

This is the error I’m getting if I try type mkvirtualenv in the terminal.

$ mkvirtualenv
mkvirtualenv: command not found

You’ll need to install virtualenvwrapper package into the same global site-packages area where virtualenv is installed. Administrative privileges are required for this installation. Ensure you’re logged in as root user or standard user with sudo privileges.

Update the System package list.

sudo apt update

Then install Python build tools.

# Python 2
sudo apt-get install python-pip python-dev build-essential

# Python 3
sudo apt-get install python3-pip python3-dev build-essential

Then install virtualenv and virtualenvwrapper packages.

With APT package manager:

sudo apt install virtualenv virtualenvwrapper

With Pip:

# Python 2
$ sudo pip install virtualenv virtualenvwrapper

# Python 3
$ sudo pip3 install virtualenv virtualenvwrapper

An alternative to installing it into the global site-packages is to add it to your user local directory (usually ~/.local). See below example:

$ pip install --user virtualenv virtualenvwrapper

Configure your Shell

Depending on your installation method you may need to add virtualenvwrapper script path to your startup file (.bashrc, .profile, etc.).

But first confirm the location of virtualenvwrapper script.

$ sudo find / -name virtualenvwrapper.sh
/usr/share/virtualenvwrapper/virtualenvwrapper.sh

Then edit ~/.bashrc and add a line to source /usr/share/virtualenvwrapper/virtualenvwrapper.sh

$ vim ~/.bashrc
source '/usr/share/virtualenvwrapper/virtualenvwrapper.sh'

Now source your bashrc file.

$ source ~/.bashrc
virtualenvwrapper.user_scripts creating /home/computingforgeeks/.virtualenvs/premkproject
virtualenvwrapper.user_scripts creating /home/computingforgeeks/.virtualenvs/postmkproject
virtualenvwrapper.user_scripts creating /home/computingforgeeks/.virtualenvs/initialize
virtualenvwrapper.user_scripts creating /home/computingforgeeks/.virtualenvs/premkvirtualenv
virtualenvwrapper.user_scripts creating /home/computingforgeeks/.virtualenvs/postmkvirtualenv
virtualenvwrapper.user_scripts creating /home/computingforgeeks/.virtualenvs/prermvirtualenv
virtualenvwrapper.user_scripts creating /home/computingforgeeks/.virtualenvs/postrmvirtualenv
virtualenvwrapper.user_scripts creating /home/computingforgeeks/.virtualenvs/predeactivate
virtualenvwrapper.user_scripts creating /home/computingforgeeks/.virtualenvs/postdeactivate
virtualenvwrapper.user_scripts creating /home/computingforgeeks/.virtualenvs/preactivate
virtualenvwrapper.user_scripts creating /home/computingforgeeks/.virtualenvs/postactivate
virtualenvwrapper.user_scripts creating /home/computingforgeeks/.virtualenvs/get_env_details

You can as well set WORKON_HOME variable to custom path which by default is $HOME/.virtualenvs

export WORKON_HOME=/my/other/path

If the directory does not exist when virtualenvwrapper is loaded, it will be created automatically.

Try run mkvirtualenv command:

$ mkvirtualenv -p /usr/bin/python3 test_venv
created virtual environment CPython3.8.2.final.0-64 in 448ms
  creator CPython3Posix(dest=/home/computingforgeeks/.virtualenvs/test_venv, clear=False, global=False)
  seeder FromAppData(download=False, pep517=latest, requests=latest, appdirs=latest, progress=latest, lockfile=latest, certifi=latest, urllib3=latest, packaging=latest, pyparsing=latest, chardet=latest, six=latest, pip=latest, colorama=latest, CacheControl=latest, pkg_resources=latest, webencodings=latest, ipaddr=latest, distlib=latest, contextlib2=latest, html5lib=latest, pytoml=latest, setuptools=latest, msgpack=latest, wheel=latest, idna=latest, distro=latest, retrying=latest, via=copy, app_data_dir=/home/computingforgeeks/.local/share/virtualenv/seed-app-data/v1.0.1.debian)
  activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator
virtualenvwrapper.user_scripts creating /home/computingforgeeks/.virtualenvs/test_venv/bin/predeactivate
virtualenvwrapper.user_scripts creating /home/computingforgeeks/.virtualenvs/test_venv/bin/postdeactivate
virtualenvwrapper.user_scripts creating /home/computingforgeeks/.virtualenvs/test_venv/bin/preactivate
virtualenvwrapper.user_scripts creating /home/computingforgeeks/.virtualenvs/test_venv/bin/postactivate
virtualenvwrapper.user_scripts creating /home/computingforgeeks/.virtualenvs/test_venv/bin/get_env_details

To enable your project virtual environment any time run:

$ workon test_venv

To exit run:

$ deactivate

You should now be able to work on your Python Project with virtualenv and virtualenvwrapper script.

Other Python related articles:

How To Install Python 2 with Virtualenv  on Ubuntu

Deploy Python 3 Django Application on CentOS 7 with Apache and mod_wsgi

Best Books for Learning Python Programming

Python Learning videos:

$15.22
$152.14
in stock
Udemy.com
$19.90
$58.51
in stock
Udemy.com
$15.22
$152.14
in stock
Udemy.com
You can support us by downloading this article as PDF from the Link below. Download the guide as PDF