How to Install Zsh on Linux

Featured image for tutorial "How to Install Zsh on Linux"

zsh (Z Shell) is a very popular and highly customizable shell for Linux and Unix-like operating systems.

It has many powerful features, and can be extended with many great plugins and themes developed by a very active community.

In this tutorial we’ll explain how to install zsh on Debian-based (Ubuntu, Debian, Linux Mint, etc) on RHEL-based (CentOS, Fedora, Rocky Linux, etc) distros, set it as the default shell, and go through the initial configuration.

As we mentioned, zsh has a lot of configurations. We’ll just go through the initial setup.

For an easy way to manage zsh extensions you can use Oh My Zsh, which is a framework that works out of the box, and comes with over 150 preinstalled themes and over 300 preinstalled plugins.

example zsh with agnoster theme
Example of how Zsh can look like. I’m using the Agnoster theme.

Install Zsh

Install Zsh on Ubuntu & Debian-based Distros

In this example we’ll be using Ubuntu 20.04. To install zsh run the following command:

sudo apt install zsh

We can easily check if zsh has been successfully installed by checking the installed version:

zsh --version
Output
zsh 5.8 (x86_64-ubuntu-linux-gnu)

Install Zsh on CentOS & RHEL-based Distros

In this example we’ll be using Rocky Linux 8.5. To install zsh run the following command:

sudo dnf install zsh

We can easily check if zsh has been successfully installed by checking the installed version:

zsh --version
Output
zsh 5.5.1 (x86_64-redhat-linux-gnu)

Set Zsh as Default Shell

Just installing zsh on our system won’t also make it the default shell.

We can set it as the default shell with the chsh (**ch**ange **sh**ell) command with the -s option. We’ll also need to supply the chsh -s command with the path to our new shell.

You can check the path to the current shell by running the following command:

echo $SHELL
Output
/bin/bash

We can find the path to zsh with the which command, by running:

which zsh
Output
/usr/bin/zsh

If we know the path to the zsh executable, we can pass it to cshs -s:

chsh -s /usr/bin/zsh

Alternatively you can pass the output of the which command in a variable. The result should be the same.

chsh -s $(which zsh)

The command won’t return any output.

For the changes to take effect, log out of the current user and log back in.

chsh : command not found

On RHEL-based distros (CentOS, Fedora, Rocky Linux, etc) you may get an error chsh : command not found when trying to change the shell.

To fix this install the util-linux-user package:

sudo dnf install util-linux-user

Now you should be able to change the shell using the chsh command.

Initial Zsh Configuration

When you open the terminal you should be greeted with some options to configure zsh.

This is the Z Shell configuration function for new users,
zsh-newuser-install.
You are seeing this message because you have no zsh startup files
(the files .zshenv, .zprofile, .zshrc, .zlogin in the directory
~).  This function can help you with a few settings that should
make your use of the shell easier.

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 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).

--- Type one of the keys in parentheses ---

zsh requires some initial configuration. You will be also able to configure zsh any time you want, later on.

For now, you can press the corresponding number, on your keyboard, to navigate through the menus and options.

Press 1 to go to the main menu where you’ll see some basic options that you can configure.

Please pick one of the following options:

(1)  Configure settings for history, i.e. command lines remembered
     and saved by the shell.  (Recommended.)

(2)  Use the new completion system.  (Recommended.)

(3)  Configure how keys behave when editing command lines.  (Recommended.)

(4)  Pick some of the more common shell options.  These are simple "on"
     or "off" switches controlling the shell's features.  

(0)  Exit, leaving the existing ~/.zshrc alone.

(a)  Abort all settings and start from scratch.  Note this will overwrite
     any settings from zsh-newuser-install already in the startup file.
     It will not alter any of your other settings, however.

(q)  Quit and do nothing else.
--- Type one of the keys in parentheses ---

You can press the corresponding numbers for the settings marked **Recommended** and you’ll be guided to how to configure them.

When you are done, navigate back to this menu and press 0, which will allow you to save changes after make your configurations:

(0)  Exit, saving the new settings.  They will take effect immediately.

zsh is very configurable, and you can also find many plugins and themes for it, which we won’t cover in this article. However you should be able to find information on how to configure it online, as well as many great plugins to enhance functionality.

This configuration screen is meant for initial configuration. To make other configurations to the zsh interactive shell you can edit the ~/.zshrc file.

nano ~/.zshrc
Contents of ~/.zshrc
# Lines configured by zsh-newuser-install
HISTFILE=~/.histfile
HISTSIZE=1000
SAVEHIST=1000
bindkey -e
# End of lines configured by zsh-newuser-install
# The following lines were added by compinstall
zstyle :compinstall filename '/home/edxd/.zshrc'

autoload -Uz compinit
compinit
# End of lines added by compinstall

You should see the changes you made during the initial configuration screen reflected in the ~/.zshrc file.

Although it’s recommended you continue configuring zsh in this file, you can also return to the initial configuration screen by running the following commands:

autoload -U zsh-newuser-install
zsh-newuser-install -f

Change Shell Back

If you want to change your shell back to Bash, or any other shell you had, then use the chsh -s [path_to_shell] command.

This command will change my shell back to Bash.

chsh -s $(which bash)

Instead of passing the output of the which bash command to chsh -s, you can also specify the path to the shell:

chsh -s /bin/bash

Uninstall Zsh

To uninstall zsh on Debian-based distros run:

sudo apt --purge remove zsh

To uninstall zsh on RHEL-based distros run:

sudo dnf --remove zsh

Conclusion

In this tutorial we explained how to install zsh on Linux, set it as the default shell, go through the initial configuration, revert back to our old shell and uninstall zsh.

If you encountered any issues or have any questions feel free to leave a comment and we’ll get back to you as soon as we can.

0 Shares:
Subscribe
Notify of
guest
Receive notifications when your comment receives a reply. (Optional)
Your username will link to your website. (Optional)

0 Comments
Inline Feedbacks
View all comments
You May Also Like
How to Install VirtualBox on Debian
Read More

How to Install VirtualBox on Debian

[powerkit_collapsible title=”Not using Debian? Choose a different version or distro.”] Ubuntu [/powerkit_collapsible] Oracle VM VirtualBox is an open-source,…