How to Completely Uninstall Python and pip on Ubuntu/Debian Systems Using apt-get


13 views

When working with Python development on Linux systems, it's common to accidentally install outdated packages through the package manager. The command:

sudo apt-get install python-pip python-dev

typically installs Python 2.7 and its corresponding pip version, which creates conflicts when you actually need Python 3.x for modern development.

To completely remove Python 2.7 and its associated pip installation, use these commands in sequence:

sudo apt-get purge python-pip
sudo apt-get purge python-dev
sudo apt-get autoremove

Sometimes additional cleanup is needed:

# Remove residual config files
sudo apt-get purge python.*
# Clean up any remaining directories
sudo rm -rf /usr/local/lib/python2.7/
sudo rm -rf ~/.local/lib/python2.7/

After uninstallation, verify with:

which python
which pip
python --version
pip --version

All commands should return "not found" or similar messages indicating successful removal.

For clean Python 3 installation:

sudo apt-get update
sudo apt-get install python3 python3-pip python3-dev
  • Never use sudo apt-get remove instead of purge (leaves config files)
  • Don't manually delete Python from /usr/bin/ (can break system tools)
  • Be cautious with sudo apt-get purge python.* (verify it won't remove system-critical packages)

For developers needing multiple Python versions:

curl https://pyenv.run | bash
exec $SHELL
pyenv install 3.9.7
pyenv global 3.9.7

When you run sudo apt-get install python-pip python-dev on Ubuntu/Debian systems, you're actually installing:

1. Python 2.7 (default version)
2. pip for Python 2.7
3. Python development headers

To completely remove these packages and their dependencies:

# Remove pip specifically
sudo apt-get remove python-pip

# Remove Python 2.7 and associated packages
sudo apt-get remove python2.7 python2.7-minimal

# Remove any leftover dependencies
sudo apt-get autoremove

# For thorough cleanup (optional)
sudo apt-get purge python2.7 python-pip

After uninstallation, you might want to clean residual config files:

# Find remaining Python 2.7 config files
sudo find / -name "*python2.7*"

# Remove them selectively (be careful!)
sudo rm -rf /path/to/python2.7/config

Check if Python 2.7 and pip are truly gone:

# Check Python 2.7
python2.7 --version
# Should return "command not found"

# Check pip
pip --version
# Should return "command not found"

After cleanup, install Python 3 and pip3 correctly:

# Install Python 3
sudo apt-get install python3

# Install pip for Python 3
sudo apt-get install python3-pip

# Verify installation
python3 --version
pip3 --version

If you encounter issues:

# If you get "unable to locate package" errors
sudo apt-get update

# If there are broken dependencies
sudo apt-get install -f

# Alternative way to install pip for Python 3
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
sudo python3 get-pip.py

For developers needing both versions:

# Install python2 and python3 simultaneously
sudo apt-get install python2.7 python3

# Use virtual environments to manage dependencies
sudo pip3 install virtualenv
virtualenv -p python3 myenv
source myenv/bin/activate