How to Install Zsh on Ubuntu

July 5, 2023

Introduction

The Z Shell (Zsh) is an interactive Unix shell that can be used as a command interpreter for shell scripting. It is an extended Bourne shell with numerous improvements, including some Bash, ksh, and tcsh features.

Some of the benefits Zsh offers are command-line completion, shared history, improved globbing, intelligent tab completion, spelling correction, and a rich set of plugins and themes.

In this tutorial, you will learn to install and configure the Z Shell on Ubuntu.

How to install Zsh on Ubuntu - a tutorial.

Prerequisites

  • A system running Ubuntu.
  • An account with root privileges.
  • Access to the terminal (Ctrl + Alt + T).

Install Zsh on Ubuntu

The default shell on Ubuntu is Bash. Follow the steps below to install and use Z Shell on Ubuntu:

Step 1: Update the System Repository

Update the system package repository to get the latest program version available. Open the terminal and run the following command:

sudo apt update

Enter the administrator password when prompted and press Enter. Wait for the process to complete.

Updating the package repository ensures that your system has the most up-to-date information about software packages and their dependencies during software installation.

Step 2: Install Zsh

Run the following command to install Z Shell on Ubuntu:

sudo apt install zsh -y

The -y flag automatically answers yes to any prompts during the installation and provides consent for installing Zsh and its dependencies. Omit the flag if you want to review the installation.

Installing Zsh on Ubuntu.

Step 3: Check Installation

After the installation finishes, check if it has been installed correctly by checking the program version. Run the following command:

zsh --version
Checking Zsh version in Ubuntu.

The output shows the current version installed on the system.

Configure Zsh on Ubuntu

Configuring Zsh in Ubuntu allows you to personalize the shell environment, boost productivity with intelligent tab completion, and leverage the flexibility of Zsh scripting capabilities.

This section explores the steps to configure Zsh as your default shell in Ubuntu and introduces you to various customization options.

Initial Configuration

Unlike Bash and other shells, Zsh requires initial configuration when you start it for the first time. To start Zsh, type the shell name in the terminal and press Enter:

zsh

The following screenshot shows the configuration options that Zsh offers on the first startup:

Initial Zsh configuration in Ubuntu.
  • Press 1 to go to the main menu and configure each setting individually.
  • Press 2 to populate the .zshrc configuration file with the default parameters, which you can later change manually in the .zshrc file.
  • Press 0 to create an empty .zshrc configuration file and configure everything from scratch.
  • Press q to exit the configuration and start it again the next time you enter Zsh.

After you complete the setup, the changes are saved, and the Z Shell prompt appears. The initial setup appears only the first time you run the shell, but you can rerun the initial setup with the zsh-newuser-install command.

Set Zsh as Default Shell

After installation, Zsh is not configured and is not set as the default shell. To set Zsh as your default shell, follow the steps below:

1. Check which shell is the default one in your system:

echo $SHELL
Checking the default shell in Linux.

The output states the value of the $SHELL variable, which is the default shell.

2. Use the chsh (change shell) command to change the default login shell. The following syntax lets you change the default shell with chsh:

chsh -s [path] [user]
  • [path] specifies the path to the shell you want to use.
  • [user] specifies the user for which you want to change the default shell. Not specifying the user changes the default shell for the current user.

If you are unsure of the shell path, utilize the which command to specify the Zsh path automatically. For example:

chsh -s $(which zsh)

Enter the root password when prompted and press Enter.

Entering the root password for the Zsh path.

The command changes the default shell to Zsh for the current user. To start using the Z Shell, log out of the terminal and log back in.

Install Oh My Zsh

Oh My Zsh is an open-source framework that adds features and more functionality to the Z Shell. It is designed to enhance the command-line experience and make it more enjoyable and productive. Oh My Zsh provides a collection of themes, plugins, and scripts that can be easily installed and managed.

Note: A Git installation on your system is necessary for Oh My Zsh. See how to install Git on Ubuntu.

Install Oh My Zsh by running the following command:

sh -c "$(wget https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh -O -)"
Installing Oh My Zsh to customize Z Shell in Linux.

After the installation completes, the installer prompts you to set Zsh as your default shell. Type y if you want to set Zsh as the default shell or n if you want to keep the default one.

After the framework is installed, you can start customizing your prompt.

Add a Custom Theme

After you install Oh My Zsh, there is a selection of over 150 themes available for the terminal. You can see what each theme looks like and their names on Oh My Zsh GitHub theme page. Once you find the theme you want to use, follow the steps below to add it to your terminal:

1. open and edit the .zshrc configuration file using a text editor. For example, we will use nano:

nano ~/.zshrc

Note: Learn about the .bashrc configuration file in Bash, which serves the same purpose as the .zshrc file in Zsh.

2. Locate the following line:

ZSH_THEME="robbyrussell"
Changing the Z Shell theme.

The line defines which theme the terminal uses. The default one is robbyrussell. To use a different theme, change the value to match the name of the theme you want. For example:

ZSH_THEME="jonathan"

3. Save the file and restart the terminal to load the theme:

Using a custom theme in Zsh.

The terminal now uses the specified theme.

Enable Auto-Suggestions

Enable the auto-suggestions plugin to get command suggestions based on your Zsh command history. Since we use some commands daily, getting auto-suggestions saves time and helps you complete your daily tasks.

Note: See how to use the Linux history command.

Follow the steps below to enable auto-suggestions:

1. Add the auto-suggestion plugin repository first. Run the following command to clone the repository:

git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
Cloning the auto-suggestions plugin repository for Z shell in Ubuntu.

2. Open the .zshrc configuration file using a text editor:

nano ~/.zshrc

3. Scroll down to the plugins section of the file and activate the auto-suggestion plugin by adding it to the plugins, as shown below:

plugins=(git zsh-autosuggestions)
Adding the autosuggestions plugin to Zsh.

4. Save the changes and exit the text editor.

5. Restart the terminal and start Zsh to enable the auto-suggestions plugin.

When you start typing a command, use the right arrow key to accept the suggestion and automatically complete the command:

Testing the autosuggestions plugin in Ubuntu.

Enable Syntax Highlighting

Syntax highlighting provides numerous benefits, including improved code readability, error detection, language recognition, productivity, and aesthetics.

Follow the steps below to add syntax highlighting to Zsh:

1. Clone the plugin repository by running the following command:

git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
Cloning the syntax highlighting plugin repository in Ubuntu.

2. Open the .zshrc file:

nano ~/.zshrc

3. Scroll down to the plugins section and add zsh-syntax-highlighting to the list of plugins:

Adding the syntax highlighting plugin to the Z shell configuration file.

4. Save the changes and exit the file.

5. Restart the terminal and start Zsh to enable syntax highlighting. For example, typing a command with a typo makes it red, while typing a command correctly highlights it green:

Using syntax highlighting in Zsh.

In the example above, we show how highlighting works using the echo command.

Uninstall Zsh

If you decide to remove Zsh from your system and revert to the old shell, follow the steps below to uninstall it:

1. Purge the zsh package from the system by running the following command:

sudo apt --purge remove zsh

When prompted, type y and press Enter to confirm you want to remove Zsh.

Removing Zsh from Ubuntu.

Purging a package removes the package and deletes any associated configuration files and dependencies automatically installed with the package.

2. Change the default shell back to Bash (or any other you want):

chsh -s $(which bash)

3. Close the terminal and open a new session for the changes to take effect.

Conclusion

This article showed how to install and configure the Zsh shell on Ubuntu. While Bash is still widely used and remains the default shell on many Linux distributions, Zsh offers numerous advantages and is becoming increasingly popular.

Next, learn about 8 shell types in Linux.

Was this article helpful?
YesNo
Bosko Marijan
Having worked as an educator and content writer, combined with his lifelong passion for all things high-tech, Bosko strives to simplify intricate concepts and make them user-friendly. That has led him to technical writing at PhoenixNAP, where he continues his mission of spreading knowledge.
Next you should read
How To Customize Bash Prompt in Linux
May 12, 2020

Follow this article to learn how to make changes to your BASH prompt. The guide shows how to edit the bashrc file, as well as how to modify PS1 variables. Use the information in...
Read more
bashrc vs. bash_profile: What Is the Difference?
September 22, 2022

The Bash shell holds custom information in hidden files such as .bashrc and .bash_profile. But what's the difference between the two? Find out when to use which file in this...
Read more
What Is the .bashrc File in Linux?
January 23, 2023

The .bashrc file holds all the configuration for the Bash shell. Learn how to configure the Bash shell using functions and aliases and create custom colors.
Read more
bashrc vs. bash_profile: What Is the Difference?
September 22, 2022

The Bash shell holds custom information in hidden files such as .bashrc and .bash_profile. But what's the difference between the two? Find out when to use which file in this guide.
Read more