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

432,275

Solution 1

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

Solution 2

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!

Solution 3

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.

Solution 4

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

Solution 5

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

curl https://bootstrap.pypa.io/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

Share:
432,275
kev
Author by

kev

Simplicity is the ultimate sophistication. — Leonardo Da Vinci less is more The basic problem is actually very complicated. It's amazing that computers only use 0s and 1s. handy tools # download bigfile $ aria2c -c -k1M -{x,j,s}16 --checksum=md5=dccff28314d9ae4ed262cfc6f35e5153 http://mirrors.zju.edu.cn/ubuntu-releases/14.04/ubuntu-14.04-desktop-amd64.iso # backup txt files $ rsync -avzm --include '*/' --include '*.txt' --exclude '*' source/ remote:target/ # transfer file at 100kB/s $ rsync -hP --bwlimit 100 remote:file . # convert json to csv $ jq -r '[.field1,.field2,.field3]|@csv' input.json &gt; output.csv # http proxy $ ncat -v -l --proxy-type http 3128 # convert socks5 to http $ delegate -P8080 SERVER=http SOCKS=1.2.3.4:1080 [email protected] # convert socks5 to http with auth $ delegated -f -P8080 SERVER=http FORWARD=socks://user:[email protected]:1080 [email protected] # zero-padding file names $ rename 's/^\d+/sprintf("%02d", $&amp;)/e' [0-9]* # concat video parts $ printf "file '%s'\n" part*.mp4 | ffmpeg -f concat -i - -c copy all.mp4 # scan raspberry pi $ sudo nmap -n -sP 192.168.1.0/24 -oX - | xmlstarlet sel -t -m '//host[address[contains(@vendor, "Raspberry Pi")]]/address[@addrtype="ipv4"]/@addr' -v . -n | ssh-keyscan -t rsa -f - | sort -u - ~/.ssh/known_hosts -o ~/.ssh/known_hosts # upgrade all outdated python packages $ pip list --outdated | awk '{print $1}' | xargs -tn1 pip install # generate birthday wordlist for aircrack-ng $ dateseq -f '%Y%m%d' 1970-01-01 2019-01-01 &gt; birthday.txt # install python on raspberry pi $ ansible pi --limit pi2 -e ansible_user=alarm --ask-pass --su --ask-su-pass -m raw -a 'pacman -Sy --noconfirm python2' # resize sdcard for raspberry pi $ echo -e 'p\nd\n2\nn\np\n2\n\n\np\nw' | fdisk /dev/mmcblk0 $ reboot $ resize2fs /dev/mmcblk0p2 # authorized key initialization $ sshpass -p xxxxxx ssh-copy-id -o StrictHostKeyChecking=no user@server # open google chrome in full-screen mode $ open -a 'Google Chrome' --args --kiosk https://github.com/ # customized hostname $ curl --resolve 'httpbin:80:23.22.14.18' http://httpbin/headers # add ssh pubkey to multiple servers $ ansible rpi -m authorized_key -a 'user=pi key="ssh-rsa ..."' # brute force attack openwrt $ hydra -l root -P password.list 192.168.1.1 http-form-post '/cgi-bin/luci:luci_username=^USER^&amp;luci_password=^PASS^:S=302 Found' # dump obs-studio events $ websocat -t -u autoreconnect:ws://127.0.0.1:4444 reuse:appendfile:obs.jl

Updated on December 23, 2021

Comments

  • kev
    kev over 2 years

    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?

  • Lennart Regebro
    Lennart Regebro almost 12 years
    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.
  • Felix Yan
    Felix Yan almost 12 years
    Yes, virtualenv installs pip.
  • Lennart Regebro
    Lennart Regebro almost 12 years
    OK. I still think the answer doesn't really answer his question in a generic way, though.
  • KomodoDave
    KomodoDave over 11 years
    Perfect and proper, this should be the accepted answer. However you should specify sudo pip-3.2 install <package>, superuser permissions are required.
  • jarondl
    jarondl over 11 years
    I find it better to use --user to install in ~.local/, than to use sudo. see pep 370
  • chobok
    chobok over 11 years
    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.
  • Shashank Bharadwaj
    Shashank Bharadwaj over 11 years
    You can combine steps 1 and 2 and just do: sudo apt-get install python3-pip
  • akaIDIOT
    akaIDIOT over 11 years
    packages.ubuntu.com shows this super handy package for Ubuntu 12.10, but not for 12.04: packages.ubuntu.com/… (great find though ;))
  • Jacob Wan
    Jacob Wan about 11 years
    virtualenv itself can be installed via pip to get the latest in older versions of Ubuntu: sudo pip install virtualenv
  • Jacob Wan
    Jacob Wan about 11 years
    This has the advantage of providing the latest version of both virtualenv and the pip that it provides to virtual environments.
  • papirrin
    papirrin over 10 years
    In Ubuntu 13.04 and others, pip-3.3 is the current command. Maybe the answer should be updated?
  • akaIDIOT
    akaIDIOT over 10 years
    @papirrin 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.
  • user2503795
    user2503795 over 10 years
    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 python3
  • Adam Ryczkowski
    Adam Ryczkowski almost 10 years
    Works on Ubuntu 14.04
  • danorton
    danorton over 9 years
    Also available in Debian 7.7 (wheezy).
  • 6005
    6005 about 9 years
    pip-3.3, pip-3.4 etc no longer work. It is now just: pip, pip2, pip3. (At least on Ubuntu 14.04)
  • repzero
    repzero over 8 years
    this solution also works on debian wheezy and kali linux
  • Yibo Yang
    Yibo Yang over 8 years
    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.
  • icedwater
    icedwater over 8 years
    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.
  • icedwater
    icedwater over 8 years
    The curl call borks for me with a syntax error on line 48.
  • Skippy le Grand Gourou
    Skippy le Grand Gourou about 8 years
    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.
  • samstav
    samstav about 8 years
    Following @LennartRegebro and @user2503795, I can confirm that this is a bit more robust: virtualenv -p `which python3` py3env
  • Shirish Kadam
    Shirish Kadam almost 8 years
    Works on Ubuntu 16.04
  • Marco Sulla
    Marco Sulla over 7 years
    @icedwater: can't help if you don't post the trace (use pastebin).
  • icedwater
    icedwater over 7 years
    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 :)
  • user1251007
    user1251007 over 7 years
    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.5
  • Marco Sulla
    Marco Sulla over 7 years
    I added a link to supported versions (currently: 2.6, 2.7, 3.3, 3.4, 3.5)
  • Albert
    Albert over 7 years
    pip>=8 does not work with Python 3.2. So you would need to do /usr/bin/easy_install3 --user "pip<8".
  • BrainSlugs83
    BrainSlugs83 over 7 years
    Getting all kinds of access denied errors on Ubuntu. -- Tried sudo, still doesn't work.
  • Pynchia
    Pynchia about 7 years
    if pip is installed already and it still doesn't work, I recommend to sudo apt-get purge python3-pip first
  • Gabriel
    Gabriel about 7 years
    This works great, but it assumes that pip has already been installed via: sudo apt-get install python3-pip
  • Billal Begueradj
    Billal Begueradj almost 7 years
    This will work on all Ubuntu versions starting from 12.04
  • hynekcer
    hynekcer almost 7 years
    Your answer is a duplicate to many answers on this page. (Search "pip3" in text) I don't see any added value of this.
  • Vitalii Dmitriev
    Vitalii Dmitriev over 6 years
    FYI: Instead of using sudo pip3 install MODULE you can do and better do pip3 install MODULE --user
  • n.caillou
    n.caillou about 6 years
    This answer is outdated.
  • vineeshvs
    vineeshvs almost 5 years
    Is it equivalent to python3 -m pip install <package_name> without virtual environment?
  • jbo5112
    jbo5112 almost 5 years
    Doesn't work with my Ubuntu 18.04 based system. The package python3-setuptools does not give me easy_install3.
  • akaIDIOT
    akaIDIOT almost 5 years
    @jbo5112 as per the note above the answer, for newer versions sudo apt install python3-pip is the easier option.