Skip to content

Instantly share code, notes, and snippets.

@AntonFriberg
Last active June 23, 2024 05:24
Show Gist options
  • Save AntonFriberg/e30c8bf64fbd07d3199369db551da5ad to your computer and use it in GitHub Desktop.
Save AntonFriberg/e30c8bf64fbd07d3199369db551da5ad to your computer and use it in GitHub Desktop.
Multiple Python Installations on Linux Using Mise-en-Place (an asdf rust clone)

Multiple Python Installations on Linux Using Mise

Note: Mise was previously called RTX

I have tried a lot different ways of managing multiple Python versions on different Linux systems.

  • pyenv
    • Uses shims which is confusing, especially for new users
  • Compiling from source
    • Takes time and can easily create broken installations with missing features
  • Using docker containers
    • Works but is not IDE friendly or ergonomic for developers
  • Using nix
    • Seems very promising but makes it very difficult to install additional packages with pip

After looking around I have finally found something that I think works very well!

Something called Mise-en-Place Mise

https://github.com/jdxcode/mise

Installation

Follow the installation instructions here https://github.com/jdxcode/mise#installation

For Ubuntu/Debian based systems you can install it using apt for instance.

Make sure that it is installed.

$ mise --version
2023.12.35 linux-x64 (da025fe 2023-06-03)

And make sure that it is hooked up to your shell.

# For bash users
$ echo 'eval "$(mise activate bash)"' >> ~/.bashrc
# For zsh users
$ echo 'eval "$(mise activate zsh)"' >> ~/.zshrc
# For fish users
$ echo 'mise activate fish | source' >> ~/.config/fish/config.fish

Using Mise

With Mise you can then manage your Python installations. It will download and compile the versions from source. This can be tricky since it requires that you have the correct build environment in place.

I like to use the one that pyenv suggests. https://github.com/pyenv/pyenv/wiki#suggested-build-environment

$ sudo apt update; sudo apt install build-essential libssl-dev zlib1g-dev \
  libbz2-dev libreadline-dev libsqlite3-dev curl \
  libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev

To change the global default Python installation (the one active on your user and shell).

$ mise use --global python@3.11.4
# Downloads, compiles and activates the Python installation
# Takes around 50 seconds on my machine
Downloading Python-3.11.4.tar.xz...
-> https://www.python.org/ftp/python/3.11.4/Python-3.11.4.tar.xz
Installing Python-3.11.4...
mise python@3.11.4 running python-build
$ python --version
3.11.4

Project Specific Python Version

In some cases it is nice to have a up to date global Python version active like above. But, when working in a specific project with older code for instance you might need an older Python version. In that case you can create a .mise.toml file in the project folder to have it automatically activate a perticular version.

$ python --version
Python 3.11.4
$ cat my_project/.mise.toml
[tools]
python = {version='3.10.12', virtualenv='.venv'}

$ cd my_project
[WARN] Tool not installed: python@3.10.12
$ mise local --install-missing
$ python --version
Python 3.10.12
$ which python
/home/antonfr/my_project/.venv/bin/python
$ cd ..
$ python --version
Python 3.11.4

Note that with the .mise.toml file above we also specified a virtualenv to create. So not only does Mise switch the python version automatically when navigating into the folder, it also activates the virtualenv for you. Very neat stuff!

Installing Poetry with Mise

Mise can even help you install and manage the [poetry] version you have installed. Although, poetry management is not directly available from Mise out of the box so you have to install a plugin first.

$ mise plugin install poetry
$ mise use --global poetry@1.6.1
$ poetry --version
Poetry (version 1.6.1)

It also works with project specific versions.

$ poetry --version
Poetry (version 1.6.1)
$ cat my_project/.mise.toml
[tools]
poetry = {version='1.5.1', pyproject='pyproject.toml'}
python = {version='3.10.12', virtualenv='.venv'}
$ cd my_project
[WARN] Tool not installed: poetry@1.5.1
$ mise install
$ poetry --version
Poetry (version 1.5.1)

NOTE: poetry should be placed above python in the .mise.toml file

Uninstall

If something goes wrong or you simply want to uninstall Mise and all of the installed environments you can follow the official documentation. https://github.com/jdxcode/mise#uninstalling

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment