Adding Advanced Packages
Let’s extend the script to install more advanced packages like Docker and Node.js, which are essential for modern development and containerized workflows. This part will also teach you how to handle external repositories and enable services.
Step 1: Install Docker
Docker is a platform for building, running, and managing containers. Since it is not included in the default repositories of many Linux distributions, you need to ensure its installation steps are included in your script.
Add Docker Installation Commands to the Script: Modify your setup.sh to include commands for Docker installation:
echo "Installing Docker..."
# Install Docker for Debian-based systems
sudo apt install -y docker.io || sudo yum install -y docker
# Enable and start Docker service
sudo systemctl enable docker
sudo systemctl start docker
echo "Docker installed!"
These commands:
1. Install Docker using the default package manager.
2. Enable the Docker service to start at boot with systemctl enable docker.
3. Start the Docker service immediately with systemctl start docker.
Verify Docker Installation: After running the script, test Docker’s installation by checking its version:
docker --version
You can also test if Docker is running properly by pulling a test image:
sudo docker run hello-world
Step 2: Install Node.js
Node.js is a runtime environment for JavaScript, widely used for server-side applications. To ensure you’re installing the latest stable version, use the NodeSource repository.
Add Node.js Installation Commands to the Script: Update setup.sh to include the following:
echo "Installing Node.js..."
# Add NodeSource repository for Debian-based systems
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
# Install Node.js
sudo apt install -y nodejs || sudo yum install -y nodejs
echo "Node.js installed!"
This script:
1. Downloads and runs the NodeSource setup script for Debian-based systems using curl.
2. Installs Node.js and npm (Node Package Manager).
Verify Node.js Installation: After running the script, test Node.js and npm installations:
node --version
npm --version
Step 3: Handle External Repositories (Optional)
For distributions where Docker or Node.js is not available in default repositories, you may need to add external repositories or manually download .rpm or .deb files.
Add Docker’s Official Repository (Optional): If the default package manager does not have Docker, add its official repository:
echo "Adding Docker repository..."
# For Debian-based systems
sudo apt-get install -y apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt update
sudo apt install -y docker-ce
This method ensures you’re installing the latest version of Docker.
Add Node.js Repository for Red Hat-based Systems: If the package manager does not include Node.js, you can manually add the NodeSource repository:
curl -fsSL https://rpm.nodesource.com/setup_18.x | sudo bash -
sudo yum install -y nodejs
Step 4: Add Logging for Installations
To make debugging easier, log the output of each installation step to a file. Update your script:
LOGFILE=/var/log/setup-script.log
exec > >(tee -a $LOGFILE) 2>&1
echo "Installing Docker..."
sudo apt install -y docker.io || sudo yum install -y docker
sudo systemctl enable docker
sudo systemctl start docker
echo "Docker installed successfully!"
echo "Installing Node.js..."
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt install -y nodejs || sudo yum install -y nodejs
echo "Node.js installed successfully!"
This script captures both standard output and error messages, appending them to /var/log/setup-script.log.
Step 5: Test the Script
Run the Script: Execute the updated script:
./setup.sh
Check Installation: After the script completes, verify that both Docker and Node.js are installed and functioning properly:
docker --version
node --version
npm --version
Test Docker and Node.js:
For Docker:
sudo docker run hello-world
For Node.js: Create a simple test script:
echo 'console.log("Node.js is working!");' > test.js
node test.js
Step 6: Modularize the Script
To keep your script organized, consider breaking it into functions:
#!/bin/bash
LOGFILE=/var/log/setup-script.log
exec > >(tee -a $LOGFILE) 2>&1
install_docker() {
echo "Installing Docker..."
sudo apt install -y docker.io || sudo yum install -y docker
sudo systemctl enable docker
sudo systemctl start docker
echo "Docker installed successfully!"
}
install_nodejs() {
echo "Installing Node.js..."
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt install -y nodejs || sudo yum install -y nodejs
echo "Node.js installed successfully!"
}
# Call the functions
install_docker
install_nodejs
By organizing the script into functions, you make it easier to extend, debug, and maintain.
At the end of this part, you’ll have a script capable of installing both Docker and Node.js, two critical tools for modern development. These additions enhance the functionality of your script and prepare your server for containerized and JavaScript-based workflows.