How I do:

Copy# Remove python2
sudo apt purge -y python2.7-minimal

# You already have Python3 but 
# don't care about the version 
sudo ln -s /usr/bin/python3 /usr/bin/python

# Same for pip
sudo apt install -y python3-pip
sudo ln -s /usr/bin/pip3 /usr/bin/pip

# Confirm the new version of Python: 3
python --version
Answer from BntMrx on Stack Overflow
Discussions

software installation - uninstall Python installed by compiling source? - Unix & Linux Stack Exchange
I installed Python 2.7.9 on Ubuntu 14.04 by compiling its source, by .configre, make, and make altinstall. make altinstall is because I don't want to overwrite the default Python 2.7.6. My self ins... More on unix.stackexchange.com
๐ŸŒ unix.stackexchange.com
March 17, 2015
python3 - How do I uninstall Python 2 from Ubuntu? - Unix & Linux Stack Exchange
Oddly didn't find any answers, and since I've never uninstalled anything from Linux, I don't know where to start. I need Python3, and only have SSH access to the machine where I'm doing this un/ins... More on unix.stackexchange.com
๐ŸŒ unix.stackexchange.com
Removing Python 2.7
We run Cloudron on an AWS EC2 instance ... box that are now only being fixed in Ubuntu's "Extended Security Maintenance" (esm3). As best I can tell, we're not actually using Python 2.7 for installed apps, so I'm wondering about uninstalling Python 2.7 entirely from our Clou... More on forum.cloudron.io
๐ŸŒ forum.cloudron.io
5
1
March 11, 2025
Can I safely remove Python 2.7.17
I am having some problems with some of my python scripts. I have Python 2.7.17 which is the default. I also have Python 3.10.7. Can I safely remove Python 2.7.17 ? More on ubuntu-mate.community
๐ŸŒ ubuntu-mate.community
0
0
October 7, 2022
๐ŸŒ
GitHub
gist.github.com โ€บ akshilshah โ€บ 46b0637888f9161eb31b4a7c21d207c1
Remove python 2.7 from ubuntu 18.04 ยท GitHub
Remove python 2.7 from ubuntu 18.04 ยท Raw ยท python-2.7-purge.sh ยท This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Top answer
1 of 5
41

The following commands will remove your make altinstall-ed python:

rm -f /usr/local/bin/python2.7
rm -f /usr/local/bin/pip2.7
rm -f /usr/local/bin/pydoc
rm -rf /usr/local/include/python2.7
rm -f /usr/local/lib/libpython2.7.a
rm -rf /usr/local/lib/python2.7

You might also have to do

rm -f /usr/local/share/man/python2.7.1
rm -rf /usr/local/lib/pkgconfig
rm -f /usr/local/bin/idle
rm -f /usr/local/bin/easy_install-2.7

Although make altinstall has served me well if the "system python" has a different major.minor number from the one you install, it doesn't work that well if only the micro number (the third position) differs. That number is excluded from the installed binary, and you end up with two versions pythonX.Y. This was always a problem but once distributions started shipping with system utilities based on 2.7.X this problem has been more severe as 2.7 is supposed to be the last of the Python2 series.

IMO the best approach to solve this problem is to prevent it from becoming one: configure python to install in a directory not used by any other python. On my system they go under /opt/python/X.Y.Z.

To use any of the Pythons installed there you use [virualenv][1] to make a new environment:

virtualenv --python=/opt/python/2.7.9/bin/python2.7 venv
source venv/bin/activate

or use [virtualenvwrapper][2]. I have some aliases for the latest versions in the series I work with.

If you are using tox for testing against multiple versions (you should) the following alias will help it find the various version:

alias tox='PATH=/opt/python/2.7.9/bin:/opt/python/2.6.9/bin:/opt/python/3.4.3/bin:/opt/python/3.3.6/bin:/opt/python/3.5-dev/bin:$PATH tox'

(these are currently the latest versions, I use a slightly different setup by maintaining links from /opt/python/2.7 to the latest /opt/python/2.7.9, and for the other minor numbers as well, within the process for downloading, building and installing a new python version)

These installs are never used directly. They are always used as the basis for virtualenv environments only, hence I don't care that they are not in my normal PATH.

2 of 5
37

Starting from @Anthon's rm list, and applying @bin-s advice to search for newer files, i came up with this bash-script to completely wipe-out my Python-3.6.6 (which had been installed from sources with make altinstall):

prefix='/usr/local/'
pyver='3.6'

rm -rf \
    $HOME/.local/lib/Python${pyver} \
    ${prefix}bin/python${pyver} \
    ${prefix}bin/python${pyver}-config \
    ${prefix}bin/pip${pyver} \
    ${prefix}bin/pydoc \
    ${prefix}bin/include/python${pyver} \
    ${prefix}lib/libpython${pyver}.a \
    ${prefix}lib/python${pyver} \
    ${prefix}lib/pkgconfig/python-${pyver}.pc \
    ${prefix}lib/libpython${pyver}m.a \
    ${prefix}bin/python${pyver}m \
    ${prefix}bin/2to3-${pyver} \
    ${prefix}bin/python${pyver}m-config \
    ${prefix}bin/idle${pyver} \
    ${prefix}bin/pydoc${pyver} \
    ${prefix}bin/pyvenv-${pyver} \
    ${prefix}share/man/man1/python${pyver}.1 \
    ${prefix}include/python${pyver}m
rm -rI ${prefix}bin/pydoc ## WARN: skip if other pythons in local exist.

Use it with care (e.g. add -I option in rm command, to verify each kill).

๐ŸŒ
Super User
superuser.com โ€บ questions โ€บ 842808 โ€บ unable-to-uninstall-python2-7-8-installed-in-downloads-directory
installation - Unable to uninstall python2.7.8 installed in ~/Downloads directory - Super User
I had a system-wide installation of python as well(some other version). To uninstall the 2.7.8 version, I issued the command sudo apt-get remove python which removed rhythmbox, totem etc.
Find elsewhere
Top answer
1 of 3
29

You can't.

From the Ubuntu wiki / Python:

Longer term plans (e.g. 14.04)

Move Python 2 to universe, port all Python applications in main to Python 3. We will never fully get rid of Python 2.7, but since there will also never be a Python 2.8, and Python 2.7 will be nearly 4 years old by the time of the 14.04 LTS release, it is time to relegate Python 2 to universe.

This means that a lot of base packages have hard dependencies on 2.7 and it will still take a lot of time tot get things migrated. Note that Python 3 has numerous backwards incompatible changes -- it's not a regular package upgrade.

If you really want to get rid of Python 2.7, you'll have to wait for the 14.04 release, but there's no guarantee.

2 of 3
15

Came here in 2019 because I develop in Python3 by default and came to the same conclusion as OP after seeing what'd be removed after running apt purge python

Since what I really wanted was to call Python3 with just python, I ran

sudo rm /usr/bin/python
sudo ln -s /usr/bin/python3 /usr/bin/python

This way, if Python2.7 is still needed, it can be called explicitly with python2.7 while simply calling python will default to Python3 because of the symbolic link.

I don't have any bash level scripts that call python2.7 with python so this change wouldn't be disruptive - while other systems would need their scripts adjusted accordingly if they did.

The main barrier to a distribution switching the python command from python2 to python3 isn't breakage within the distribution, but instead breakage of private third party scripts developed by sysadmins and other users.

- The "python" Command On Unix-Like Systems

This answer isn't a direct response to OP, but as someone who had a similar question this is the functionality I was looking for when I was thinking of removing 2.7. Rather than delete, just prioritize which one gets to use python.

๐ŸŒ
Alan C. Assis
acassis.wordpress.com โ€บ 2020 โ€บ 06 โ€บ 09 โ€บ removing-python-2-7-from-ubuntu-20-20
Removing python 2.7 from Ubuntu 20.04 โ€“ Alan C. Assis
September 1, 2020 - I solved my issues removing python 2 and installing python3 as default: ... After doing it I got IDF installed correctly. ... Like Loading... ... I am running Mint 19.3 which is derived from Ubuntu (Linux version 5.3.0-59-generic (buildd@lcy01-amd64-025) (gcc version 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04)) #53~18.04.1-Ubuntu SMP Thu Jun 4 14:58:26 UTC 2020).
๐ŸŒ
Ubuntu MATE Community
ubuntu-mate.community โ€บ support & help requests
Can I safely remove Python 2.7.17 - Page 2 - Support & Help Requests - Ubuntu MATE Community
October 7, 2022 - I am having some problems with some of my python scripts. I have Python 2.7.17 which is the default. I also have Python 3.10.7. Can I safely remove Python 2.7.17 ?
๐ŸŒ
Linux Hint
linuxhint.com โ€บ uninstall-python-ubuntu-22-04
Uninstall Python in Ubuntu 22.04 โ€“ Linux Hint
Tutorial on the multiple ways of removing Python from Ubuntu 22.04 by uninstalling both CPython and PyPy from the system using APT and from the source code.
๐ŸŒ
Medium
ascheng.medium.com โ€บ linux-uninstall-python-and-install-python-106969832734
[Linux] Uninstall python and Install python - Gavin A. S. Cheng - Medium
June 2, 2021 - # Remove python2 sudo apt purge -y python2.7-minimal # You already have Python3 but # don't care about the version sudo ln -s /usr/bin/python3 /usr/bin/python # Same for pip sudo apt install -y python3-pip sudo ln -s /usr/bin/pip3 /usr/bin/pip ...