Blog Archive

Friday, February 7, 2020

Virtualenv with Virtualenvwrapper on Ubuntu 18.04 [updated: 2/7/2020]

Before we start with the tutorial I would like to tell you the story how I realized that I need a reminder on how to set up virtualenvwrapper on top of virtualenv in Ubuntu 18.04. I have done this process several of times on different computers and every time it seemed to be just a little bit different than before.
Recently I got myself a new laptop and I on my way home I have read several tutorials on “How to set up virtualenvwrapper on Ubuntu 18.04”. And let me tell you that it seems really easy because all of those tutorials are pretty straight forward and basically explain how to do the next three 3 things:
  • Install virtualenv
  • Install virtualenvwrapper
  • Edit .bashrc/.bash_profile or both
But even though all these tutorials are meant to help us set up virtualenvwrapper on a modern Ubuntu 18.04 installation non of them worked for me.
I had several errors while trying to figure out what went wrong while following these tutorials. First I’ve had some of “mkvirtualenv: command not found”, then a little of “-bash: /usr/bin/virtualenvwrapper.sh: No such file or directory”, and then a touch of “ERROR: virtualenvwrapper could not find virtualenv in your path”.
After some research I realized that all virtualenvwrapper Ubuntu 18.04 tutorials are copies an old text written before April 2016 (release date of Ubuntu 16.04).
And I know it to be true because from Ubuntu 16.04 and onward the location for the vritualenvwrapper’s pip installation has changed from /usr/local/bin/virtualenvwrapper.sh to ~/.local/bin/virtualenvwrapper.sh.Note that the local directory is hidden.
So let’s start by writing a tutorial that will show you how to avoid all those issues mentioned above.

Prerequisites

In this text I will show you how to set up virtualenvwrapper with pip3 (pip for Python 3). I chosen this version of pip instead of a Python 2 version because Python’s 2 end of life is January 1. 2020.
Python 2 will retire in… https://pythonclock.org/
To complete this tutorial you will need a computer with Ubuntu 18.04 installed and a internet connection :) Also some knowledge in terminals and Vim editor would be useful. We will assume you already updated and upgraded the system.

Setting up a Virtual Environment

Now open your terminal in the home directory by right clicking and choosing the option “Open in Terminal” or you can press the CTRLALT, and T keys on your keyboard at the same time to open the Terminal application automatically.
You first need to create a special directory that will hold all of your virtual environments so proceed with creating a new hidden directory called virtualenv.
Now you should install pip for Python3.
Confirm the pip3 installation.
pip3 --version
Now install virtualenv via pip3.
To find out the location where your virtualenv was installed type:
Install virtualenvwrapper via pip3:
We are going to modify your .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 (.virtualenv) and we will also point to the locations of the virtualenv and virtualenvwrapper.
Now open the .bashrc file using Vim editor.
If you still haven’t used Vim editor or you don’t have it installed on your computer you should install it now. It is one of widely used Linux editors and for a good reason.
After you installed Vim open the file .bashrc file by typing in the vim .bashrc command in your terminal. Navigate to the bottom of the .bashrc file, press the letter i to enter the insert mode of Vim and add these rows:


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

After you are done, press the esc key then type:wq and press enter, this command will save and exit the Vim editor. Close and reopen your terminal when you’re done.
To create a virtual environment in Python3 and activate it immediately use this command in your terminal:
You should confirm that this environment is set up for Python3:
To deactivate the environment use the deactivate command.
To list all available virtual environments use the command workon or lsvirtualenv (Same result as workon but shown in a fancy way) in your terminal:
To activate one specific environment use workon + name of your environment:
There are several useful command you might need to use someday:
Rmvirtualenv will remove a specific virtual environment located in your .virtualenv directory.
Cpvirtualenv will copy the existing virtual environment to a new virtual environment and activate it.
Well done!
You have now created your first isolated Python 3 environment!
Thank you for reading! Check out more articles like this on my Medium profile: https://medium.com/@goranaviani and other fun stuff I build on my GitHub page: https://github.com/GoranAviani

Source:

No comments:

Post a Comment