In the world of Python development, having the flexibility to switch between different Python versions is crucial. This not only allows developers to stay up-to-date with the latest language features but also ensures compatibility with different projects.
In this guide, we will explore two popular methods for managing Python versions on Ubuntu: update-alternatives and pyenv.
🧱 Requirements: Before You Begin
If you are using Method 1 (update-alternatives) or Method 2 (pyenv) to manage Python versions, make sure your WSL Ubuntu system has the necessary build tools installed — especially if you plan to compile Python from source using pyenv
.
Without these tools, you may get errors like “no acceptable C compiler found” during installation.
👉 To install the required packages, run this command:
sudo apt update && sudo apt install -y make build-essential libssl-dev zlib1g-dev \ libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncursesw5-dev \ xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev
🧠 Explanation:
build-essential
: Required for compiling C code.libssl-dev
,libbz2-dev
, etc.: These libraries help build Python with full features like SSL, compression, and database support.
✅ This setup is compatible with WSL Ubuntu 20.04, 22.04, and 24.04.
🧩Method 1: Using update-alternatives
Step 1: Check available Python versions
Open a terminal and use the following command to view the available Python versions:
sudo update-alternatives --config python
If your system does not have any alternatives registered for the python
command, then proceed next.
Step 2: Check available Python versions
Check available Python versions:
ls /usr/bin/python*
You will see something like:
/usr/bin/python3
If only default python you’ll see on the output then proceed on next step if you want to add new Python versions.
Step 3: Add alternatives manually
Example if you want to add Python 3.12, run command:
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.12 1
Repeat for another version (optional):
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.11 2
Step 4: Choose the default version
sudo update-alternatives --config python
Then select the desired Python version you want by entering the corresponding number and pressing Enter.
Step 5: Check the Python version
python --version
That’s it for method 1.
🐍 Method 2: Using pyenv
Step 1: Install pyenv
Install pyenv using the following commands.
git clone https://github.com/pyenv/pyenv.git ~/.pyenv echo 'export PATH="$HOME/.pyenv/bin:$PATH"' >> ~/.bashrc echo 'eval "$(pyenv init --path)"' >> ~/.bashrc source ~/.bashrc
Step 2: Install the Desired Python Version
Use pyenv to install the Python version you need. For example, to install Python 3.12.0:
pyenv install 3.12.0
Step 3: Set the Global Python Version
Set the global Python version to the one you just installed:
pyenv global 3.12.0
Final Step: Verify the Switch
Check that the switch was successful by running:
python --version
You can also view a list of installed Python versions and their settings using:
pyenv versions
Optional: Using pyenv-virtualenv
The pyenv-virtualenv
is an optional extension to pyenv
that provides additional functionality for managing virtual environments.
Install pyenv-virtualenv plugin
Install the pyenv-virtualenv
plugin to manage virtual environments:
git clone https://github.com/pyenv/pyenv-virtualenv.git "$(pyenv root)"/plugins/pyenv-virtualenv export PATH="$HOME/.pyenv/plugins/pyenv-virtualenv/bin:$PATH" eval "$(pyenv virtualenv-init -)" source ~/.bashrc
Create and activate a virtual environment
Create a virtual environment with the desired Python version:
pyenv virtualenv 3.12.0 myenv
Activate the virtual environment:
pyenv activate myenv
Verify the Python version in the virtual environment
Ensure the correct Python version is active within the virtual environment:
python --version
Conclusion
Whether you prefer the system-wide approach of update-alternatives
or the more flexible per-user and per-project management of Python versions with pyenv
, these methods provide you with the tools needed to seamlessly switch between Python versions on your Ubuntu system. Choose the one that best fits your development workflow and project requirements.
Happy coding!