Home / Exciting tips / Linux / How to make your own Ubuntu post-installation script

How to make your own Ubuntu post-installation script

Setting up Ubuntu with your favorite programs can be tedious. You have to search with the terminal for each application. Instead, consider creating a “post-installation” script. A shell script you can run right after a new installation of Ubuntu that will automatically install all programs with no effort.

Note: not using Ubuntu but using an Ubuntu-like operating system such as Linux Mint, Elementary OS, Zorin OS, or others? This post-installation script guide will work just fine on your OS as well!

Adding the shebang

The first step of writing a Bash script is to add in what’s called a “shebang.” In computing terms, a “shebang” is a mechanism that can tell the Bash terminal what program it should use to run the script. Examples of shebangs include #!/bin/bash, #!/bin/sh, and #!/usr/bin/env.

In shell scripts, the most common shebang to use is the #!/bin/bash, as scripts are usually run with the bash command. However, using #!/bin/sh is also acceptable. In our post-installation script, we will be using the most common shebang (#!/bin/bash).

To add the shebang to your Ubuntu post-installation script, you must first create the script file. To do that, use the touch command below.

touch ubuntu-post-installer.sh

After creating the ubuntu-post-installer.sh file on your Ubuntu PC, it is time to open up the file for editing in the Nano text editor. Using the command below, start editing.

nano -w ubuntu-post-installer.sh

Inside of the Nano text editor, write in the Bash shebang at the top of the file. Do not create any spaces! It must be the very first line.

#!/bin/bash

Following the shebang, press the Enter key to create a new line in the file, and move on to the next section of the guide.

Step 1 – Automatically installing programs from Apt

The thing that most Ubuntu users are going to want to add to their post-installation script is a line of code that will automatically install all of their most-used programs. Things like the Gimp image editor, maybe the Chromium web browser, the Steam gaming store, the Telegram chat app, and more.

To add your “automatic” Apt installer line of code, go to the Nano text editor and add the following line below. Be sure to change “program1 program2 program3” with the actual names of the packages you intend to have your script automatically install.

Note: you must specify the exact package names of each app for it to work with the automatic installer. If you cannot remember the name of a program you would like to add to the line of code, search for the package with apt search programname.

sudo apt install program1 program2 program3 -y

After writing out your automatic Apt installation line into the script, press the Enter key to create a new line. Then, save your edits so far by pressing Ctrl + O. Once all edits are saved, move on to the next section of the guide.

Step 2 – Automatically installing programs from the Snap Store

A lot of mainstream apps that Ubuntu users install are found in the Snap store. If you rely a lot on Snap apps, you may want to write in a line of code in your script that will take care of all Snaps on your system.

To start the process, head over to the Snap Store website, and use the search function to locate the apps you would like to install automatically.

When you find an app in the Snaps, click the “install” button to find the name of the package. Keep in mind that if you want to install apps that have “–classic” or “–edge,” they will need to be separate installation commands in the script.

Once you’ve got the names of all of the Snap packages, write the code below into the script. Please remember that “snappackage1 snappackage2 snappackage3” is an example. You will need to replace it for the script to function successfully.

sudo snap install snappackage1 snappackage2 snappackage3

Done adding the Snap line of code to your script? Press Ctrl + O to save the edits. Then, press Enter to make a new line and move to the next section of the guide.

Step 3 – Automatically installing Flatpak apps

Not a lot of Ubuntu users use Flatpak, as Snap packages are ready to go out of the box. However, if you are one of the Ubuntu users that rely on Flatpaks, you can easily add it to the post-installation script to make things easier.

To start the process, you must have the Flatpak runtime installed. To get your script to install Flatpak out of the box, enter the code below.

sudo apt install flatpak -y

Following the Flatpak installation line, you must have your script subscribe to the Flathub app store. Press Enter on the key and add the code below to the script.

flatpak remote-add --if-not-exists flathub https://flathub.org/repo/flathub.flatpakrepo

After adding the Flathub line of code, it is time to work on the Flatpak automatic installation command. First, run the flatpak search command and find the names of each package you’d like to install.

When you’ve got the names of all Flatpak apps, press the Enter key, and add in the installation command. Keep in mind that “flatpak1 flatpak2 flatpak3” is an example. You will need to change it to the actual names of the Flatpak packages.

sudo flatpak install flatpak1 flatpak2 flatpak3 -y

Once the Flatpak installation line is in the script, press Ctrl + O to save the edits, then, press Ctrl + X to exit Nano, as editing is complete!

Running the script

To run your Ubuntu post-installation script, follow the step-by-step instructions below.

Step 1: Place the script into the home directory of any new Ubuntu installation.

Step 2: Open up a terminal window on Ubuntu by pressing Ctrl + Alt + T or Ctrl + Shift + T on the keyboard. Then, update the permissions of the script with the chmod command.

sudo chmod +x ~/ubuntu-post-installer.sh

Step 3: Run the script on your fresh Ubuntu Linux PC with the bash command.

sudo bash ubuntu-post-installer.sh 

About excitingtips

Check Also

How to install the Snow GTK theme on Linux

Snow is a light theme for Linux that follows a Mac-like design spec with greyish …

Leave a Reply

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