Rye Global Guide

Using Rye Globally and Simply
Nov 01, 2024

Rye (https://rye.astral.sh) is a Python project and package management solution built in Rust. It is fast, fully featured, and well organized.

Having struggled a bit to install a user-based version of python with Rye, we found that the best way to use Rye globally (per user account), is to go with Rye’s nature. We explore how to setup a global rye setup using the standard Rye project paradigm.

Note

The methodology we outline here sidesteps Rye’s global shims (https://rye.astral.sh/guide/shims/), which uses a .python-version configuration file. The reason we do this is that global shims don’t have a natural way to add libraries in the true global sense.

In addition, Rye also has a concept of global tools (https://rye.astral.sh/guide/tools/) that are installed outside of the project realm.

We sidestep both these mechanisms here.

Our solution is to install a project via Rye, call it global, populate it will whatever tools and libs you need, then use that global project/environment to run your many python dependent scripts for your entire user account.

The reasons why we think this method is effective:

  1. Consistency: You use Rye as you normally would for any other project

  2. It is easy to understand and use

  3. Less entropy compared to using global configuration outside a project

  4. It doesn’t interfere with any other Rye projects

Here is the outline of how it works:

  • Install Rye as usual

  • Inside the Rye base, ~/.rye/, create a project/folder named global

  • Activate that environment using ~/.rye/.venv/bin/activate

  • When in that project, add all tools that you need for your global environment using Rye

See https://github.com/astral-sh/rye/discussions/998#discussioncomment-10385800 for background motivation.

Install Steps

  • install rye:

    curl -sSf https://rye.astral.sh/get | bash
    
  • Make rye active

    source "$HOME/.rye/env"
    
  • Create a global rye environment:

    cd ~/.rye; rye init global
    
  • Sync that new global:

    cd ~/.rye/global ; rye sync
    
  • Initialize that global env:

    . ~/.rye/global/.venv/bin/activate
    
  • Add dependencies etc… :

    cd ~/.rye/global       # We repeat this just in case you forgot
    rye add tabulate yfinance colored termcolor
    

Initialize at Login

Once you have all this in place, you probably want to know how to initialize all this at login:

  • Activate the global env

    . ~/.rye/global/.venv/bin/activate
    
  • Optionally activate Rye: You don’t need it just to run the global environment, but you need it to “add” libs/programs in global

    source "$HOME/.rye/env"   # Exposes the *rye* command
    cd ~/.rye/global          # You need to be here to add stuff.
    rye add Sphinx ansible    # Example: adds deps to pyproject.toml and installs em
    

Automating the Environment at Login

You can automate this global Python environment at login in many ways. One way to do this is to create a small script in ~/bin and execute that at login:

  • Here is a short env script, ~/bin/rye.sh to do all the activation:

    # File: ~/bin/rye.sh
    . "$HOME/.rye/env"
    . ~/.rye/global/.venv/bin/activate
    
  • Then in your .bashrc:

    # .bashrc
    if [[ -f ~/bin/rye.sh ]]; then
       source ~/bin/rye.sh
    fi