Installing Powerline in a Python virtual environment

Learn how to install Powerline in a Python virtual environment. Installing on a virtual environment allows you to keep your system Python free of user packages.

Powerline is a text-based tool that provides useful information in a variety of contexts. The following demo shows Powerline displaying information about a Git repository:

Note: It’s relatively easier to install Powerline to use the system Python by using APT packages. For this alternative, check our guide to install Powerline on Ubuntu.

Manually install Powerline fonts

To install the Powerline fonts:

  1. Install git:
    sudo apt update
    sudo apt install --yes git
    
  2. Clone the Powerline fonts repository to the fonts folder in your computer:
    git clone https://github.com/powerline/fonts.git --depth=1 fonts
    
  3. Run the installation script:
    ./fonts/install.sh
    
  4. Optionally, you can remove your copy of the fonts repository. This guide doesn’t need it anymore:
    rm -rf fonts/
    

To configure the fonts in the Ubuntu Terminal app:

  1. Open Terminal.
  2. Go to Edit > Preferences.
  3. In the Preferences - Profile dialog, choose the Text tab, enable Custom font, and click the button right next to it to select a font.
  4. From the Choose a Terminal Font dialog, select one of the Powerline fonts, such as Source Code Pro for Powerline Regular.

Install Powerline in a virtual environment

  1. Create a Python virtual environment in the ~/.venv folder:
    python3 -m venv ~/.venv
    
  2. Activate the virtual environment:
    source ~/.venv/bin/activate
    
  3. Install the wheel package:
    pip install wheel
    
  4. Install Powerline:
    pip install powerline-status
    

Configure Bash

To configure Powerline for bash, add the following lines to your $HOME/.bashrc file:

# Powerline configuration
if [ -f $HOME/.venv/lib/python3.6/site-packages/powerline/bindings/bash/powerline.sh ]; then
  powerline-daemon -q
  POWERLINE_BASH_CONTINUATION=1
  POWERLINE_BASH_SELECT=1
  source $HOME/.venv/lib/python3.6/site-packages/powerline/bindings/bash/powerline.sh
fi

To apply the changes to your current terminal:

source ~/.bashrc

After running the previous command or restarting your terminal, the Powerline segments appear in your prompt.

Configure Vim

To use Powerline, Vim requires support for Python 3. You can check the features included in your installation with the following command:

vim --version

Look for the python3 entry, which must have a plus sign (+) next to it to indicate that the feature is available. If your version of vim doesn’t include support for Python 3, you can install the latest vim package, which at the time of this writing includes support for Python 3:

sudo apt update
sudo apt install --yes vim

To configure Powerline for Vim, add the following lines to your $HOME/.vimrc file:

set rtp+=$HOME/.venv/lib/python3.6/site-packages/powerline/bindings/vim/

set laststatus=2

The laststatus setting displays the status bar in Vim and makes Powerline visible by default.

Configure tmux

To configure Powerline in tmux, add the following to your ~/.tmux.conf file:

set -g default-terminal "screen-256color"
source "$HOME/.venv/lib/python3.6/site-packages/powerline/bindings/tmux/powerline.conf"

You must set the correct value for your terminal using the default-terminal setting. If you don’t configure the correct value for your terminal, you might run into some issues. For example, Vim inside of tmux doesn’t show colored Powerline segments. The screen-256color or xterm-256color values work fine in our environments.

Edit your Powerline configuration

You can customize what segments appear in Powerline, or even the behavior of specific segments. Follow these instructions to customize your Powerline installation:

  1. Copy the $HOME/.venv/lib/python3.6/site-packages/powerline/config_files/ folder to $HOME/.config/powerline:
    mkdir -p $HOME/.config/powerline
    cp -R $HOME/.venv/lib/python3.6/site-packages/powerline/config_files/* \
          $HOME/.config/powerline/
    
  2. Edit the files there according to your needs. A good starting point is the $HOME/.config/powerline/config.json file.
  3. Reload the Powerline daemon:
    powerline-daemon --replace
    

The following example shows how to configure Powerline on the shell to behave as the demo shown in the introduction of this guide:

  1. To show the vcs segment—which displays information about git repositories—on the left side of the shell, replace the default theme with the default_leftonly theme in the $HOME/.config/powerline/config.json file:
    ···
    "shell": {
      "colorscheme": "default",
      "theme": "default_leftonly",
      "local_themes": {
        "continuation": "continuation",
        "select": "select"
      }
    },
    ···
    
  2. To make the vcs segment display in a different color when the git repository is dirty, add the status_colors attribute to the vcs segment in the $HOME/.config/powerline/themes/shell/default_leftonly.json file:
    ···
    {
      "function": "powerline.segments.common.vcs.branch",
      "priority": 40,
      "args": { "status_colors": true }
    }
    ···
    

You can also create custom Powerline segments that display information tailored to your workflow. For more information, check Creating a custom Powerline segment.