Skip to content
Tutorials Center
My learning

Topics

Lesson 2 of 36 ยท Getting Started

Installing and Setting Up Material UI

Install Material UI in a React project, add Emotion, Roboto and icons, understand peer dependencies, and choose between path imports and the barrel import.

Setting up Material UI takes a few minutes: install the packages, load a font, and render your first component. This lesson walks through each step for a Vite-based React app, explains why MUI needs Emotion and peer dependencies, and shows the import style that keeps your bundle small.

You need Node.js (an LTS version) and a React project. If you do not have one yet, create it with Vite.

Create a React project

The quickest way to get a clean React app is Vite. These commands are read-only here because they run in your terminal, not in the browser:

Terminal
npm create vite@latest my-mui-app -- --template react
cd my-mui-app
npm install

Pick react-ts instead of react if you want TypeScript. MUI ships its own type definitions, so no extra @types package is needed. For a framework such as Next.js, see the Next.js chapter of the React course; we cover MUI with Next.js later in this course.

Install the packages

Material UI needs three groups of packages:

Terminal
npm install @mui/material @emotion/react @emotion/styled
npm install @mui/icons-material
  • @mui/material is the component library.
  • @emotion/react and @emotion/styled are the default styling engine. MUI components generate their CSS through Emotion.
  • @mui/icons-material is optional but you will want it for icons.

If you prefer Yarn or pnpm, use yarn add or pnpm add with the same package names.

Peer dependencies

@mui/material lists react and react-dom as peer dependencies: it does not install React itself, it expects your app to provide a compatible version. Emotion is also a peer dependency, which is why you install it explicitly. If the versions do not match, npm prints a warning (or an error, depending on the setting). Read it; version mismatches cause confusing runtime bugs.

The runnable example below is a tiny stand-in that mimics how a peer-dependency range check works. It is plain JavaScript, no MUI involved.

JS How a peer dependency range check works

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The real check is done by npm with full semver rules, but the idea is the same: a library says which versions of React it works with, and your installed versions must fall in that range.

Load the Roboto font

Material Design uses the Roboto typeface, and MUI's default theme asks for it. MUI does not load the font for you. The simplest option is the Fontsource package, which bundles the font files with your app:

Terminal
npm install @fontsource/roboto

Then import the weights you use once, near the top of your entry file (src/main.jsx):

JSX
import '@fontsource/roboto/300.css';
import '@fontsource/roboto/400.css';
import '@fontsource/roboto/500.css';
import '@fontsource/roboto/700.css';

Alternatively, add a <link> tag for Google Fonts to index.html. Fontsource is friendlier for privacy rules and offline use because the files are served from your own domain. If you skip the font entirely, MUI falls back to Helvetica or Arial, which works but looks different from the documentation.

Weights 300, 400, 500 and 700 are the ones MUI's typography uses: light for large headings, regular for body, medium for buttons and bold for strong text. Loading only the weights you need keeps the page fast.

Render your first component

Replace the contents of src/App.jsx with this real MUI code (read-only here):

JSX
import Button from '@mui/material/Button';

export default function App() {
  return <Button variant="contained">Hello Material UI</Button>;
}

Start the dev server with npm run dev. You should see a blue contained button with an uppercase label. If styles look wrong or the font is missing, check the troubleshooting list at the end of this lesson.

Path imports vs the barrel import

You can import a component in two ways:

JSX
// Path import: one component per line
import Button from '@mui/material/Button';
import TextField from '@mui/material/TextField';

// Barrel import: one line, many names
import { Button, TextField } from '@mui/material';

Both are valid. The barrel import is shorter and is tree-shakeable in modern bundlers (Vite, Next.js, webpack 5 in production mode). Historically, the barrel import made development builds slow, because bundlers had to process the whole package, which has hundreds of modules. Path imports avoid that, so many teams prefer them, especially in large apps.

Pick one style per project and enforce it with an ESLint rule (no-restricted-imports). Mixing both is harmless but looks inconsistent. We use path imports in this course because they work in every tool without configuration.

The icons package has the same choice. The icon barrel import { Delete } from '@mui/icons-material' is notoriously slow in development, so always prefer import DeleteIcon from '@mui/icons-material/Delete'.

What the setup looks like

The next example is a stand-in for a typical first screen: it shows the three things you just installed (component library, font, icon) working together. The Icon is an inline SVG and the font stack mimics MUI's default fontFamily.

Stand-in: first screen with font, button and icon

Troubleshooting checklist

  • "Cannot find module '@emotion/react'": you forgot to install Emotion. Add @emotion/react and @emotion/styled.
  • Two copies of React or "Invalid hook call": run npm ls react. All packages must share one React.
  • Font looks like Arial: Roboto was not imported. Add the Fontsource imports or a font <link>.
  • Slow dev server after adding icons: switch from the icon barrel import to per-icon path imports.
  • Styles look unpolished (default browser margins, mismatched backgrounds): add CssBaseline, the subject of the next lesson.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which packages must you install besides @mui/material for the default styling engine?

    MUI uses Emotion by default, and it is a peer dependency, so you install @emotion/react and @emotion/styled yourself.
  2. 2. Why does MUI list React as a peer dependency?

    A peer dependency means the library relies on the host app to provide that package, which avoids two copies of React.
  3. 3. What is the main downside of the barrel import (import { Button } from '@mui/material') in a large app?

    Production bundles are tree-shaken, but dev tools may have to process the whole package, so path imports are often faster during development.
  4. 4. How do you get the Roboto font into your app with Fontsource?

    MUI does not load fonts. Fontsource bundles Roboto so you import the weights you need once in your entry file.

Summary

  • Install @mui/material, @emotion/react, @emotion/styled, and optionally @mui/icons-material.
  • React and Emotion are peer dependencies: your app provides them and versions must match the allowed ranges.
  • MUI does not load Roboto; use Fontsource or a Google Fonts link.
  • Path imports are safest for fast development builds; barrel imports are shorter and tree-shake in production.
  • Check for duplicate React copies and missing Emotion when setup fails.

Next: First App: ThemeProvider and CssBaseline