Linux

How to Install Zsh on Ubuntu

Introduction

The Z Shell (Zsh) is an interactive Unix shell that serves as a command interpreter for shell scripting. It extends the features of the Bourne shell, incorporating enhancements from Bash, ksh, and tcsh.

Zsh offers several benefits, including advanced command-line completion, shared history, enhanced globbing, intelligent tab completion, spelling correction, and a wide array of plugins and themes.

In this tutorial, you’ll learn how to install and configure Zsh on Ubuntu.

Prerequisites

A system running Ubuntu
An account with root privileges
Access to the terminal (use Ctrl + Alt + T)
Installing Zsh on Ubuntu
The default shell in Ubuntu is Bash. Follow these steps to install and set up Zsh:

Step 1: Update the System Repository
To ensure you have the latest versions of available software, update the package repository. Open the terminal and run:

sudo apt update

Enter your administrator password when prompted and press Enter. This step ensures your system has the latest information about software packages.

Step 2: Install Zsh

To install Zsh, execute the following command:

sudo apt install zsh -y

The -y flag automatically confirms the installation and dependencies. You can omit this flag if you’d like to review the installation process.

Step 3: Verify Installation

After installation, check that Zsh has been correctly installed by running:

zsh –version

This command will display the installed version of Zsh.

Configuring Zsh on Ubuntu
Configuring Zsh allows you to customize your shell environment, enhancing productivity with features like intelligent tab completion and Zsh’s flexible scripting capabilities.

Initial Configuration
When you run Zsh for the first time, it requires initial configuration. To start Zsh, simply type:

zsh

You’ll see several configuration options:

Press 1 to access the main menu for individual settings.
Press 2 to fill the .zshrc file with default parameters, which you can modify later.
Press 0 to create an empty .zshrc file and configure everything from scratch.
Press q to exit the configuration.
After completing the setup, your changes will be saved, and you’ll see the Z Shell prompt. You can rerun the initial setup using the command zsh-newuser-install.

Setting Zsh as the Default Shell

By default, Zsh is not set as the default shell. To change this, follow these steps:

Check your current default shell:

echo $SHELL

Use the chsh (change shell) command to set Zsh as your default shell:

chsh -s $(which zsh)

Enter your password when prompted. Log out and log back in to start using Zsh.

Installing Oh My Zsh
Oh My Zsh is a popular open-source framework that enhances Zsh by adding various features, themes, and plugins. Ensure you have Git installed on your system, then run:

sh -c “$(wget https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh -O -)”

After installation, you will be prompted to set Zsh as your default shell. Type y to confirm or n to keep the default shell.

Customizing Your Zsh Environment
Adding a Custom Theme
After installing Oh My Zsh, you can choose from over 150 themes. To change your theme:

Open and edit the .zshrc file:

nano ~/.zshrc

Find the line:

ZSH_THEME=”robbyrussell”

Change “robbyrussell” to your desired theme name, for example:

ZSH_THEME=”jonathan”

Save the file and restart the terminal to apply the changes.

Enabling Auto-Suggestions

The auto-suggestions plugin provides command suggestions based on your history, saving you time. To enable it:

Clone the auto-suggestions repository:

git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
Open the .zshrc file:

nano ~/.zshrc

Add zsh-autosuggestions to the plugins list:

plugins=(git zsh-autosuggestions)

Save changes and restart the terminal.

Enabling Syntax Highlighting

Syntax highlighting enhances code readability and error detection. To add it:

Clone the syntax highlighting repository:

git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
Edit the .zshrc file:

nano ~/.zshrc

Add zsh-syntax-highlighting to the plugins list.

Save changes and restart the terminal.

Uninstalling Zsh

If you wish to remove Zsh and revert to Bash, follow these steps:

Purge the Zsh package:

sudo apt –purge remove zsh

Confirm by typing y.

Change your default shell back to Bash:

chsh -s $(which bash)

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

Conclusion

This tutorial provided a comprehensive guide on installing and configuring the Zsh shell on Ubuntu. While Bash remains a widely used default shell, Zsh offers numerous enhancements that are increasingly popular among users. With its features and customization options, Zsh can significantly improve your command-line experience.

Related Articles

Leave a Reply

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

Check Also
Close
Back to top button