Zsh and Oh-My-Zsh Installation Guide

Install and configure Zsh with Oh-My-Zsh on Ubuntu 22.04

In this guide, we will install Zsh along with its configuration manager Oh-My-Zsh on an Ubuntu 22.04 machine.

This article is based on Getting started with Zsh and Install Zsh, Oh-My-Zsh, Fonts-Powerline & Plugins.

Before getting started with Zsh and Oh-My-Zsh, we should think about installing a custom terminal. For instance, we can install terminator, a terminal emulator that provides easy management of multiple terminals.

sudo apt-get install terminator

Install Zsh

We can directly install Zsh through the apt package manager:

sudo apt-get install zsh

After the installation succeeds, we can launch Zsh for the first time through the terminal which redirects us to a first-time configuration where we have to choose from three options.

zsh
You can:

(q)  Quit and do nothing.  The function will be run again next time.

(0)  Exit, creating the file ~/.zshrc containing just a comment.
     That will prevent this function from being run again.

(1)  Continue to the main menu.

(2)  Populate your ~/.zshrc with the configuration recommended
     by the system administrator and exit (you will need to edit
     the file by hand, if so desired).

By selecting option 1, we proceed to the main menu with the following four configuration categories:

  1. Configure the history (e.g., how many commands are retained in the shell history)
  2. Configure the completion system (e.g., enable command completion)
  3. Configure key bindings (e.g., choose between Emacs or Vi key bindings)
  4. Enable/Disable Zsh features

Note that we can easily change all configurations and options later by editing the ~/.zshrc file.

Once we completed the configuration, we type 0 to save the changes and exit the first-time configuration.

Next, we make Zsh to our default shell:

chsh -s $(which zsh)

Thereby, Zsh will start automatically when we open a terminal window.

Install Oh-My-Zsh

Before we can install Oh-My-Zsh, we have to make sure that git and curl are installed on our system or install it otherwise. Therefore, we simply try to install them through the apt package manager, which outputs that it is already installed in case it is already on our system.

sudo apt-get install git curl

Next, we can download the installation script for Oh-My-Zsh from GitHub:

curl -Lo install.sh https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh

This command downloads the file install.sh into the current directory, which we can inspect less install.sh and eventually we execute it to start the installation:

sh install.sh

Congratulations, we have now installed Oh-My-Zsh.

The configuration of Oh-My-Zsh is added to the configuration file of Zsh ~/.zshrc by default. First, we change the theme of Oh-My-Zsh by editing the configuration file nano ~/.zshrc and replacing the default configuration ZSH_THEME="robbyrussell" to ZSH_THEME="agnoster". After changing the theme, we can open a new terminal window to see if the appearance of our terminal changed. If we do not have installed Fonts Powerline on our system, the terminal prompt will not be shown correctly. In that case, we have to install the Fonts Powerline font package:

sudo apt-get install fonts-powerline

Note that we might need to restart the system if the prompt is still shown incorrectly.

Install Plugins

Now that we have installed and configured Zsh and Oh-My-Zsh, we can install some useful plugins.

First, we will install the plugin zsh-autosuggestions to enable command completion based on our command history. To do that, we have to clone the respective git repository into the plugin folder of our Oh-My-Zsh installation:

cd ~/.oh-my-zsh/custom/plugins
git clone https://github.com/zsh-users/zsh-autosuggestions

To enable the downloaded plugin, we edit the configuration file of Zsh nano ~/.zshrc and add the plugin to the plugin configuration by changing the line plugins=(git) to plugins=(git zsh-autosuggestions). When opening a new shell (or executing source ~/.zshrc), the autosuggestion plugin should be activated.

Next, we will install the plugin zsh-syntax-highlighting which verifies the correctness of commands. We perform the installation in the very same way as before, cloning the git repository into the plugins folder:

cd ~/.oh-my-zsh/custom/plugins
git clone https://github.com/zsh-users/zsh-syntax-highlighting

Eventually, we add the plugin to our configuration (nano ~/.zshrc) by changing the line plugins=(git zsh-autosuggestions) to plugins=(git zsh-autosuggestions zsh-syntax-highlighting). Just like before, the syntax highlighting plugin gets activated when opening a new shell or by executing source ~/.zshrc.




Enjoy Reading This Article?

Here are some more articles you might like to read next:

  • Python Virtual Environment
  • Tensorflow Tutorial: Load Custom Image Dataset
  • GitHub SSH Key
  • Tensorflow Federated Tutorial