/ programming

How to install pip, virtualenv and virtualenvwrapper on Linux

I don't know how much up-to-date is pip in repositories from other distributions but version in Ubuntu 15.04 is from about year ago.

sudo apt-get install python-pip
[email protected]:~$ pip -V
pip 1.5.6 from /usr/lib/python2.7/dist-packages (python 2.7)

Because of this some commands don't work correctly.

[email protected]:~$ pip freeze
Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/pip/basecommand.py", line 122, in main
    status = self.run(options, args)
  File "/usr/lib/python2.7/dist-packages/pip/commands/freeze.py", line 74, in run
    req = pip.FrozenRequirement.from_dist(dist, dependency_links, find_tags=find_tags)
  File "/usr/lib/python2.7/dist-packages/pip/__init__.py", line 299, in from_dist
    assert len(specs) == 1 and specs[0][0] == '=='

Storing debug log for failure in /home/eshlox/.pip/pip.log

Let's remove pip.

sudo apt-get remove python-pip

I think that better option would be to install current version of pip from official site. When it is possible I always prefer to install software in my user home directory, that's why --user flag appears below.

Let's install pip.

wget https://bootstrap.pypa.io/get-pip.py
python get-pip.py --user
rm get-pip.py

Check version.

[email protected]:~$ .local/bin/pip -V
pip 7.1.0 from /home/eshlox/.local/lib/python2.7/site-packages (python 2.7)

Pip is now installed in user home directory, executable file exists in ~/.local/bin. We should add this to $PATH environment variable.

Edit ~/.bashrc file and add at the end:

export PATH=$PATH:$HOME/.local/bin

Reload configuration.

source .bashrc

Now we can just use pip in shell.

[email protected]:~$ pip -V
pip 7.1.0 from /home/eshlox/.local/lib/python2.7/site-packages (python 2.7)

We can simply upgrade pip when newer version will be available.

pip install pip --upgrade

In the same way we can install virtualenv and virtualenvwrapper.

pip install virtualenv virtualenvwrapper --user

Because we added ~/.local/bin to $PATH, virtualenv and virtualenwrapper are available from command line.

[email protected]:~$ mkvirtualenv --version

As described here, we should add shell startup file with our path to virtualenwrapper (.bashrc). It can look like this:

export WORKON_HOME=$HOME/projects
export PROJECT_HOME=$HOME/projects
source $HOME/.local/bin/virtualenvwrapper.sh

Done. Everything works as it should, and we are not dependent on versions from system repository.