71

Ubuntu 20.04 comes with Python 3.8. I cannot uninstall Python 3.8 but I need Python 3.9

I went ahead and installed Python 3.9 from:

sudo add-apt-repository ppa:deadsnakes/ppa

sudo apt install python3.9

How do I install pip for python 3.9?

Installing pip using sudo apt-get install python3-pip does not work for me as it installs pip for python 3.8

Installing pip using python3.9 get-pip.py gives an error:

~/python_tools$ python3.9 get-pip.py 
Traceback (most recent call last):
  File "/home/ubuntu/python_tools/get-pip.py", line 23704, in <module>
    main()
  File "/home/ubuntu/python_tools/get-pip.py", line 198, in main
    bootstrap(tmpdir=tmpdir)
  File "/home/ubuntu/python_tools/get-pip.py", line 82, in bootstrap
    from pip._internal.cli.main import main as pip_entry_point
  File "<frozen zipimport>", line 259, in load_module
  File "/tmp/tmpkwyc8h7j/pip.zip/pip/_internal/cli/main.py", line 10, in <module>
  File "<frozen zipimport>", line 259, in load_module
  File "/tmp/tmpkwyc8h7j/pip.zip/pip/_internal/cli/autocompletion.py", line 9, in <module>
  File "<frozen zipimport>", line 259, in load_module
  File "/tmp/tmpkwyc8h7j/pip.zip/pip/_internal/cli/main_parser.py", line 7, in <module>
  File "<frozen zipimport>", line 259, in load_module
  File "/tmp/tmpkwyc8h7j/pip.zip/pip/_internal/cli/cmdoptions.py", line 18, in <module>
ModuleNotFoundError: No module named 'distutils.util'
Gilson
  • 1,708
  • 3
  • 16
  • 21
  • I recommend checking Eric Platon's answer https://stackoverflow.com/a/68523326/435129 before jumping into curl – P i Aug 21 '21 at 21:56

8 Answers8

140

You can install pip for python 3.9 the following way:

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python3.9 get-pip.py

It is important you use python3.9 instead of just python3, to ensure pip is installed for python 3.9.

If you see any permissions errors, you may need to use

python3.9 get-pip.py --user

If you get an error like No module named 'distutils.util' when you run python3.9 get-pip.py, and you are on a Debian-based Linux distribution, run

sudo apt install python3.9-distutils

and then rerun your get-pip.py command. If you are not on a Debian-based distribution, use the equivalent command for your distribution's package manager.

These instructions are based in part on the official installation instructions provided by the pip maintainers.


This portion of my answer is a bit out of the scope of the question, since the question is specifically for python 3.9. However, for anyone trying to install pip on python 3.6 or older, at the time of writing the file at https://bootstrap.pypa.io/get-pip.py only supports python 3.7 or newer.

The workaround is to instead download from https://bootstrap.pypa.io/pip/<python version>/get-pip.py instead. For example, if you want to install pip for python 3.6, then you can download from https://bootstrap.pypa.io/pip/3.6/get-pip.py, and then follow all of the steps above as usual.

Shane Bishop
  • 3,905
  • 4
  • 17
  • 47
  • thanks for your answer but I get an error when I try get-pip.py I added the error to the body of my question. – Gilson Jan 09 '21 at 17:00
  • 1
    @Gilson I updated my answer to include a solution for that – Shane Bishop Jan 09 '21 at 17:19
  • thank you! I appreciate your help. It worked! – Gilson Jan 10 '21 at 02:59
  • 2
    type "pip3.9 install" to use pip – S7bvwqX Jun 23 '21 at 08:11
  • pip3 install -t ~/.local/lib/python3.9/site-packages/ selenium – VikingGlen Jul 18 '21 at 23:42
  • 2
    With `sudo apt install python3.9-distutils` it installs `python3.8` on Ubuntu 20.04. I want only `python3.9` – Timo Nov 21 '21 at 19:07
  • on ubuntu18, seems `sudo apt install python3.9-distutils` will got `python3.9 -m pip` ready. without `get-pip.py`. – yurenchen Dec 13 '21 at 14:24
  • For anyone suggesting edits, please note that (as I point out in my answer) my answer is based in part on the [official instructions](https://pip.pypa.io/en/stable/installation/) for installing pip provided by the pip maintainers. Edit suggestions to install pip using a package manager or other similar changes are against the intent of my answer. If you want to offer ways to install via package manager, please leave that as an answer of your own. Also, I left out `ensurepip` so that my answer would be simpler. – Shane Bishop Dec 14 '21 at 02:45
  • Do check `sudo apt-get install python3.9-venv` in Eric Platon's answer below - that approach saves you having to set PATHs etc for the new binaries `get-pip.py` installs, which I'd rather avoid as it saves manual steps and config clutter. – Chris Dec 30 '21 at 10:43
  • 1
    I just get two pages of stack traces. – Owl Mar 08 '22 at 18:11
  • Someone referred to here but working on Ubuntu 18.04, Python 3.6 installed. But get-pip.py now require Python's minimal version is 3.7: ```Python min_version = (3, 7) if this_python < min_version: ``` – Keelung Aug 17 '22 at 03:54
  • @Keelung, please see my updated answer. I give instructions that should work for Python 3.6. – Shane Bishop Aug 17 '22 at 11:47
  • python3.9 get-pip.py results in Traceback (most recent call last): File "/home/mrt/get-pip.py", line 32099, in main() ... ModuleNotFoundError: No module named 'distutils.cmd' – Frederick Ollinger Sep 27 '22 at 18:45
30

An alternative that relies only on deadsnakes/ppa is to install python3.9-venv.

sudo apt-get install python3.9-venv
python3.9 -m venv venv
source venv/bin/activate
pip --version
# pip 21.1.3 from /home/.../venv/lib/python3.9/site-packages/pip (python 3.9)

Perhaps easier to keep coherent over time, but forcing into Virtualenv.


This method was born out a problem on Ubuntu 18. Other proposals in the thread aimed at OP's target (20.04) did not work. The install script from PyPa ends on Ubuntu 18 with:

python3.9 get-pip.py
# ...
# AttributeError: 'HTMLParser' object has no attribute 'unescape'
Eric Platon
  • 9,819
  • 6
  • 41
  • 48
  • 3
    This answer needs to find its way to the top – P i Aug 21 '21 at 21:55
  • `sudo apt-get install python3.10-venv` is now more appropriate these days. – Timo Nov 21 '21 at 19:21
  • What is `python3.9 - m venv venv` doing. I read in `man` `-m module-name Searches sys.path for the named module and runs the corresponding .py file as a script. This terminates the option list (following options are passed as arguments to the module).`. So is it a prep for the following `source` to work? – Timo Nov 21 '21 at 19:22
  • 1
    @Timo Yes, that is to allow for the `source` command. The `venv` module call creates a `venv` directory (unfortunate use of the same name?) to contain the virtual development environment for this project. Under this directory, the tree contains the dependencies under `lib` and executable scripts under `bin` (among others). In particular `activate` and `deactivate` allow to set or unset the virtual environment in the current shell (so calling `pip` or `python` uses the `venv/bin` ones instead of whatever default is available. – Eric Platon Aug 03 '22 at 05:46
15

Pip is included by default in python 3.4 and later.

python3.9 -m pip --version

If, for some reason, pip is not installed, you can install it manually by using get-pip:

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python3.9 get-pip.py
dellitsni
  • 417
  • 2
  • 9
3

If anyone else is running into seemingly bizarre WSL2 behavior from their pips, TechDog's suggestion fixed my WSL2 Ubuntu 20.04. It was the update-alternatives line, exactly as TechDog posted, that did the trick!

update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 1
Gnubesoft
  • 31
  • 2
  • Owl, check your Python version and path. Was it 3.10 or 3.9? – Gnubesoft Mar 09 '22 at 22:17
  • I fixed it, it was a really weird problem: the links pointed to 3.9 but python3 was 3.6. However after a reboot, python3 was correct. Very strange indeed. – Owl Mar 10 '22 at 03:47
  • 1
    I hate those bugs! Glad it worked out for you after all. – Gnubesoft Mar 11 '22 at 04:14
  • Running Ubuntu 20.04 in Windows 10 WSL2. Ran into this issue setting up my python dev environment. This resolved my issue of "pip --version" returning `pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.8)` -> to -> `pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.9)` – Chris - Haddox Technologies Sep 19 '22 at 19:54
3

this is a weird one, but it's the easiest and it works:

export PYTHON_VERSION_SHORT=3.9
apt-get install -y python${PYTHON_VERSION_SHORT} python3-pip && \
ln -s -f /usr/bin/python${PYTHON_VERSION_SHORT} /usr/bin/python3 && \
ln -s -f /usr/bin/python${PYTHON_VERSION_SHORT} /usr/bin/python && \
ln -s -f /usr/bin/pip3 /usr/bin/pip

when you install pip3, it's (at the time of writing) installed for python3.8. but if you overwrite /usr/bin/python3 to link to python3.9, pip3 will then be interpreted using python3.9, and you'll have a working pip against python3.9

I've been using this for some two years without a problem, but fingers crossed because it's no good practice at all, it'll break if python3-pip and python3.9 have compatibility issues.

timfeirg
  • 1,426
  • 18
  • 37
2

Below are the steps I used to install in UBUNTU 16.4., prefix SUDO if needed. I had some issues in using python in the command line so I have used update-alternatives to default python3.9 to python command, please change the version if needed.

apt update
apt install software-properties-common
add-apt-repository ppa:deadsnakes/ppa -y
apt update
apt install python3.9
update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 1
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python3.9 get-pip.py
TechDog
  • 3,039
  • 1
  • 24
  • 30
2

This worked for me on Ubuntu 18.04:

$ python3.9 -m ensurepip
Rob Agar
  • 12,337
  • 5
  • 48
  • 63
2

If you're building a Docker container, the following Dockerfile installs Python 3.9 on Ubuntu 20.04 (LTS):

FROM ubuntu:20.04
RUN set -ex && \
    apt install -y \
        software-properties-common && \
    add-apt-repository -y ppa:deadsnakes/ppa && \
    apt install -y \
        python3.9 \
        python3.9-distutils \
        python3.9-venv && \
    python3.9 --version && \
    python3.9 -m ensurepip && \
    pip3.9 --version
ENTRYPOINT []

The software-properties-common package introduces add-apt-repository. Installing the python3.9-distutils and python3.9-venv allows ensurepip to be invoked directly.

Anyway, the Python standard library is supposed to include ensurepip as of Python 3.5+ but distro maintainers seem to separate the installer into smaller pieces.

ingyhere
  • 11,818
  • 3
  • 38
  • 52