87

Is it this, which people seem to recommend most often:

$ sudo apt-get install python-setuptools
$ sudo easy_install pip
$ sudo pip install virtualenv

Or this, which I got from http://www.pip-installer.org/en/latest/installing.html:

$ curl -O https://github.com/pypa/virtualenv/raw/master/virtualenv.py
$ python virtualenv.py my_new_env
$ . my_new_env/bin/activate
(my_new_env)$ pip install ...

Or something entirely different?

Hugo Tavares
  • 23,060
  • 5
  • 45
  • 43
coffee-grinder
  • 25,040
  • 19
  • 53
  • 81

16 Answers16

93

If you can install the latest Python (2.7.9 and up) Pip is now bundled with it. See: https://docs.python.org/2.7//installing/index.html
If not :
Update (from the release notes):

Beginning with v1.5.1, pip does not require setuptools prior to running get-pip.py. Additionally, if setuptools (or distribute) is not already installed, get-pip.py will install setuptools for you.

I now run the regular:

curl --silent --show-error --retry 5 https://bootstrap.pypa.io/get-pip.py | sudo python

Here are the official installation instructions: http://pip.readthedocs.org/en/latest/installing.html#install-pip

EDIT 25-Jul-2013:
Changed URL for setuptools install.

EDIT 10-Feb-2014:
Removed setuptools install (thanks @Ciantic)

EDIT 26-Jun-2014:
Updated URL again (thanks @LarsH)

EDIT 1-Mar-2015:
Pip is now bundled with Python

JAR.JAR.beans
  • 8,075
  • 3
  • 39
  • 52
Tal Weiss
  • 8,438
  • 6
  • 51
  • 58
  • 1
    Where/how do you install setuptools-0.8? Looks like you are just grabbing `ez_setup.py` for a "lightweight version" of getting pip. I'd do the same but fear that it will break in the near future and I'll get stuck here again (like I always do) – Steven Lu Jul 13 '13 at 04:48
  • 3
    Ah, looks like the instructions say to just do as you say here, it is sanctioned. Nevermind then. – Steven Lu Jul 13 '13 at 04:56
  • 4
    There is new (?) way to do this, this installs pip and setuptools automatically: `wget --no-check-certificate https://raw.github.com/pypa/pip/master/contrib/get-pip.py` and then `python get-pip.py` (may require sudo) – Ciantic Feb 09 '14 at 22:26
  • 3
    Now the URL for get-pip.py seems to have changed to `https://bootstrap.pypa.io/get-pip.py`. Probably better just to link to the official instructions. – LarsH Jun 25 '14 at 18:09
  • Thanks @LarsH - edited my answer to reflect the change. Again. – Tal Weiss Jun 26 '14 at 15:14
  • 2
    P.S. Thanks for this answer - it helped me get pip installed. – LarsH Jun 26 '14 at 15:59
  • 1
    Any idea if it's possible to specify a specific version of pip by passing an argument to get-pip.py? Otherwise I'd just use the installed pip to install a specified version of pip, e.g. `pip install -U pip==1.5.6` – orluke Dec 15 '14 at 22:09
  • 1
    @orluke Not sure why you would want to do this, but you can download pip from Github - here are the releases: https://github.com/pypa/pip/releases – Tal Weiss Dec 16 '14 at 07:31
21

http://www.pip-installer.org/en/latest/installing.html is really the canonical answer to this question.

Specifically, the systemwide instructions are:

$ curl -O http://python-distribute.org/distribute_setup.py
$ python distribute_setup.py
$ curl -O https://raw.github.com/pypa/pip/master/contrib/get-pip.py
$ python get-pip.py

The section quoted in the question is the virtualenv instructions rather than the systemwide ones. The easy_install instructions have been around for longer, but it isn't necessary to do it that way any more.

wh1tney
  • 1,007
  • 1
  • 9
  • 16
Richard Jones
  • 380
  • 1
  • 5
  • Edited to clarify the difference between the systemwide instructions on that page and the `virtualenv` instructions that were quoted in the question. – ncoghlan Apr 08 '11 at 06:11
  • 1
    Update: distribute does not work anymore for pip! See http://stackoverflow.com/a/17601159/78234 – Tal Weiss Jul 11 '13 at 18:54
  • 1
    Please note that the instructions on the linked page no longer match the instructions here. It is best to just visit the official page and do as they say (and why did anyone doubt them in the first place?!) – Neil Traft May 07 '14 at 17:10
  • The approach described in this answer is outdated and does not seem to work anymore. – cel Jan 02 '15 at 05:04
16

This answer comes from @webology on Twitter:

$ sudo apt-get install python-setuptools
$ sudo easy_install pip
$ sudo pip install --upgrade pip virtualenv virtualenvwrapper

My added notes:

  • On Mac/Windows (and Linux if the apt repo is outdated) you'd replace the first step with downloading setuptools from http://pypi.python.org/pypi/setuptools
  • On Windows you'd have to omit virtualenvwrapper from the last step and install it manually somehow. I don't know if there's a way to do this without Cygwin, but I hope so.
coffee-grinder
  • 25,040
  • 19
  • 53
  • 81
  • 1
    If the condition is that you want system-wide installations of pip and virtualenv, this is what I generally do. The instructions from the pip docs that are given as the second alternative in the question are based on the premise that we're avoiding installing anything system-wide. – Carl Meyer Apr 08 '11 at 04:59
  • 3
    You can replace the first two commands with `$ sudo apt-get install python-pip`. Installing python-pip will also install python-setuptools. – Dennis Sep 18 '13 at 06:39
4

On Ubuntu 12.04 I've had good luck just using the package manager:

sudo apt-get install python-pip virtualenvwrapper
Jason McVetta
  • 1,357
  • 11
  • 9
  • 1
    The packaged versions are far out of date. 12.04 provides `python-pip 1.0-1build1` which was released in early 2011 and `virtualenvwrapper 2.11.1-21` released January 2012. @Richard Jones' answer will get you the latest. – JCotton Mar 12 '13 at 01:49
3

Do this:

curl "https://bootstrap.pypa.io/get-pip.py" -o "get-pip.py"
python get-pip.py
pip install virtualenv

See

asmaier
  • 9,605
  • 10
  • 68
  • 93
3

There is no preferred method - everything depends on your needs. Often you need to have different Python interpreters on the system for whatever reason. In this case you need to install the stuff individually for each interpreter. Apart from that: I prefer installing stuff myself instead of depending of pre-packaged stuff sometimes causing issues - but that's only one possible opionion.

Andreas Jung
  • 1
  • 18
  • 70
  • 118
  • I'm asking about the preferred method specifically because I'm going to give these instructions to a class of 20 people and don't want to mislead them. – coffee-grinder Apr 07 '11 at 18:42
3

There really isn't a single "answer" to this question, but there are definitely some helpful concepts that can help you to come to a decision.

The first question that needs to be answered in your use case is "Do I want to use the system Python?" If you want to use the Python distributed with your operating system, then using the apt-get install method may be just fine. Depending on the operating system distribution method though, you still have to ask some more questions, such as "Do I want to install multiple versions of this package?" If the answer is yes, then it is probably not a good idea to use something like apt. Dpkg pretty much will just untar an archive at the root of the filesystem, so it is up to the package maintainer to make sure the package installs safely under very little assumptions. In the case of most debian packages, I would assume (someone can feel free to correct me here) that they simply untar and provide a top level package.

For example, say the package is "virtualenv", you'd end up with /usr/lib/python2.x/site-packages/virtualenv. If you install it with easy_install you'd get something like /usr/lib/python2.x/site-packages/virtualenv.egg-link that might point to /usr/lib/python2.x/site-packages/virtualenv-1.2-2.x.egg which may be a directory or zipped egg. Pip does something similar although it doesn't use eggs and instead will place the top level package directly in the lib directory.

I might be off on the paths, but the point is that each method takes into account different needs. This is why tools like virtualenv are helpful as they allow you to sandbox your Python libraries such that you can have any combination you need of libraries and versions.

Setuptools also allows installing packages as multiversion which means there is not a singular module_name.egg-link created. To import those packages you need to use pkg_resources and the __import__ function.

Going back to your original question, if you are happy with the system python and plan on using virtualenv and pip to build environments for different applications, then installing virtualenv and / or pip at the system level using apt-get seems totally appropriate. One word of caution though is that if you plan on upgrading your distributions Python, that may have a ripple effect through your virtualenvs if you linked back to your system site packages.

I should also mention that none of these options is inherently better than the others. They simply take different approaches. Using the system version is an excellent way to install Python applications, yet it can be a very difficult way to develop with Python. Easy install and setuptools is very convenient in a world without virtualenv, but if you need to use different versions of the same library, then it also become rather unwieldy. Pip and virtualenv really act more like a virtual machine. Instead of taking care to install things side by side, you just create an whole new environment. The downside here is that 30+ virtualenvs later you might have used up quite bit of diskspace and cluttered up your filesystem.

As you can see, with the many options it is difficult to say which method to use, but with a little investigation into your use cases, you should be able to find a method that works.

elarson
  • 807
  • 8
  • 10
2

Starting from distro packages, you can either use:

sudo apt-get install python-virtualenv

which lets you create virtualenvs, or

sudo apt-get install python{,3}-pip

which lets you install arbitrary packages to your home directory.

If you're used to virtualenv, the first command gives you everything you need (remember, pip is bundled and will be installed in any virtualenv you create).

If you just want to install packages, the second command gives you what you need. Use pip like this:

pip install --user something

and put something like

PATH=~/.local/bin:$PATH

in your ~/.bashrc.


If your distro is ancient and you don't want to use its packages at all (except for Python itself, probably), you can download virtualenv, either as a tarball or as a standalone script:

wget -O ~/bin/virtualenv https://raw.github.com/pypa/virtualenv/master/virtualenv.py
chmod +x ~/bin/virtualenv

If your distro is more of the bleeding edge kind, Python3.3 has built-in virtualenv-like abilities:

python3 -m venv ./venv

This runs way faster, but setuptools and pip aren't included.

Tobu
  • 23,001
  • 3
  • 85
  • 97
2

To install pip on a mac (osx), the following one liner worked great for me:

sudo easy_install pip
Brad Parks
  • 54,283
  • 54
  • 221
  • 287
2

In Raspbian, there is even no need to mention python2.7. Indeed this is best way to install pip if python version in less then 2.7.9.

curl --silent --show-error --retry 5 https://bootstrap.pypa.io/get-pip.py | sudo python

Thanks to @tal-weiss

2

Since virtualenvs contain pip by default, I almost never install pip globally. What I do ends up looking more like:

$ sudo apt-get install python-setuptools
$ curl -O http://python-distribute.org/distribute_setup.py
$ sudo python distribute_setup.py
$ sudo easy_install virtualenv

I then proceed to install and set up virtualenvwrapper to my liking and off I go. it might also be worthwhile to take a look at Jeremy Avnet's virtualenv-burrito:

https://github.com/brainsik/virtualenv-burrito

David Gouldin
  • 565
  • 1
  • 4
  • 11
  • 3
    Why are you bothering with python-setuptools and then distribute when you can go directly to distribute? – lambacck Apr 07 '11 at 19:05
  • @lambacck Could you please post what you are describing in a separate answer (even if it seems repetitive)? – coffee-grinder Apr 08 '11 at 01:41
  • 2
    Not to mention that on any recent Ubuntu (and Debian too? Not sure) the "python-setuptools" package actually IS distribute, so you may as well just skip the specific distribute step. – Carl Meyer Apr 08 '11 at 05:01
  • @coffee-grinder distribute is a fork of setuptools and therefor provides the same functionality (and more). Don't bother doing "apt-get install python-setuptools" unless python-setuptools IS distribute (as Carl indicates it might be) in which case skip steps 2 & 3. – lambacck Apr 08 '11 at 13:54
2

@ericholscher says on Twitter, "The one in the official docs.."

It's a great point, you should do what the docs say.

Quoted from the official pip installation instructions at http://www.pip-installer.org/en/latest/installing.html:

$ curl -O https://github.com/pypa/virtualenv/raw/master/virtualenv.py
$ python virtualenv.py my_new_env
$ . my_new_env/bin/activate
(my_new_env)$ pip install ...
coffee-grinder
  • 25,040
  • 19
  • 53
  • 81
1

On Debian the best way to do it would be

sudo apt-get install python-pip

Yehonatan
  • 2,820
  • 6
  • 25
  • 37
1

I use get-pip and virtualenv-burrito to install all this. Not sure if python-setuptools is required.

# might be optional. I install as part of my standard ubuntu setup script
sudo apt-get -y install python-setuptools

# install pip (using get-pip.py from pip contrib)
curl -O https://raw.github.com/pypa/pip/develop/contrib/get-pip.py && sudo python get-pip.py

# one-line virtualenv and virtualenvwrapper using virtualenv-burrito
curl -s https://raw.github.com/brainsik/virtualenv-burrito/master/virtualenv-burrito.sh | bash
Rudiger Wolf
  • 1,740
  • 11
  • 14
1

https://github.com/pypa/pip/raw/master/contrib/get-pip.py is probably the right way now.

coderanger
  • 44,716
  • 4
  • 39
  • 59
0

The former method is fine. The only problem I can see is that you might end up with an old version of setuptools (if the apt repository hasn't been kept up-to-date..

bradley.ayers
  • 33,409
  • 13
  • 84
  • 94