How to Install Oh My Zsh on Ubuntu 22.04 (Step-By-Step Oh My Zsh Installation Guide)

Z Shell (Zsh) stands as an interactive Unix shell renowned for its role as a command interpreter and shell scripting tool. It represents an enhanced Bourne shell, incorporating various improvements inspired by Bash, ksh, and tcsh functionalities. Zsh presents several advantages, including command-line completion, shared history, enhanced globbing, intelligent tab completion, spelling correction, and an extensive collection of plugins and themes.

This tutorial will guide you through the process of Oh My Zsh installation. We will also see how to install Zsh on the Ubuntu 22.04 system, allowing you to leverage its powerful features for an improved command-line experience.

What Is the Difference Between Zsh vs. Bash?

Both Zsh and Bash are widely adopted open-source command-line interpreters. Zsh is designed to be backwards compatible with Bash, boasting nearly identical syntax. This ensures a smooth transition between the two, as most commands and interaction patterns learned in Bash also apply to Zsh.

In comparing Zsh vs. Bash, the prominent difference is the extensive interactive customization features offered by Zsh. Users can personalize their shell experience by configuring themes and tapping into a vast ecosystem of Zsh plugins. These plugins introduce numerous interactive tweaks, enhancing the overall user experience.

While the individual gains from these enhancements may seem modest, the cumulative effect of these productivity tweaks quickly elevates your command-line performance to a higher level.

Prerequisites

To install Zsh and the Oh My Zsh plugin, you should have the following prerequisites:

Ubuntu 22.04 or the latest should run on your system or inside Virtualbox.

You should have root or sudo privileges in case of non-root users.

Zsh Installation on Ubuntu 22.04 (Install Zsh Ubuntu)

Before starting the Oh My Zsh setup process, you need to install Zsh on Ubuntu 22.04. Therefore, follow the steps provided below to install Zsh Ubuntu. After completing the Zsh installation, we'll guide you through the installation of Oh My Zsh on Ubuntu 22.04 and how to start using this enhanced shell environment.

Let's start the Oh My Zsh installation on Ubuntu 22.04.

Install Zsh on Ubuntu 22.04

To install Zsh on Ubuntu, follow the steps below:

Step 1: Update Ubuntu Apt Repositories

Before installing the Zsh Ubuntu, it's essential to update your software repositories. Open your terminal and execute the following command:

$ sudo apt update

how to install oh my zsh on ubuntu 22.04 (step-by-step oh my zsh installation guide)

After installing the updates, set up the required dependencies or packages for installing Zsh on Ubuntu 22.04:

$ sudo apt install build-essential curl file git

Step 2: Install Zsh From the Official Ubuntu Repositories

To install Zsh Ubuntu, use the Ubuntu apt repositories, and execute the following command:

$ sudo apt install -y zsh

In the above command, the "-y" option automatically accepts all installation prompts instead of asking from the user.

how to install oh my zsh on ubuntu 22.04 (step-by-step oh my zsh installation guide)

Step 3: Verify the Zsh Installation

With Zsh successfully installed, you can verify the Zsh installation by displaying the installed version and using the following command:

$ zsh --version

The above command will display the Zsh version installed on your server.

how to install oh my zsh on ubuntu 22.04 (step-by-step oh my zsh installation guide)

You can also verify the Zsh Ubuntu installation by checking the path to the Zsh binary:

$ which zsh

how to install oh my zsh on ubuntu 22.04 (step-by-step oh my zsh installation guide)

The above command should display the path “usr/bin/zsh.”

You can also use the “whereis” command to verify the installation path:

$ whereis zsh

how to install oh my zsh on ubuntu 22.04 (step-by-step oh my zsh installation guide)

The "whereis" command displays the location of a terminal program or script. In this case, it provides the specific path to Zsh.

Step 4: Set Zsh as Your Default Shell

Now that Zsh is installed, you can set Zsh as your default shell using the chsh command. Specify the path to the Zsh binary with the "-s" option:

$ sudo chsh -s /usr/bin/zsh

This command modifies the “/etc/passwd” user's file to load Zsh as the default shell for your username during terminal sessions. After restarting your terminal, confirm the change by displaying the current shell using the $SHELL environment variable:

echo $SHELL

Congratulations, you have successfully installed Zsh on Ubuntu, making it your default terminal shell.

Install Oh My Zsh on Ubuntu 22.04 (Oh My Zsh Ubuntu installation)

Oh My Zsh stands as an open-source framework designed to elevate the functionality and features of the Z Shell, aiming to enhance productivity and enjoyment in the command-line environment. This framework provides a hassle-free installation process and efficient management of an extensive array of themes, plugins, and scripts.

Step 1: Install Curl or Wget

The Oh My Zsh Ubuntu installation takes a few seconds to complete on your system. First, install curl or wget to clone the git repository or download it from the internet:

$ sudo apt install curl

Step 2: Oh My Zsh Ubuntu Installation

While you've transitioned to Zsh as your default shell, the experience may not feel significantly different from Bash at this point. Rather than building up Zsh from the ground up independently, a more effective approach is to leverage the Oh My Zsh ecosystem. 

This allows you to explore and experiment with the wealth of tools and configurations already developed by the community, providing a more streamlined and enriched command-line experience.

You can install Oh My Zsh on Ubuntu 22.04 using the following command:

$ sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

Executing the command above will establish Zsh as the default shell for every user. No server restart or logoff is required. Simply input “Y” and press “Enter” to initiate the process, which will be completed in just a few seconds. The following output should display inside your terminal window:


how to install oh my zsh on ubuntu 22.04 (step-by-step oh my zsh installation guide)

Configure Oh My Zsh Plugins

A Zsh plugin comprises a set of convenient aliases and functions tailored to enhance productivity when using command-line tools. The Oh My Zsh ships with over 300 pre-configured plugins that are ready to use right away.

By default, these pre-built plugins are deactivated. You can locate a list of locally installed plugins in the “~/.oh-my-zsh/plugins” directory. To activate a plugin in Oh My Zsh, edit the plugin line in the .zshrc configuration file. Simply add the names of your preferred plugins, separated by spaces:

plugins=(plugin_1 plugin_2 plugin_3 …)

To access the plugins list, open your web browser and visit the GitHub URL: https://github.com/ohmyzsh/ohmyzsh/wiki/Plugins  

Here, you can access the list of all available plugins.

Once you've explored the available plugins and identified the one you wish to utilize, use its name in the plugin() function within the ~/.zshrc file to enable it. For example, to activate the Python pip plugin, add the following line to the ~/.zshrc file:

plugins=(python pip)

After modifying the theme, use “Ctrl-O” to save and “Ctrl-X” to exit. Then, refresh the Oh My Zsh plugin by executing the following command:

source ~/.zshrc

Configuring the Oh-My-Zsh Theme

Customizing your preferred Oh My Zsh theme is simple. The default directory for Oh My Zsh Themes is located at ~/.oh-my-zsh/themes/. You can explore all available themes in this directory.

To view the list of available themes, open your browser and navigate to:

https://github.com/ohmyzsh/ohmyzsh/wiki/Themes 

When selecting a theme, avoid those with rainbow colors or hard-to-read color combinations. Themes featuring white or light words on a black background may not be suitable for users with impaired vision or on headless servers without a monitor.

Preview your chosen theme in your browser. Once you've found a theme you like, open the ~/.zshrc file using any code editor. For example:

sudo nano ~/.zshrc

how to install oh my zsh on ubuntu 22.04 (step-by-step oh my zsh installation guide)

In the screenshot above, the default theme is set to “robbyrussel.” To change the theme, replace the text after ZSH_THEME= with your preferred theme. For example, if you want to use the “agnoster” theme, replace “robbyrussel” with “agnoster:”

ZSH_THEME="agnoster"

If you wish to alternate between multiple themes regularly, set the ZSH_THEME= parameter to random:

ZSH_THEME="random"

To specify a predefined set of themes for random selection, define the ZSH_THEME_RANDOM_CANDIDATES parameter. For example:

ZSH_THEME_RANDOM_CANDIDATES=("agnoster" "grml" "robbyrussell")

After changing the theme, save with “Ctrl-O” and exit with “Ctrl-X.” Then, refresh the Oh My Zsh plugin:

source ~/.zshrc

Configure the Auto-Suggestions Plugin

Enable the auto-suggestions plugin to receive command suggestions based on your Zsh command history, saving time and facilitating the completion of daily tasks, especially for frequently used commands. Follow the steps below to activate auto-suggestions:

Begin by adding the auto-suggestion plugin repository. Clone the repository with the following command:

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

Open the .zshrc configuration file using a text editor:

nano ~/.zshrc

In the plugins section of the file, enable the auto-suggestion plugin by adding it to the list of plugins, as illustrated below:

plugins=(git zsh-autosuggestions)

how to install oh my zsh on ubuntu 22.04 (step-by-step oh my zsh installation guide)

Save the modifications and exit the text editor.

Restart the terminal and launch Zsh to activate the auto-suggestions plugin.

Now, when typing a command, use the right arrow key to accept the suggestion and effortlessly complete the command.

Enable Syntax Highlighting

Syntax highlighting offers several advantages, such as enhanced code readability, error detection, language recognition, increased productivity, and improved aesthetics. Follow the steps below to integrate syntax highlighting into Zsh:

Clone the plugin repository with the following command:

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


how to install oh my zsh on ubuntu 22.04 (step-by-step oh my zsh installation guide)

Open the .zshrc file:

nano ~/.zshrc

Navigate to the plugins section and include zsh-syntax-highlighting in the list of plugins.

how to install oh my zsh on ubuntu 22.04 (step-by-step oh my zsh installation guide)

Save the modifications and exit the file.

Restart the terminal and launch Zsh to activate syntax highlighting. For instance, typing a command with a typo will highlight it in red, while correctly entering a command will display it in green:

how to install oh my zsh on ubuntu 22.04 (step-by-step oh my zsh installation guide)

In the given example, we demonstrated the functioning of highlighting using the “echo” command.

Configure JSON Tools Plugin

Given JSON's widespread adoption as the default data standard in web development, you might often find yourself needing to interact with it directly in the terminal, especially during the testing of new API calls. The JSON Tools Plugin enhances this experience, providing a more convenient way to work with JSON. To give it a try, incorporate the plugin into your Zsh configuration file:

plugins=(... jsontools)

With this plugin activated, you can easily pipe your JSON data into the relevant JSON tool. For example, a standard JSON dump can be challenging to read. Let's fetch information about a random Chuck Norris joke from the Chuck Norris Jokes API:

curl https://api.chucknorris.io/jokes/random

how to install oh my zsh on ubuntu 22.04 (step-by-step oh my zsh installation guide)

Now, let's pass this JSON dump through the pretty print JSON tool:

curl https://api.chucknorris.io/jokes/random | pp_json

As demonstrated, the output is now formatted for human readability. The JSON Tools Plugin significantly improves the clarity of JSON data in your terminal.

Configure Oh My Zsh Automatic Updates

By default, OH-MY-ZSH updates automatically every two weeks. To deactivate this feature, uncomment the DISABLE_AUTO_UPDATE line in the ~/.zshrc file and set the parameter DISABLE_AUTO_UPDATE to "true."

sudo nano ~/.zshrc

DISABLE_AUTO_UPDATE="true"

how to install oh my zsh on ubuntu 22.04 (step-by-step oh my zsh installation guide)

If you prefer to customize the update frequency, you can set the number of days for the update check. For instance, to skip the auto-update every day, set the parameter UPDATE_ZSH_DAYS to 1.

UPDATE_ZSH_DAYS=1

Save the changes with “Ctrl-O,” followed by “Enter,” and exit with “Ctrl-X.” Then, refresh the Oh My ZSH plugin by typing the following:

source ~/.zshrc

You also have the option to manually run the update using the following command:

omz update

Alternatively, you can use the extended command:

omz update command

Finally, to apply the changes, restart your system:

sudo reboot now

That concludes the installation and configuration of Oh My Zsh on Ubuntu 22.04. By following the guides above, you have successfully tailored the automatic update settings according to your preferences.

Configure Dirhistory Plugin

This plugin enhances your directory navigation experience by building upon the concept of directory access history. It introduces convenient keyboard bindings, making it even easier to navigate through your directory history. To enable this feature, add "dirhistory" to your configuration file:

plugins=(... dirhistory)

Once added, save the file and refresh the Zsh configuration:

source ~/.zshrc

Now, take advantage of the following key bindings to seamlessly navigate through your directory history: ALT + LEFT to move to the previous directory, ALT + RIGHT to navigate to the next directory, ALT + UP to enter the parent directory, and ALT + DOWN to move to the first child directory. This plugin adds a layer of efficiency to your directory exploration.

Tab Autocompletion in Zsh

Another remarkable feature of Zsh is its advanced tab completion system. This functionality intelligently suggests available commands based on the command-line context, including scripts, environment variables, commands, and their options.

To experience this, type the system and press TAB to receive a list of auto-suggestions. Pressing TAB again allows you to navigate through the available programs using TAB or arrow keys:

how to install oh my zsh on ubuntu 22.04 (step-by-step oh my zsh installation guide)

Similarly, typing systemctl, adding a space, and pressing TAB provides a list of available systemctl commands with their descriptions. Navigate through the list using TAB or arrow keys:

how to install oh my zsh on ubuntu 22.04 (step-by-step oh my zsh installation guide)

Entering systemctl - and pressing TAB multiple times allows you to navigate through a list of available systemctl options with their descriptions:

command options autocomplete

For a touch of magic, type systemctl stop, add a space, and press TAB to witness a list of all currently running Linux processes eligible for the “systemctl stop” command:

target processes autocomplete

Embracing advanced tab autocompletion is straightforward and offers a significant productivity boost. Incorporate it into your command-line toolkit for a more efficient and powerful experience.

Find Reverse History Search

Traditionally, invoking reverse history search involves using “CTRL + R,” allowing you to search for any word within your command history. Press “CTRL + R” and enter a substring, such as “system,” to locate the last command containing this substring:

how to install oh my zsh on ubuntu 22.04 (step-by-step oh my zsh installation guide)

Continue pressing “CTRL + R” to move backward through history, or use “CTRL + S” to navigate forward.

Zsh introduces a subtle enhancement to this functionality. If you recall the beginning of a command you're searching for, type it and utilize the UP and DOWN keys to traverse through the history filtered by your specified substring. For example, enter “apt” and press UP to find the last command commencing with this substring.

Configure History Plugin

Within Oh My Zsh, many plugins are essentially collections of practical aliases crafted by seasoned system administrators. The History plugin offers a set of convenient aliases for the history command. Activate it by adjusting the Zsh configuration file:

plugins=(... history)

Upon activation, you gain access to the following aliases:

h (history): Prints your command history.

hs (history | grep): Uses grep to search your command history.

his (history | grep -i): Employs grep for a case-insensitive command history search.

Enable History Timestamps

An additional useful feature is the inclusion of timestamps for each history command. To activate this functionality, uncomment the following line in your configuration file and select your preferred date format:

HIST_STAMPS="yyyy-mm-dd"

Enabling this setting ensures that a timestamp accompanies each command displayed by the history command, providing a chronological record of your command history.

Enable Colored Man Pages Plugin

How frequently do you find yourself perusing the help pages of various command-line utilities? The Colored Man Pages Plugin enhances your reading speed by highlighting different commands and their arguments, making them instantly noticeable in the documentation. Enable this plugin by modifying the Zsh configuration file:

plugins=(... colored-man-pages)

If you need to uninstall Oh My Zsh, along with its plugins and themes, from Ubuntu 22.04, you can achieve this by removing the “oh-my-zsh” package. Execute the following command in your terminal:

$ sudo uninstall oh_my_zsh
Customizing your preferred Oh My Zsh theme is simple. The default directory for Oh My Zsh Themes is located at ~/.oh-my-zsh/themes/. You can explore all available themes in this directory.

$ sudo apt --purge remove zsh

This command will remove all files and folders associated with Oh My Zsh, restoring the server to its state before the installation.

Switch to Zsh to Bash Shell

Switch the default shell back to Bash (or any other preferred shell) using the following command:

$ chsh -s $(which bash)
After running this command, close the current terminal and open a new session to ensure that the changes take effect.

For Linux hosting on a VPS, utilize and install Zsh Ubuntu Shell on your server.

Conclusion

This guide demonstrated the Oh My Zsh installation and configuration on Ubuntu 22.04. Although Bash continues to be widely utilized and serves as the default shell for numerous Linux distributions, Zsh has gained popularity due to its myriad advantages. 

You have the flexibility to customize your Zsh environment further and incorporate personal tweaks. To install Zsh Ubuntu extends beyond a mere shell, as it also functions as a powerful scripting language. 

If you choose to delve deeper into customization, explore the extensive resources available in the official Zsh documentation.