How to install python3 version of package via pip on Ubuntu?
Asked Answered
M

19

441

I have both python2.7 and python3.2 installed in Ubuntu 12.04.
The symbolic link python links to python2.7.

When I type:

sudo pip install package-name

It will default install python2 version of package-name.

Some package supports both python2 and python3.
How to install python3 version of package-name via pip?

Munguia answered 26/5, 2012 at 3:50 Comment(6)
Are there separated pip-2.7 and pip-3.2 commands in your system?Lump
Can I change the first line of /usr/bin/pip from #!/usr/bin/python to #!/usr/bin/python3 ?Munguia
Yes, but I would recommend you to cp the pip to pip-3.2 then change it, so you would get a better choice next time :)Lump
possible duplicate of pip: dealing with multiple Python versions?Ingham
If you are having trouble with pip-2.7, etc. Try running whereis pip from your command line. It seems they have changed the names to pip2.7 on Ubuntu 14.04.Ananna
Run pip3 install package-name instead. It should be available by default nowadays. Check akaIDIOT's answer below for more details.Reitz
L
267

You may want to build a virtualenv of python3, then install packages of python3 after activating the virtualenv. So your system won't be messed up :)

This could be something like:

virtualenv -p /usr/bin/python3 py3env
source py3env/bin/activate
pip install package-name
Lump answered 26/5, 2012 at 3:57 Comment(10)
Does virtualenv install pip even if you don't have it installed in the main Python 3? If so, then this does solve his problem. Otherwise not.Squatness
Yes, virtualenv installs pip.Lump
OK. I still think the answer doesn't really answer his question in a generic way, though.Squatness
virtualenv itself can be installed via pip to get the latest in older versions of Ubuntu: sudo pip install virtualenvWhalebone
This has the advantage of providing the latest version of both virtualenv and the pip that it provides to virtual environments.Whalebone
This works for me. Two additions: You can leave the virtualenv with deactivate and python3 might be installed at a different location. Mine is at /usr/local/bin/python3, which you can find out with which python3Mourner
Following @LennartRegebro and @user2503795, I can confirm that this is a bit more robust: virtualenv -p `which python3` py3envWillamina
Getting all kinds of access denied errors on Ubuntu. -- Tried sudo, still doesn't work.Eudy
This answer is outdated.Balata
Is it equivalent to python3 -m pip install <package_name> without virtual environment?Atrip
A
477

Ubuntu 12.10+ and Fedora 13+ have a package called python3-pip which will install pip-3.2 (or pip-3.3, pip-3.4 or pip3 for newer versions) without needing this jumping through hoops.


I came across this and fixed this without needing the likes of wget or virtualenvs (assuming Ubuntu 12.04):

  1. Install package python3-setuptools: run sudo aptitude install python3-setuptools, this will give you the command easy_install3.
  2. Install pip using Python 3's setuptools: run sudo easy_install3 pip, this will give you the command pip-3.2 like kev's solution.
  3. Install your PyPI packages: run sudo pip-3.2 install <package> (installing python packages into your base system requires root, of course).
  4. Profit!
Antiknock answered 4/9, 2012 at 11:1 Comment(15)
Perfect and proper, this should be the accepted answer. However you should specify sudo pip-3.2 install <package>, superuser permissions are required.Vellicate
I find it better to use --user to install in ~.local/, than to use sudo. see pep 370Kakaaba
Doing this in Ubuntu 12.04 leaves me with pip-3.2 and pip both installing to the Python 3 directory, with no pip-2.7 available. I had to update pip for Python 2 to resolve both issues.Buzzard
You can combine steps 1 and 2 and just do: sudo apt-get install python3-pipCaught
packages.ubuntu.com shows this super handy package for Ubuntu 12.10, but not for 12.04: packages.ubuntu.com/… (great find though ;))Antiknock
In Ubuntu 13.04 and others, pip-3.3 is the current command. Maybe the answer should be updated?Demobilize
@Demobilize for recent Ubuntus that's true indeed, though the package python3-pip is a lot easier than going through easy_install3. I'll add a disclaimer to the answer.Antiknock
Also available in Debian 7.7 (wheezy).Protolithic
pip-3.3, pip-3.4 etc no longer work. It is now just: pip, pip2, pip3. (At least on Ubuntu 14.04)Carmeliacarmelina
this solution also works on debian wheezy and kali linuxOvular
As of today, I believe apt-get gives you the outdated 1.5.6 version; if you don't want an AssertionErrror during pip freeze > requirements (or other potential bugs), do install from source for the latest version and save yourself some headache.Pulmonate
I'm still on Ubuntu 12.04, and I don't get pip-3.2 either. I have pip3.5 but it doesn't really install Python 3 stuff. It complains that (the Python 2 version of) pandas is already installed.Vick
pip>=8 does not work with Python 3.2. So you would need to do /usr/bin/easy_install3 --user "pip<8".Irradiation
Doesn't work with my Ubuntu 18.04 based system. The package python3-setuptools does not give me easy_install3.Polad
@Polad as per the note above the answer, for newer versions sudo apt install python3-pip is the easier option.Antiknock
L
267

You may want to build a virtualenv of python3, then install packages of python3 after activating the virtualenv. So your system won't be messed up :)

This could be something like:

virtualenv -p /usr/bin/python3 py3env
source py3env/bin/activate
pip install package-name
Lump answered 26/5, 2012 at 3:57 Comment(10)
Does virtualenv install pip even if you don't have it installed in the main Python 3? If so, then this does solve his problem. Otherwise not.Squatness
Yes, virtualenv installs pip.Lump
OK. I still think the answer doesn't really answer his question in a generic way, though.Squatness
virtualenv itself can be installed via pip to get the latest in older versions of Ubuntu: sudo pip install virtualenvWhalebone
This has the advantage of providing the latest version of both virtualenv and the pip that it provides to virtual environments.Whalebone
This works for me. Two additions: You can leave the virtualenv with deactivate and python3 might be installed at a different location. Mine is at /usr/local/bin/python3, which you can find out with which python3Mourner
Following @LennartRegebro and @user2503795, I can confirm that this is a bit more robust: virtualenv -p `which python3` py3envWillamina
Getting all kinds of access denied errors on Ubuntu. -- Tried sudo, still doesn't work.Eudy
This answer is outdated.Balata
Is it equivalent to python3 -m pip install <package_name> without virtual environment?Atrip
V
212

Short Answer

sudo apt-get install python3-pip
sudo pip3 install MODULE_NAME

Source: Shashank Bharadwaj's comment

Long Answer

The short answer applies only on newer systems. On some versions of Ubuntu the command is pip-3.2:

sudo pip-3.2 install MODULE_NAME

If it doesn't work, this method should work for any Linux distro and supported version:

sudo apt-get install curl
curl https://bootstrap.pypa.io/get-pip.py | sudo python3
sudo pip3 install MODULE_NAME

If you don't have curl, use wget. If you don't have sudo, switch to root. If pip3 symlink does not exists, check for something like pip-3.X

Much python packages require also the dev package, so install it too:

sudo apt-get install python3-dev

Sources:
python installing packages with pip
Pip latest install

Check also Tobu's answer if you want an even more upgraded version of Python.

I want to add that using a virtual environment is usually the preferred way to develop a python application, so @felixyan answer is probably the best in an ideal world. But if you really want to install that package globally, or if need to test / use it frequently without activating a virtual environment, I suppose installing it as a global package is the way to go.

Vote answered 21/10, 2012 at 19:25 Comment(7)
The curl call borks for me with a syntax error on line 48.Vick
@icedwater: can't help if you don't post the trace (use pastebin).Vote
Thanks @MarcoSulla, but I just re-ran this and noticed a UserWarning: Support for Python 3.0-3.2 has been dropped. Future versions will fail here. The paste is at ix.io/1fX5 for all interested parties :)Vick
A syntax error might occur when using a version of python that is no longer supported by pip. The above commands do work with python3.5Marcosmarcotte
I added a link to supported versions (currently: 2.6, 2.7, 3.3, 3.4, 3.5)Vote
if pip is installed already and it still doesn't work, I recommend to sudo apt-get purge python3-pip firstBillowy
FYI: Instead of using sudo pip3 install MODULE you can do and better do pip3 install MODULE --userErde
R
52

Well, on ubuntu 13.10/14.04, things are a little different.

Install

$ sudo apt-get install python3-pip

Install packages

$ sudo pip3 install packagename

NOT pip-3.3 install

Rubio answered 24/2, 2014 at 14:22 Comment(3)
Works on Ubuntu 14.04Junko
Works on Ubuntu 16.04Coel
This will work on all Ubuntu versions starting from 12.04Geode
M
42

The easiest way to install latest pip2/pip3 and corresponding packages:

curl https://bootstrap.pypa.io/pip/2.7/get-pip.py | python2
pip2 install package-name    

curl https://bootstrap.pypa.io/get-pip.py | python3
pip3 install package-name

Note: please run these commands as root

Munguia answered 26/5, 2012 at 4:39 Comment(0)
V
37

I had the same problem while trying to install pylab, and I have found this link

So what I have done to install pylab within Python 3 is:

python3 -m pip install SomePackage

It has worked properly, and as you can see in the link you can do this for every Python version you have, so I guess this solves your problem.

Vinegar answered 12/7, 2015 at 17:8 Comment(2)
Though Fedora has a python3-pip package, it does not create a pip3 or pip-3 command as suggested in other answers. This answer indeed works.Peaked
This works great, but it assumes that pip has already been installed via: sudo apt-get install python3-pipShackle
H
14

Old question, but none of the answers satisfies me. One of my systems is running Ubuntu 12.04 LTS and for some reason there's no package python3-pip or python-pip for Python 3. So here is what I've done (all commands were executed as root):

  • Install setuptools for Python3 in case you haven't.

    apt-get install python3-setuptools
    

    or

    aptitude install python3-setuptools
    
  • With Python 2.4+ you can invoke easy_install with specific Python version by using python -m easy_install. So pip for Python 3 could be installed by:

    python3 -m easy_install pip
    
  • That's it, you got pip for Python 3. Now just invoke pip with the specific version of Python to install package for Python 3. For example, with Python 3.2 installed on my system, I used:

    pip-3.2 install [package]
    
Hoover answered 23/6, 2013 at 23:29 Comment(0)
S
9

If you have pip installed in both pythons, and both are in your path, just use:

$ pip-2.7 install PACKAGENAME
$ pip-3.2 install PACKAGENAME

References:

This is a duplicate of question #2812520

Speer answered 28/5, 2012 at 13:35 Comment(0)
T
9

If your system has python2 as default, use below command to install packages to python3

$ python3 -m pip install <package-name>

Tarnish answered 16/1, 2019 at 10:20 Comment(0)
D
4

Easy enough:

sudo aptitude install python3-pip
pip-3.2 install --user pkg

If you want Python 3.3, which isn't the default as of Ubuntu 12.10:

sudo aptitude install python3-pip python3.3
python3.3 -m pip.runner install --user pkg
Disdainful answered 7/3, 2013 at 10:52 Comment(0)
L
4

You can alternatively just run pip3 install packagename instead of pip,

Lucilla answered 6/5, 2017 at 13:9 Comment(1)
Your answer is a duplicate to many answers on this page. (Search "pip3" in text) I don't see any added value of this.Latricialatrina
S
3

Firstly, you need to install pip for the Python 3 installation that you want. Then you run that pip to install packages for that Python version.

Since you have both pip and python 3 in /usr/bin, I assume they are both installed with a package manager of some sort. That package manager should also have a Python 3 pip. That's the one you should install.

Felix' recommendation of virtualenv is a good one. If you are only testing, or you are doing development, then you shouldn't install the package in the system python. Using virtualenv, or even building your own Pythons for development, is better in those cases.

But if you actually do want to install this package in the system python, installing pip for Python 3 is the way to go.

Squatness answered 26/5, 2012 at 4:39 Comment(0)
L
3

Although the question relates to Ubuntu, let me contribute by saying that I'm on Mac and my python command defaults to Python 2.7.5. I have Python 3 as well, accessible via python3, so knowing the pip package origin, I just downloaded it and issued sudo python3 setup.py install against it and, surely enough, only Python 3 has now this module inside its site packages. Hope this helps a wandering Mac-stranger.

Limonite answered 5/1, 2014 at 22:58 Comment(0)
K
1

Execute the pip binary directly.

First locate the version of PIP you want.

jon-mint python3.3 # whereis ip
ip: /bin/ip /sbin/ip /usr/share/man/man8/ip.8.gz /usr/share/man/man7/ip.7.gz

Then execute.

jon-mint python3.3 # pip3.3 install pexpect
Downloading/unpacking pexpect
  Downloading pexpect-3.2.tar.gz (131kB): 131kB downloaded
  Running setup.py (path:/tmp/pip_build_root/pexpect/setup.py) egg_info for package pexpect

Installing collected packages: pexpect
  Running setup.py install for pexpect

Successfully installed pexpect
Cleaning up...
Karelian answered 4/6, 2014 at 20:56 Comment(0)
C
0
  1. You should install ALL dependencies:

    sudo apt-get install build-essential python3-dev python3-setuptools python3-numpy python3-scipy libatlas-dev libatlas3gf-base

  2. Install pip3(if you have installed, please look step 3):

    sudo apt-get install python3-pip

  3. Iinstall scikit-learn by pip3

    pip3 install -U scikit-learn

  4. Open your terminal and entry python3 environment, type import sklearn to check it.

Celle answered 5/5, 2016 at 3:31 Comment(0)
C
0

To install pip for python3 use should use pip3 instead of pip. To install python in ubuntu 18.08 bionic
before installing a version of python, activate virtual environment so that it won't have any problem in a future versions of python.

virtualenv -p /usr/bin/python3 py3env
source py3env/bin/activate

then install the actual python version you want.

>> sudo apt-get install python3.7

To install the required pip package in ubuntu
>> sudo apt-get install python3-pip

Carp answered 28/2, 2020 at 14:0 Comment(0)
C
0

You Can Simply type in terminal/console .

Commands

sudo apt update
sudo apt upgrade
sudo apt install python3-pip3
pip3 install package-name
Cerebellum answered 3/4, 2022 at 4:39 Comment(0)
P
0

You can use pip3.2 install package-name or python3.2 -m pip install package-name

Pilsudski answered 30/9, 2023 at 15:52 Comment(1)
That answer was already given more than a decade ago. Please, when answering old questions, be sure to bring something new, that wasn't known back in the time.Mythopoeic
H
-1

Another way to install python3 is using wget. Below are the steps for installation.

wget http://www.python.org/ftp/python/3.3.5/Python-3.3.5.tar.xz
tar xJf ./Python-3.3.5.tar.xz
cd ./Python-3.3.5
./configure --prefix=/opt/python3.3
make && sudo make install

Also,one can create an alias for the same using

echo 'alias py="/opt/python3.3/bin/python3.3"' >> ~/.bashrc

Now open a new terminal and type py and press Enter.

Hemi answered 10/4, 2014 at 18:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.