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'
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.
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
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
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'
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
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.
This worked for me on Ubuntu 18.04:
$ python3.9 -m ensurepip