How to Install Zsh on Linux

install zsh on Linux

This tutorial shows how to install zsh and configure 'Oh my zsh' framework on the Linux machine. Z shell (Zsh) is one of the most powerful shell interpreters which is an extended version of bash, tcsh, and ksh. It is a very popular command-line productivity tool for web developer’s workflow.

Some of the prominent features are:

  • Better tab completion
  • Easy directory navigation
  • Supports lots of Themes and Plugins
  • Syntax highlighting
  • Auto completion
  • Interactive configuration
  • Color customization

Install ZSH on Linux

By default, Ubuntu and Debian distros do not have zsh. To install zsh on ubuntu 20.04 and Debian 10, run:

$ sudo apt install zsh

On CentOS 8:

$ sudo yum install zsh

To verify the zsh installation:

$ which zsh
/usr/bin/zsh

From the output you can see, zsh has been successfully installed in /usr/bin/zsh.

Change the current shell

First, check what shell you are currently running on by the following echo command:

$ echo $0
-bash

or

$ echo $SHELL
/bin/bash

The above output shows the currently using shell is 'bash’. To change the default shell, you have to run the following chsh command:

$ chsh -s $(which zsh)

Logout from the current session, now when you log in to terminal you will have Zsh shell instead of default bash.

$ echo $0
-zsh

Note: On CentOS 8 install 'util-linux-user' package to have chsh:

$ sudo dnf install util-linux-user

or

$ sudo yum install util-linux-user

Install 'Oh my zsh' Framework

Oh My Zsh is an open-source framework that runs on top of Zsh. It comes with lots of features, themes, and plugins.

Firstly, you have to install wget and git for downloading the necessary installer tools:

On Ubuntu:

$ sudo apt install git wget

On CentOS:

$ sudo dnf install wget git

Then, download the Oh My Zsh installation script, run:

$ wget https://github.com/robbyrussell/oh-my-zsh/raw/master/tools/install.sh -O - | zsh

Output:

installation of oh my zsh

The installation folder is ‘~/.oh-my-zsh’, run ls command to list its contents:

$ ls ~/.oh-my-zsh 
 CODE_OF_CONDUCT.md   LICENSE.txt  cache	 lib  oh-my-zsh.sh  templates  tools
 CONTRIBUTING.md     README.md	 custom  log  plugins	    themes

Next, you have to create a configuration file for the zsh shell. You can copy the configuration template from ‘~/.oh-my-zsh’ folder to your home directory:

$ cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc
$ source .zshrc

source .zshrc

Now on running ls command, you will notice a new look.

How to Change the theme

By default, Oh-my-zsh uses 'robbyrussell' theme. You can find that by opening the .zshrc file:

.zshrc
# If you come from bash you might have to change your $PATH.
# export PATH=$HOME/bin:/usr/local/bin:$PATH

# Path to your oh-my-zsh installation.
export ZSH=$HOME/.oh-my-zsh

# Set name of the theme to load --- if set to "random", it will
# load a random theme each time oh-my-zsh is loaded, in which case,
# to know which specific one was loaded, run: echo $RANDOM_THEME
# See https://github.com/ohmyzsh/ohmyzsh/wiki/Themes
ZSH_THEME="robbyrussell"

There are many other themes available and in ~/.oh-my-zsh/themes/ directory.

$ ls ~/.oh-my-zsh/themes

list themes

To change the default theme, edit the .zshrc file and change the default theme.

For example to change the theme to 'kiwi' update as shown:

Change to kiwi theme

Then, apply the change by running:

$ source .zshrc

How to Enable plugins

There are many plugins offered by Oh-my-zsh. You can list all of them in ~/.oh-my-zsh/plugins directory.

list Oh My Zsh plugins

In order to enable plugins, edit .zshrc file and add the corresponding plugins to the ‘plugins’ line.

For example:

plugins=(git docker gcloud terraform vagrant)

Configure syntax highlight on Zsh

If you want to enable the syntax highlight on Zsh shell, clone the zsh-syntax-hightlighting from github and move it to plugins folder:

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

Then, insert the following line at the end of the ~/.zshrc file:

$ echo "source ~/.oh-my-zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh” >> ~/.zshrc
$ source ~/.zshrc

Configure autosuggestions on Zsh

While you type a command on zsh shell, it’s useful if the shell supports the autosuggestions mechanism. Fortunately, oh-my-zsh supports this feature very well.

To enable auto-suggestion install zsh-autosuggestions plugin:

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

Then add the plugin to the list of plugins in ~/.zshrc file:

plugins=(git docker gcloud terraform vagrant zsh-autosuggestions)

Now apply the change by running:

$ source ~/.zshrc

Conclusion

In this tutorial, we learned how to install and configure ZSH on Ubuntu 20.04 and CentOS 8 machines.

Fish shell is another good alternative, worth trying for a better terminal experience. Thanks for reading and please leave your suggestion in the below comment section.

Truong Nguyen 7:30 am

Comments

Your email address will not be published. Required fields are marked *