How To Fix mkvirtualenv command not found on Ubuntu

Estimated reading: 2 minutes 260 views

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

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

Install virtualenv and virtualenvwrapper

You’ll need to install the virtualenvwrapper package into the same global site-packages area where virtualenv is installed. Administrative privileges are required for this installation.

Update the System package list.

apt update

Then install Python build tools.

With Python 3:

apt install python3-pip python3-dev build-essential

With Python 2:

apt install python-pip python-dev build-essential

Then install virtualenv and virtualenvwrapper packages.

With APT package manager:

apt install virtualenv virtualenvwrapper

With Pip:

With Python 3:

pip3 install virtualenv virtualenvwrapper

With Python 2:

pip install virtualenv virtualenvwrapper

Configure your Shell

First, confirm the location of the virtualenvwrapper script.

find / -name virtualenvwrapper.sh

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

nano ~/.bashrc

Add to end

VIRTUALENVWRAPPER_PYTHON=$(which python3)
source $(sudo find / -name virtualenvwrapper.sh)

Now source your bashrc file.

source ~/.bashrc

You can also set the WORKON_HOME variable to the 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 running the mkvirtualenv command:

mkvirtualenv -p /usr/bin/python3 test_green

To enable your project virtual environment any time run:

workon test_green

To exit run:

deactivate

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

Conclusion

Hopefully, this article will be helpful for you. Good luck!