Install Watchexec on Ubuntu 24.04

Install Watchexec on Ubuntu 24.04

Watchexec is a command line tool that watches files and directories for changes and then executes a specified command when a change is detected. It is commonly used by developers to automate tasks such as running tests, building projects, or restarting servers when the source code or configuration files are modified. This tutorial explains how to install Watchexec on Ubuntu 24.04.

Install Watchexec

Retrieve the newest release tag for Watchexec and assign it to a variable:

WATCHEXEC_VERSION=$(curl -s "https://api.github.com/repos/watchexec/watchexec/releases/latest" | grep -Po '"tag_name": "v\K[0-9.]+')

Download Watchexec archive from GitHub releases page:

wget -qO watchexec.tar.xz https://github.com/watchexec/watchexec/releases/latest/download/watchexec-$WATCHEXEC_VERSION-x86_64-unknown-linux-gnu.tar.xz

Create temporary directory and extract a tar.xz file:

mkdir watchexec-temp
tar xf watchexec.tar.xz --strip-components=1 -C watchexec-temp

Move the binary to the /usr/local/bin directory:

sudo mv watchexec-temp/watchexec /usr/local/bin

Watchexec version can be verified using a command:

watchexec --version

Remove the downloaded file along with the temporary directory:

rm -rf watchexec.tar.xz watchexec-temp

Testing Watchexec

Create a file for testing:

echo "console.log('Hello world');" > test.js

Now, run the following command to watch files with js extension:

watchexec -e js echo "File changed"

Once the test.js file change is detected, Watchexec triggers the echo command, which prints "File changed" to the terminal. To quit Watchexec, press CTRL+C.

Uninstall Watchexec

To uninstall Watchexec, remove its associated file:

sudo rm -rf /usr/local/bin/watchexec

Leave a Comment

Cancel reply

Your email address will not be published.