Python: Easily Manage Multiple Versions on Ubuntu

Python: Easily Manage Multiple Versions on Ubuntu

Master the art of managing multiple Python versions on Ubuntu with easy tools and techniques to improve productivity.

Never delete or replace the default Python version installed in Ubuntu- and if there’s no special need; just make use of the default version.

Out of the box, ubuntu comes with a lower release of python pre-installed, this is in order to have a stable version to fall back to, avoiding breaking dependencies.

But most times, you may want to use newer versions of python. It is possible to have multiple versions of python on your ubuntu. Install the particular python version as shown here.

💡
Note: anytime you want to exit python console in the terminal, just press “Ctrl + D” or type “exit()” and hit enter.
python3

OR

python3 --version

If you have installed your desired python version, and ran any of the commands shown above, you may notice that the default version is been shown any time you check for the version or try to launch a python console in the terminal.

But if you check the specific version as shown below, you noticed that it was installed successfully. In my case, I installed python3.12 along with the default python3.10.

python3.12 --version

Or launch the console with the command below.

python3.12

As well, you can list out all the python versions install in the bin directory.

ls -l /usr/bin/python*

The command will give an output like this:

lrwxrwxrwx 1 root root      25 Nov 18 16:32 /usr/bin/python3 →python3.10

-rwxr-xr-x 1 root root 5909000 Sep 11 16:47 /usr/bin/python3.10
-rwxr-xr-x 1 root root 7894408 Oct  1 09:52 /usr/bin/python3.12
-rwxr-xr-x 1 root root     960 Jan 25  2023 /usr/bin/python3-futurize
-rwxr-xr-x 1 root root     964 Jan 25  2023 /usr/bin/python3-pasteurize

Let’s hit the nail on the head!

After several failed attempts to change from the default python version to a specificied one, I found out safer ways to manage multiple versions of python on my Ubuntu without messing up with dependencies again.

Let’s start from simple to complex:

1. Using Venv

  • Install virtual environment package known as venv:

      sudo apt install python3-venv
    

    The above will install the venv package for thre default python version that comes pre-installed with ubuntu. To install venv for a specific version of python, specify (python3.8-venv, python3.11-venv, python3.13-venv) in the command as shown below.

      sudo apt install python3.12-venv
    
  • Create a virtual environment for your project:

    Now, when you want to use this specific version for a particular project, you will just have to create or navigate to the project directory and run this command.

      python3.12 -m venv myenv
    

    It will create a virtual environment call “myenv”.

  • Activate the virtual environment:

      source myenv/bin/activate
    
  • Install modules for your virtual envirenment:

      python3.12 -m ensurepip --upgrade
      python3.12 -m pip install --upgrade setuptools
      python3.12 -m pip install <module_name>
    

2. Using Pyenv

Pyenv is a popular tool used to manage multiple versions of Python on a single system. Unlike the simple venv which is part of core python, pyenv has some features that makes it stands out:

  • Multiple Python Versions: Install and switch between multiple versions of Python, including standard releases, pre-releases, and even development versions.

  • Project-Specific Python Versions: Set a specific Python version for a project using a .python-version file, ensuring that your project always uses the correct version.

  • Global and Local Versions: You can set a global Python version (default for all shells) and local versions (specific to a directory or project).

  • Easy Installation: Install various Python versions with a simple command, without interfering with the system Python.

  • Isolation: Works well with virtual environments, allowing you to create isolated environments for your projects.

You can follow the steps below to set a project with pyenv:

  1. Install pyenv:

     curl https://pyenv.run | bash
    

    If you encountered errors, it means you need to install dependencies as shown below.

    Essential: curl, make, build-essential, libssl-dev, zlib1g-dev, libbz2-dev, libreadline-dev, libsqlite3-dev, libffi-dev.

    You can install the essential ones first and add any optional ones later if you encounter issues with specific Python packages; or just install all for future uses.

     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
    

    Check for type of shell you’re using:

     echo $SHELL
     ps -p $$
     echo $0
    

    Then add pyenv to your shell’s configuration file (like .bashrc or .zshrc). See the commands corresponding to your type of shell bellow.

    Bash shell:

     echo -e 'export PYENV_ROOT="$HOME/.pyenv"\nexport PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
    
     echo -e 'eval "$(pyenv init --path)"\neval "$(pyenv init -)"' >> ~/.bashrc
    

    Zsh shell:

     echo -e 'export PYENV_ROOT="$HOME/.pyenv"\nexport PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
    
     echo -e 'eval "$(pyenv init --path)"\neval "$(pyenv init -)"' >> ~/.zshrc
    

    After appending to your particular shell, refresh it to activate the added instructions.

     exec $0
    
  2. Check if pyenv is installed correctly:

     pyenv --version
    
  3. Check installable versions of python available:

     pyenv install --list
    
  4. Install the python desired version:

     pyenv install 3.13
    
  5. Set as global or local version:

     pyenv global 3.13
    

    Or navigate to your desired project directory and run the command below.

     pyenv local 3.13
    
  6. Test pyenv:

    You can create a test script and try to execute it in the terminal with the commands below.

     python test_program.py
    
  7. See the versions of python you have installed:

     pyenv versions
    
  8. Check the version of python currently active:

     python --version
    

Let’s wrap it here:

Using env is pretty simple; but if you’re setting up your machine and want to do it once and have a rest for a long time, I would personally urge you to go with pyenv!