Skip to content

Less - Installation

This chapter guides you through installing Less, a CSS preprocessor that extends CSS with dynamic behavior such as variables, mixins, operations, and functions. The most common way to use Less is by compiling it into standard CSS using Node.js.

  • Node.js and npm: Less compilation via the command line or build tools requires Node.js. Npm (Node Package Manager) is included with Node.js and is used to install Less itself.
  • Text Editor: Any text editor for writing code (e.g., VS Code, Sublime Text, Atom).

If you don’t have Node.js installed, visit https://nodejs.org/ and download the installer for your operating system (Windows, macOS, or Linux). The LTS (Long Term Support) version is generally recommended for stability. Follow the installation instructions provided by the installer.

Step 2: Verify Node.js and npm Installation

Section titled “Step 2: Verify Node.js and npm Installation”

Once Node.js is installed, open your terminal (Command Prompt on Windows, Terminal on macOS/Linux) and verify the installation by typing:

node -v
npm -v

These commands should display the installed versions of Node.js and npm, respectively.

You can install Less globally on your system or locally within a project. For starting out and general command-line use, global installation is common.

Global Installation:

npm install -g less

The -g flag installs Less globally, making the lessc (Less compiler) command available from any directory in your terminal.

Local (Project) Installation (Recommended for projects):

# Navigate to your project directory first
# cd /path/to/your/project
npm install less --save-dev

This installs Less as a development dependency for the current project and adds it to your package.json file. This is the preferred method for projects, as it ensures that all developers on the project use the same version of Less. You would typically run locally installed Less via npm scripts.

After installation (global or local), you can verify that Less is installed correctly by checking its version:

lessc -v

This should output the installed version of Less, for example: lessc 4.1.3 (Less Compiler) [JavaScript]. The exact version number may differ.

Let’s create a basic Less file and compile it to CSS.

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Less Example</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<header>
<h1>Welcome to Modern Less</h1>
</header>
<main>
<p>Dynamic styling made easy!</p>
<button class="btn-primary">Click Me</button>
</main>
</body>
</html>

2. Create a Less file (style.less) in the same directory:

Section titled “2. Create a Less file (style.less) in the same directory:”
// Define some variables
@primary-color: #3498db; // A nice blue
@secondary-color: #2ecc71; // A calm green
@text-color: #333;
@base-font-size: 16px;
// Global styles
body {
font-family: Arial, sans-serif;
color: @text-color;
line-height: 1.6;
margin: 20px;
}
header h1 {
color: @primary-color;
text-align: center;
}
main p {
font-size: @base-font-size * 1.1;
}
// A reusable mixin for button styles
.button-base() {
display: inline-block;
padding: 10px 15px;
border: none;
border-radius: 4px;
color: white;
cursor: pointer;
text-decoration: none;
font-size: @base-font-size;
}
.btn-primary {
.button-base(); // Use the mixin
background-color: @primary-color;
&:hover {
background-color: darken(@primary-color, 10%);
}
}

Open your terminal, navigate to the directory where you saved style.less and index.html, and run:

lessc style.less style.css

This command will process style.less and generate a style.css file in the same directory. If style.css already exists, it will be overwritten.

body {
font-family: Arial, sans-serif;
color: #333333;
line-height: 1.6;
margin: 20px;
}
header h1 {
color: #3498db;
text-align: center;
}
main p {
font-size: 17.6px;
}
.btn-primary {
display: inline-block;
padding: 10px 15px;
border: none;
border-radius: 4px;
color: white;
cursor: pointer;
text-decoration: none;
font-size: 16px;
background-color: #3498db;
}
.btn-primary:hover {
background-color: #217dbb;
}

Open index.html in your web browser. You should see the styles applied: the heading in blue, and a styled blue button.

Manually re-running lessc after every change is inefficient. In modern development, you would typically:

  • Use a file watcher to automatically recompile Less files on save. Tools like nodemon, chokidar-cli, or less-watch-compiler can be configured for this.
  • Integrate Less compilation into a build process using tools like Webpack (with less-loader), Parcel, Gulp (with gulp-less), or npm scripts.
  • lessc: command not found: If you installed Less globally and get this error, your system’s PATH environment variable might not include the npm global binaries directory. Search online for “npm global path not found [your OS]” for solutions. Alternatively, try running with npx lessc ... which uses the locally or globally installed package.
  • Project Dependencies: For team projects or to ensure consistent environments, always install Less locally (npm install less --save-dev). You can then run lessc using an npm script in your package.json, e.g., "scripts": { "compile:less": "lessc src/style.less dist/style.css" }, and execute it with npm run compile:less.
  • Official Documentation: For the most up-to-date information, advanced usage, and language features, always refer to the official Less.js website: http://lesscss.org/