如何在WSL环境中通过控制台将Python升级至最新版本(当前版本为3.8.10)
Upgrading Python to the Latest Version in WSL
Hey there! Upgrading Python on WSL (most folks use Ubuntu-based distros here, which I’ll assume is your case since you’re on 3.8.10) is totally doable with a couple of reliable methods. Let’s walk through them step by step:
Method 1: Use the Deadsnakes PPA (Recommended - Simple & Stable)
Deadsnakes is a trusted PPA that provides up-to-date Python versions for Ubuntu/Debian-based systems. This is the easiest way to get the latest Python without messing with system dependencies.
- First, update your system’s package list and upgrade existing packages to avoid conflicts:
sudo apt update && sudo apt upgrade -y - Install the tools needed to add new PPAs:
sudo apt install software-properties-common -y - Add the Deadsnakes PPA to your system:
sudo add-apt-repository ppa:deadsnakes/ppa -y - Refresh the package list again to recognize the new PPA’s packages:
sudo apt update - Install the latest Python version (replace
3.12with the actual latest version number, e.g.,3.13if it’s available):sudo apt install python3.12 python3.12-dev python3.12-venv -y - Optional: Set the new Python version as your default
python3command:- First, check the path of your newly installed Python:
which python3.12 - Add it to the system’s alternatives list:
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.12 1 - If you have multiple Python versions installed, you can select the default one interactively:
Just enter the number corresponding to your new Python version and hit enter.sudo update-alternatives --config python3
- First, check the path of your newly installed Python:
- Verify the upgrade worked:
You should see the latest version number printed here.python3 --version
Method 2: Compile from Source (For Custom Configurations)
If you need specific compile-time features or the PPA doesn’t have the version you want, compiling from source is an option. It’s a bit more involved but gives you full control.
- Install all dependencies required to compile Python:
sudo apt update && sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev wget -y - Download the latest Python source code (replace the URL with the link to the latest version from Python’s official site):
wget https://www.python.org/ftp/python/3.12.0/Python-3.12.0.tgz - Extract the downloaded archive:
tar -xf Python-3.12.0.tgz - Navigate into the extracted source directory:
cd Python-3.12.0 - Configure the build (the
--enable-optimizationsflag enables performance optimizations, which is recommended):./configure --enable-optimizations - Compile and install Python (use
-jfollowed by your CPU core count to speed up compilation, e.g.,-j4for a 4-core CPU):
We usemake -j4 sudo make altinstallaltinstallinstead ofinstallto avoid overwriting the system’s default Python version (which could break critical system tools). - Verify the installation:
You can also set this as your default using thepython3.12 --versionupdate-alternativessteps from Method 1 if you’d like.
Important Notes
- Don’t uninstall the system’s default Python 3.8: Ubuntu relies on this version for many core utilities, removing it could break your WSL instance.
- When creating virtual environments, make sure to specify the new Python version explicitly, e.g.,
python3.12 -m venv my_new_env.
内容的提问来源于stack exchange,提问作者Max Jahleel Castillo
相关产品推荐
相关产品推荐

