I updated my Ubuntu from 22.04 to 24.04 Previously python 3.10 was getting used Now the default version is python3.12
- Python 3.12 seems to call for virtual environment usage, Have been going through some documentation and forums, but was thinking if it is better to go back to python3.10
- Would you recommend update-alternative where I can have both versions?
[sudo] password for meena:
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Package python3.10 is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source
E: Package 'python3.10' has no installation candidate
No, it's not better to go back to Python 3.10.
Or more precisely, it's not better to change the system installation of Python back to 3.10. Your system is built in a way that it expects to have Python 3.12 available, and if you remove it, various things will stop working.
There are two changes you can and probably should make to your Python usage:
Virtual environments are now a recommended practice for all versions of Python, so you should start using them. There are numerous tools that you can use to create and manage virtual environments; for example, I particularly recommend pipx for installing Python applications, i.e. anything you intend to run, but not use in your own code. If you're going to be writing your own Python code, on the other hand, consider something like hatch (my favorite), pdm, flit, or poetry (which is a little less polished than the others; it slightly predates some of the modern standards in Python development). You might also consider pipenv, which is a simpler tool that just creates a virtual environment and manages the list of packages installed in it, without the development project features the others have.
You can also create virtual environments manually using the
venv
module which is built into the Python standard library (python -m venv <path>
), or virtualenv which is a slightly more sophisticated version of the same thing. Each of these tools has its own unique workflow so you can try them and see what you like.If you need access to different versions of Python other than 3.12, I'd suggest installing them "manually", not using
apt
. Perhaps the easiest way to do this is with something like pyenv, which gives you a fairly simple tool that can install, upgrade, and uninstall many different versions of Python without touching the one used by your system. After you install the version you need, you can then use it to create virtual environments, or you can have your tool of choice use it to create virtual environments.There are other tools like rye and uv (now in the process of merging... sort of?) which try to do all of the above things - they will let you create and manage virtual environments, and also let you install and manage versions of Python. Feel free to give those a try if that sounds useful.
Don't change the default python version. That will destroy Ubuntu.
Instead, use a virtual environment with Miniforge.
Download the 64 installer for linux, as described here.
Now install it.
Now create a virtual env with python3.10
Activate this environment with
Now run
python3
, and it should bepython3.10
.I've found that pyenv is more comfortable to use than other tools like anaconda.
You can find the installation instructions here: https://github.com/pyenv/pyenv#automatic-installer
After installing it, using it as simple as running
to install a given python version.
If you want to use python version globally, you can set it with
and if you want to set it only on a project, you can do so with:
This will create a
.python-version
file containing the version, and pyenv will automatically run that version when you run python in that directory. This is useful when you're working on various projects that require different python versions, which can happen often in a professional setting or with certain OSS projects.Do keep in mind that this gives each python version a virtualenv, and that packages you installed on system python, even if they were on the same version at the time, will not be accessible.
Here's how you use Python and its packages, in order of my preference:
1. Use system Python and system packages
On Ubuntu 24.04, that means Python 3.12. You install required packages through apt (e.g.
apt install python3-numpy
orapt install python3-django
).2. Use system Python and Virtual Environment
Again, for you that means Python 3.12. Set up a Venv and install packages there (e.g.
virtualenv venv; source venv/bin/activate; pip install whatever
).3. Use pyenv or something like anaconda
Unlike the previous solutions, this allows you to get different Python versions. Try to avoid this, if you can. If software works on Python 3.10 but breaks on Python 3.12, the better approach is to fix the software.
Never use pip as user outside a venv (or pyenv or anaconda, or...)
This installs software "globally" under your user account. It becomes hard to manage, and you don't get isolation between projects.
NEVER use pip as root outside a venv (or ...)
Same problems as above, but bigger. May break your system.
Just because I didn't see anyone mentioning docker:
If you don't have docker installed:
This executes python in the version you specify in the tag. You can pass in any version, that it will work. Way quicker to setup than having to install different versions and has a better isolation from the rest of your OS. If you need to run files inside your docker container, use the
-v
option to mount your filesystem to the containers' filesystem.Of course, the utility of this solution depends highly on your use case. If you want to read more about docker, see its documentation: https://docs.docker.com/