You’ve just upgraded your store to Magento 2.4.8, setup:upgrade and setup:di:compile both finished without complaint, and then static content deployment stops with a Magento 2 LESS compilation error like this one:
Compilation from source: /var/www/html/vendor/magento/theme-frontend-blank/web/css/styles-l.less error evaluating function `unit` The first argument to unit must be a number. Have you forgotten parenthesis? index: 1085 in _typography.less on line 32, column 1 31| @font-size-unit: rem; // The unit to which most typography values will be converted by default 32| @font-size-unit-ratio: unit(@root__font-size * 16/100); // Ratio of the root font-size to the font-size unit 33| @font-size-unit-convert: true; // Controls whether font-size values are converted to the specified font-size unit
Sometimes the same error points to line 35 (@font-size__base: unit(@font-size-unit-ratio * @font-size-ratio__base, px);) or to another .less file of your custom theme. And to make it more confusing, compiling the same files with Grunt works perfectly.
In this tutorial, we will learn why the Magento 2.4.8 LESS compilation error happens and how to fix it step by step, without hacking core files. Previously, I published a guide on how to upgrade Magento 2 to the latest version; if you’re in the middle of that upgrade, this is the fix for the static content step.
Contents
- Why does the LESS compilation error happen in Magento 2.4.8?
- Step 1: Find the file that really breaks
- Step 2: Refresh the outdated lib/web files
- Step 3: Wrap divisions in parentheses in your theme and modules
- Step 4: Update less.php to the latest 5.x version
- Step 5: Clean preprocessed files and redeploy
- Common errors
- Wrapping up
Why does the LESS compilation error happen in Magento 2.4.8?
When Magento deploys static content in production (or with setup:static-content:deploy), it doesn’t use Node.js. It compiles LESS on the server with the PHP library wikimedia/less.php.
Magento 2.4.8 upgraded that library to the 5.x major version (the Magento Open Source 2.4.8 release notes list it as AC-12023: “updated to use the latest major version 5.x of the wikimedia/less.php library”). You can confirm it in Composer: magento/framework 103.0.7 (Magento 2.4.7) requires wikimedia/less.php ^3.2, while 103.0.8 (Magento 2.4.8) requires ^5.0.
The important change in less.php 5.0 is in its changelog: the math option default changed from always to parens-division. In plain words:
- Before:
16/100was always calculated, so it became0.16. - Now: a division is only calculated when it is inside parentheses, like
(16/100). Otherwise the slash stays as text, because in CSS a slash can be a valid separator (for examplefont: 12px/1.5).
So unit(@root__font-size * 16/100) receives something that isn’t a number, and less.php stops with “The first argument to unit must be a number. Have you forgotten parenthesis?”. Multiplication, addition and subtraction still work as before; only division needs parentheses.
Here’s the interesting part: Magento’s own lib/web/css/source/lib/variables/_typography.less was already fixed in 2.4.7 to use double parentheses:
@font-size-unit-ratio: unit((@root__font-size * 16/100)); // Ratio of the root font-size to the font-size unit
If your error shows the old line without the extra parentheses, your server is compiling an outdated copy of that file. In practice it is almost always one of these:
- The
lib/webfolder in your project root wasn’t refreshed during the upgrade. In a Composer installation,lib/web/cssis copied from themagento/magento2-basepackage; if your deployment pipeline commitslib/webto Git or skips the copy, you keep the 2.4.6 version. - Your custom theme (or a theme you bought) overrides
_typography.less,_variables.lessor other library files with a copy taken from an older Magento version. - A third-party extension ships
.lessfiles that divide without parentheses.
Grunt compiles fine because it uses the Node.js less compiler with its own version and settings, not less.php. That’s why you can’t use a successful Grunt build as proof that production deploy will work.
Step 1: Find the file that really breaks
Run the deployment once more and read the error carefully:
php bin/magento setup:static-content:deploy -f en_US
The line Compilation from source: shows the entry file being compiled (for example styles-l.less), not the broken one. The file you need is the one after “in”: _typography.less on line 32. Because of Magento’s theme fallback, that file can live in several places: your theme in app/design, the parent theme, or lib/web. Search all of them for the old line:
grep -rn --include=*.less "unit(@root__font-size \* 16/100)" lib/web app/design app/code
Every result is a file that must be updated. If you don’t get any result, search for the exact line printed in your own error message instead.
Step 2: Refresh the outdated lib/web files
If the result is inside lib/web, compare that folder with the copy that came with Magento 2.4.8 in vendor:
diff -rq vendor/magento/magento2-base/lib/web/css lib/web/css
Any “differ” line means your root copy is not the 2.4.8 version. You should never edit lib/web by hand, so it’s safe to restore it from the package. Make a backup first, then copy:
cp -a lib/web/css ~/lib-web-css-backup cp -a vendor/magento/magento2-base/lib/web/css/. lib/web/css/ diff -rq vendor/magento/magento2-base/lib/web/css lib/web/css && echo "lib/web/css is up to date"
If your project keeps lib/web in Git, commit the updated files. Otherwise the next deployment will bring the old version back and the error will return.
Step 3: Wrap divisions in parentheses in your theme and modules
If the outdated file is in your theme (app/design/frontend/Vendor/theme/web/css/...) or in a module, update it. You have two options:
- If you only copied the core file to override one or two variables, delete the copied file and move your variable overrides to your theme’s
_theme.less. That way you always get the latest core version. My Magento 2 theme development tutorial explains where each file goes. - If you really need the custom file, add parentheses around every division.
Some before/after examples:
// Before (breaks or outputs invalid CSS with less.php 5.x) @font-size-unit-ratio: unit(@root__font-size * 16/100); width: @indent__base / 2; margin-top: -@indent__xl / 2; // After @font-size-unit-ratio: unit((@root__font-size * 16/100)); width: (@indent__base / 2); margin-top: -(@indent__xl / 2);
Be careful: a division outside unit() or another function usually doesn’t throw any error. less.php just writes it to the CSS as text, for example width: 20px / 2. Browsers ignore that invalid value, so your layout looks broken (smaller fonts, missing spacing) even though deployment succeeded. To find these hidden divisions in your own code, run:
grep -rnE --include=*.less "[0-9a-z_)] ?/ ?[0-9@(]" app/design app/code
This also matches comments and URLs, so review the results manually and only change real math expressions. Don’t forget extensions installed via Composer in vendor: if one of them causes the error, update it to a version that supports Magento 2.4.8 instead of editing vendor files.
Step 4: Update less.php to the latest 5.x version
Several compatibility bugs were fixed in later 5.x releases, so make sure you’re not stuck on 5.0. Check your installed version:
composer show wikimedia/less.php
Then update it within the range allowed by Magento:
composer update wikimedia/less.php
Don’t try to downgrade to less.php 3.x. Magento 2.4.8’s magento/framework requires ^5.0, so Composer will refuse the downgrade unless you break other constraints, and the old version is exactly what Adobe replaced for security reasons.
Step 5: Clean preprocessed files and redeploy
Magento caches preprocessed LESS files in var/view_preprocessed. If you skip this step, it may keep compiling the old content. Remove the generated files (keep the .htaccess in pub/static), redeploy and flush the cache:
rm -rf var/view_preprocessed/* pub/static/frontend/* pub/static/adminhtml/* php bin/magento setup:static-content:deploy -f en_US php bin/magento cache:flush
Add every locale your store uses to the deploy command (for example en_US fr_FR). Remove the -f flag if your store runs in production mode. When the deployment finishes with “Execution time”, open the storefront and check a few pages on desktop and mobile.
Common errors
Deployment works, but some styles are missing on desktop
After upgrading to 2.4.8, some stores reported that styles inside min-width: (@screen__l) media queries were ignored (GitHub issue #39817). Update less.php as in Step 4 and update your extensions; some vendors, such as Amasty, released a dedicated compatibility module for this problem. Also re-check your theme for divisions without parentheses, because they produce invalid CSS silently.
The error still shows the old line after I fixed the file
Magento is still reading a cached copy. Delete var/view_preprocessed again and make sure you edited the file Magento actually uses: a file in your child theme wins over the parent theme, and the theme wins over lib/web.
The deploy fails with a different LESS error
The fix is the same idea: read the file name after “in” and the line number, open that file, and look for a division without parentheses or a variable that is no longer a number. If the error is an exception unrelated to LESS, check var/log/exception.log; my guide to the Magento 2 500 internal server error shows how to read those logs.
You can read more about how Magento compiles LESS on the server in the official Adobe Commerce CSS and LESS preprocessing guide.
Wrapping up
The Magento 2.4.8 LESS compilation error “The first argument to unit must be a number” isn’t a bug in your new Magento version. It’s caused by an old .less file (usually _typography.less) that divides without parentheses, which less.php 5.x no longer evaluates. Refresh lib/web, add parentheses in your theme, update less.php, clear var/view_preprocessed and redeploy, and your styles will compile again.
Did this fix the error on your store, or did you get a different LESS message? Leave a comment below with the full error output and I’ll take a look.



