Python is a versatile programming language widely used for various applications. Ubuntu, a popular Linux distribution, often comes with a default Python version pre-installed. However, you might need to switch between different Python versions for various projects or compatibility reasons. In this detailed guide, we will walk you through the steps to change the Python version in Ubuntu, ensuring your development environment is tailored to your needs.
1. Check Current Python Version
Before making any changes, it’s important to check the current Python version installed on your system. Open your terminal and run:
python --version
python3 --version
2. Install the Desired Python Version
If the required Python version is not installed on your system, you can install it using the following commands. For example, to install Python 3.9, use:
sudo apt update
sudo apt install python3.9
3. Update Alternatives
Ubuntu uses the update-alternatives
command to manage multiple versions of a program. To configure the default Python version, follow these steps:
3.1. Add the New Python Version
First, add the new Python version to the alternatives system:
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.8 1
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.9 2
3.2. Configure the Default Version
Next, configure the default Python version by selecting the appropriate version from the list:
sudo update-alternatives --config python
You will be prompted to choose which version to use as the default. Enter the selection number corresponding to your desired Python version.
4. Verify the Change
After updating the alternatives, verify that the change has been applied successfully:
python --version
5. Update Python3 Alternatives (Optional)
If you also need to update the default python3
version, follow similar steps:
5.1. Add the New Python3 Version
Add the new Python3 version to the alternatives system:
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 2
5.2. Configure the Default Version
Configure the default python3
version by selecting the appropriate version from the list:
sudo update-alternatives --config python3
5.3. Verify the Change
Verify that the change has been applied successfully:
python3 --version
6. Creating Virtual Environments
For projects that require different Python versions, it’s often best to use virtual environments. This allows you to isolate dependencies and manage multiple Python environments efficiently.
6.1. Install Virtualenv
First, install virtualenv
if you haven’t already:
sudo apt install python3-venv
6.2. Create a Virtual Environment
Create a virtual environment with the desired Python version:
python3.9 -m venv myenv
6.3. Activate the Virtual Environment
Activate the virtual environment:
source myenv/bin/activate
To deactivate the environment, simply run:
deactivate
Conclusion
Changing the Python version in Ubuntu is a straightforward process that involves updating alternatives and possibly installing the desired version. By following the steps outlined in this guide, you can easily switch between different Python versions to suit your project needs. Additionally, using virtual environments allows for even greater flexibility in managing project-specific dependencies and Python versions.