Create a script to completely and fully remove python from ubuntu22.04 including all its setup, config files, pip, pip packages, site packages, and any other related files and directories then install python 3.11
Assistant
Removing Python entirely from Ubuntu can severely break your system because many Ubuntu components and utilities depend on Python to function correctly. It's strongly recommended not to remove the system Python installation. However, you can remove user-installed Python packages, custom installations, and any configurations you have added, and then install Python 3.11.
Below is a bash script that cleans up user-installed Python packages and configurations, and then installs Python 3.11 on Ubuntu 22.04 using the deadsnakes PPA.
bash
#!/bin/bash
# Script to clean up user-installed Python packages and install Python 3.11 on Ubuntu 22.04
# WARNING: Removing system Python packages can break your system.
# Proceed with caution and avoid removing default system packages.
echo "Starting the Python cleanup and Python 3.11 installation script..."
# Update package list
echo "Updating package list..."
sudo apt update
# Remove pip and pip packages for Python 2
echo "Removing Python 2 pip and associated packages..."
sudo apt remove -y python-pip
sudo rm -rf ~/.local/lib/python2.*
sudo rm -rf ~/.cache/pip
# Remove pip and pip packages for Python 3
echo "Removing Python 3 pip and associated packages..."
sudo apt remove -y python3-pip
sudo rm -rf ~/.local/lib/python3.*
sudo rm -rf ~/.cache/pip
# Remove any Python packages installed in /usr/local
echo "Cleaning up Python installations in /usr/local..."
sudo rm -rf /usr/local/lib/python2.*
sudo rm -rf /usr/local/lib/python3.*
sudo rm -rf /usr/local/bin/python2.*
sudo rm -rf /usr/local/bin/python3.*
sudo rm -rf /usr/local/bin/pip*
sudo rm -rf /usr/local/bin/easy_install*
sudo rm -rf /usr/local/include/python2.*
sudo rm -rf /usr/local/include/python3.*
# Clean up any remaining python files
echo "Cleaning up remaining Python files..."
sudo rm -rf /usr/local/share/python
sudo rm -rf /usr/lib/python-exec
sudo rm -rf /usr/lib/python-wheels
# Avoid removing /usr/lib/python3 as it may be required by the system
sudo rm -rf /etc/python
sudo rm -rf /etc/python2.*
sudo rm -rf /etc/python3.*
# Autoremove unnecessary packages
echo "Removing unnecessary packages..."
sudo apt autoremove -y
sudo apt autoclean -y
# Install required packages for adding PPAs
echo "Installing software-properties-common..."
sudo apt install -y software-properties-common
# Add deadsnakes PPA for latest Python versions
echo "Adding deadsnakes PPA..."
sudo add-apt-repository -y ppa:deadsnakes/ppa
# Update package list after adding new PPA
echo "Updating package list after adding deadsnakes PPA..."
sudo apt update
# Install Python 3.11 and associated packages
echo "Installing Python 3.11 and associated packages..."
sudo apt install -y python3.11 python3.11-venv python3.11-dev python3.11-distutils
# Install pip for Python 3.11
echo "Installing pip for Python 3.11..."
curl -sS https://bootstrap.pypa.io/get-pip.py | sudo python3.11
# Verify the Python 3.11 installation
echo "Verifying Python 3.11 and pip installations..."
python3.11 --version
pip3.11 --version
echo "Python 3.11 installation script completed successfully."
Instructions:
Save the Script:
Save the script to a file, for example install_python3.11.sh:
bash
nano install_python3.11.sh
Paste the script into the file, then save and exit (press Ctrl+X, then Y, and enter).
Make the Script Executable:
bash
chmod +x install_python3.11.sh
Run the Script:
bash
./install_python3.11.sh
Important Notes:
Backup Important Data:
Before running scripts that modify system files, always back up important data.
Understand the Script:
Ensure you understand each step of the script. It removes user-installed Python configurations and packages but avoids removing system-critical Python packages.
System Stability:
This script is designed to prevent breaking your system by not removing default Python packages required by Ubuntu. However, use it at your own risk.
Python 3.11 Usage:
After installation, use python3.11 and pip3.11 commands to work with Python 3.11.
Verification:
After running the script, verify the installation:
bash
python3.11 --version
pip3.11 --version
You should see outputs similar to:
code
Python 3.11.0
pip X.Y.Z from /usr/local/lib/python3.11/site-packages/pip (python 3.11)
Additional Setup:
If you need to set Python 3.11 as the default python3, you can update the alternatives:
This script helps you clean up user-installed Python packages and configurations and installs Python 3.11 safely on Ubuntu 22.04 without disrupting system-critical Python installations. Always proceed with caution when modifying system files and ensure you have backups of important data.