virtualenvwrapper is an excellent tool to manage python virtual environments. It lets you collect all virtual environments into a single directory. It can also activate the virtual environments from any directory with a single command. virtualenvwrapper makes all the hassles of finding the activate file and sourcing it go away. This article explains the installation and configuration of virtualenvwrapper in Linux systems.
This article assumes you are using python 3. If you want to configure virtualenvwrapper for
python 2.7, substitute
python2 in relevant places.
pip3 if not already installed. For Debian based systems such as Debian, Ubuntu Or Linux
mint, run the following command from a terminal.
sudo apt install python3-pip
For Fedora based systems such as Fedora, Cent OS or RHEL, use
sudo dnf install python3-pip
Once you have
pip3 installed, use it to install virtualenvwrapper. Run the following command from
sudo pip3 install virtualenvwrapper
After installation, you need to do some configurations to make virtualenvwrapper work. First, you
will have to create a directory to save all virtual environments. Usually, it is the
directory in your home directory.
Now you have to set this as the home directory for your
workon command (You will learn more about
workon command in next section). The following command will do that for you.
Next, you have to add some configurations to your
.bashrc file. Open your
.bashrc file using
and add the following line at the end of it.
VIRTUALENVWRAPPER_PYTHON='/usr/bin/python3' source /usr/local/bin/virtualenvwrapper.sh
The above configuration is for Debian based systems. For Fedora based systems, you have to add
VIRTUALENVWRAPPER_PYTHON='/usr/local/bin/python3' source /usr/local/bin/virtualenvwrapper.sh
Now, source the updated
Creating and Using Virtual Environments
To create a new virtual environment, run
This will create a new virtual environment and activate it for you. To deactivate the virtual environment, run
To activate your virtual environment again, simply run
This will work from any directory. Also, you can have any number of virtual environments and switch
between them using
If you want to create a virtual environment that uses a different python interpreter instead of the
one you configured in your
.bashrc file, you can pass it as an argument to the
command. For example, the following command will create a virtual environment that uses
mkvirutalenv my-very-old-env --python=/usr/bin/python2.6
Of course, for this to work, you should have