The Firebase CLI lets you deploy hosting, functions, Firestore rules, and other services from your terminal. Install it with npm install -g firebase-tools, authenticate with firebase login, initialize your project with firebase init, and deploy with firebase deploy. Use the --only flag for selective deployment and .firebaserc for managing multiple project environments like staging and production.
Deploying with the Firebase CLI
The Firebase CLI is the primary tool for deploying Firebase Hosting, Cloud Functions, Firestore and RTDB rules, and Storage rules from your local development environment. This tutorial walks you through installing the CLI, logging in, initializing your project configuration, deploying individual services, using preview channels for staging, and setting up multiple project aliases for environment management.
Prerequisites
- Node.js 18 or later installed on your machine
- A Firebase project created in the Firebase Console
- A web app or Cloud Functions ready to deploy
- Terminal or command-line access
Step-by-step guide
Install the Firebase CLI
Install the Firebase CLI
Install the Firebase CLI globally using npm. This gives you the firebase command in your terminal. After installation, verify it works by checking the version. The CLI is regularly updated with new features and bug fixes, so keep it current. If you previously installed an older version, the same command upgrades it.
1# Install or update the Firebase CLI2npm install -g firebase-tools34# Verify installation5firebase --version67# Expected output: 13.x.x or laterExpected result: The firebase command is available in your terminal and shows the current version number.
Authenticate with Firebase
Authenticate with Firebase
Run firebase login to authenticate with your Google account. This opens a browser window where you sign in and authorize the CLI to access your Firebase projects. The CLI stores the auth token locally so you only need to log in once per machine. For CI/CD environments where no browser is available, use firebase login:ci to generate a token that can be used with the FIREBASE_TOKEN environment variable.
1# Interactive login (opens browser)2firebase login34# Verify you're logged in and see available projects5firebase projects:list67# For CI/CD: generate a CI token8firebase login:ci9# Then use: FIREBASE_TOKEN=your-token firebase deployExpected result: You are authenticated and firebase projects:list shows your Firebase projects.
Initialize Firebase in your project directory
Initialize Firebase in your project directory
Navigate to your project directory and run firebase init. The CLI walks you through selecting which Firebase features to configure: Hosting, Functions, Firestore Rules, Realtime Database Rules, Storage Rules, and Emulators. For each feature, it creates the necessary configuration files. Choose an existing Firebase project or create a new one. The command creates firebase.json (deployment config) and .firebaserc (project aliases).
1# Navigate to your project directory2cd your-project34# Initialize Firebase (interactive wizard)5firebase init67# Or initialize specific features only8firebase init hosting9firebase init functions10firebase init firestore11firebase init emulatorsExpected result: Your project directory contains firebase.json and .firebaserc with your selected features configured.
Configure firebase.json for deployment
Configure firebase.json for deployment
The firebase.json file controls what gets deployed and how. For Hosting, it specifies the public directory (where your build output goes), rewrites for SPAs, custom headers, and redirects. For Functions, it points to the functions directory. For Firestore and RTDB, it specifies the rules files. Review and customize this file to match your project structure.
1{2 "hosting": {3 "public": "dist",4 "ignore": ["firebase.json", "**/.*", "**/node_modules/**"],5 "rewrites": [6 { "source": "**", "destination": "/index.html" }7 ],8 "headers": [9 {10 "source": "**/*.@(js|css)",11 "headers": [12 { "key": "Cache-Control", "value": "max-age=31536000" }13 ]14 }15 ]16 },17 "functions": {18 "source": "functions",19 "runtime": "nodejs20"20 },21 "firestore": {22 "rules": "firestore.rules",23 "indexes": "firestore.indexes.json"24 },25 "database": {26 "rules": "database.rules.json"27 },28 "storage": {29 "rules": "storage.rules"30 }31}Expected result: firebase.json is configured with the correct public directory, rewrites, and paths to rules files.
Deploy all services or select specific ones
Deploy all services or select specific ones
Run firebase deploy to deploy all configured services at once, or use the --only flag to deploy specific services. Selective deployment is faster and safer because it only changes what you intend to update. You can deploy multiple specific targets by separating them with commas. Always build your frontend project before deploying Hosting.
1# Deploy everything2firebase deploy34# Deploy only hosting5npm run build && firebase deploy --only hosting67# Deploy only Cloud Functions8firebase deploy --only functions910# Deploy a specific function11firebase deploy --only functions:myFunction1213# Deploy only Firestore rules and indexes14firebase deploy --only firestore1516# Deploy multiple targets17firebase deploy --only hosting,functions1819# Deploy with a message for version tracking20firebase deploy --only hosting -m "v2.1.0 - Added user profiles"Expected result: The selected services are deployed to your Firebase project and the CLI shows the deployment URL.
Use preview channels for staging
Use preview channels for staging
Firebase Hosting preview channels let you deploy to temporary URLs for testing before going to production. Each channel gets a unique URL that expires after 7 days by default. Share these URLs with your team for review. When ready, deploy to production with the standard deploy command.
1# Deploy to a preview channel2firebase hosting:channel:deploy staging34# Deploy with a custom expiration (30 days)5firebase hosting:channel:deploy staging --expires 30d67# List all active channels8firebase hosting:channel:list910# Delete a preview channel11firebase hosting:channel:delete stagingExpected result: A temporary preview URL is generated where your team can test the deployment before going to production.
Set up project aliases for multiple environments
Set up project aliases for multiple environments
Use .firebaserc to define aliases for different Firebase projects, such as development, staging, and production. Switch between environments with firebase use and deploy to the correct project each time. This prevents accidental deployments to production. Each alias maps to a different Firebase project ID.
1// .firebaserc2{3 "projects": {4 "default": "my-app-dev",5 "staging": "my-app-staging",6 "production": "my-app-prod"7 }8}910// Switch between environments11// firebase use staging12// firebase deploy --only hosting1314// Or deploy to a specific alias without switching15// firebase deploy --only hosting --project productionExpected result: You can switch between project environments and deploy to the correct Firebase project using aliases.
Complete working example
1#!/bin/bash2# Firebase deployment script with environment support3# Usage: ./deploy.sh [environment] [service]4# Examples:5# ./deploy.sh production hosting6# ./deploy.sh staging functions7# ./deploy.sh production all89ENV=${1:-"staging"}10SERVICE=${2:-"all"}1112echo "Deploying to: $ENV"13echo "Service: $SERVICE"1415# Validate environment16if [[ "$ENV" != "staging" && "$ENV" != "production" ]]; then17 echo "Error: Environment must be 'staging' or 'production'"18 exit 119fi2021# Production safety check22if [[ "$ENV" == "production" ]]; then23 read -p "Are you sure you want to deploy to PRODUCTION? (y/n) " -n 1 -r24 echo25 if [[ ! $REPLY =~ ^[Yy]$ ]]; then26 echo "Deployment cancelled."27 exit 028 fi29fi3031# Build frontend if deploying hosting32if [[ "$SERVICE" == "hosting" || "$SERVICE" == "all" ]]; then33 echo "Building frontend..."34 npm run build35 if [ $? -ne 0 ]; then36 echo "Build failed. Aborting deployment."37 exit 138 fi39fi4041# Deploy42if [[ "$SERVICE" == "all" ]]; then43 firebase deploy --project "$ENV"44else45 firebase deploy --only "$SERVICE" --project "$ENV"46fi4748if [ $? -eq 0 ]; then49 echo "Deployment to $ENV ($SERVICE) complete!"50else51 echo "Deployment failed."52 exit 153fiCommon mistakes when using Firebase CLI for Deployment
Why it's a problem: Deploying hosting without running the build command first, resulting in stale or missing files
How to avoid: Always run your build command (npm run build) before firebase deploy --only hosting. Add it to your deployment script to automate this.
Why it's a problem: Deploying all services when only one changed, which is slow and risky
How to avoid: Use firebase deploy --only hosting or firebase deploy --only functions to deploy only what changed. This is faster and reduces the risk of unintended changes.
Why it's a problem: Deploying to production instead of staging because the wrong project alias is active
How to avoid: Always use the --project flag explicitly in CI/CD: firebase deploy --project my-app-staging. Check the active project with firebase use before deploying interactively.
Why it's a problem: Running firebase init in a directory that already has a firebase.json, accidentally overwriting configuration
How to avoid: The CLI warns before overwriting existing files, but always back up your firebase.json before running firebase init again. Use firebase init [feature] to add a single feature without touching existing config.
Best practices
- Use the --only flag to deploy specific services instead of deploying everything at once
- Always build your frontend before deploying hosting: npm run build && firebase deploy --only hosting
- Set up project aliases in .firebaserc for development, staging, and production environments
- Use preview channels for staging deployments with firebase hosting:channel:deploy
- Add a deployment script that includes build steps, environment selection, and confirmation prompts
- Keep the Firebase CLI updated with npm install -g firebase-tools for the latest features and fixes
- Use firebase login:ci and FIREBASE_TOKEN for automated CI/CD deployments
- Deploy Cloud Functions in batches of 10 or fewer to avoid API rate limits
Still stuck?
Copy one of these prompts to get a personalized, step-by-step explanation.
Walk me through setting up the Firebase CLI for a project that uses Hosting, Cloud Functions, and Firestore rules. Show me how to configure firebase.json, set up staging and production environments with project aliases, and write a deployment script.
Write a bash deployment script for Firebase that supports environment selection (staging/production), selective service deployment (hosting, functions, or all), automatic frontend builds before hosting deployment, and a production safety confirmation prompt.
Frequently asked questions
Do I need to install the Firebase CLI globally?
No. You can use npx firebase-tools to run CLI commands without a global install. However, global installation (npm install -g firebase-tools) is more convenient for frequent use and avoids re-downloading on every command.
How do I deploy to a different Firebase project?
Use the --project flag: firebase deploy --project my-other-project. Or set up aliases in .firebaserc and switch with firebase use my-alias. This is essential for managing staging and production environments.
Can I deploy from a CI/CD pipeline?
Yes. Generate a CI token with firebase login:ci and set it as the FIREBASE_TOKEN environment variable in your CI/CD system. Then run firebase deploy --project your-project in your pipeline. Most CI systems like GitHub Actions, CircleCI, and GitLab CI have Firebase deployment templates.
What does firebase deploy --only functions:myFunction do?
It deploys only the specific function named myFunction instead of all functions. This is faster and safer for making changes to a single function in production without affecting others.
How long does a hosting deployment take?
Typically 30 seconds to 2 minutes depending on the size of your build output and network speed. The Firebase CDN propagates changes globally within a few minutes after deployment completes.
What happens if a deployment fails halfway through?
Firebase deploys services atomically. If hosting deployment fails, your previous version remains live. If function deployment fails, already-deployed functions from that batch may have been updated, but other batches remain unchanged. Always deploy functions selectively to minimize risk.
Can RapidDev help set up a CI/CD pipeline for Firebase deployment?
Yes. RapidDev can configure automated deployment pipelines with GitHub Actions, staging preview channels, production approval gates, and rollback strategies for your Firebase project.
Talk to an Expert
Our team has built 600+ apps. Get personalized help with your project.
Book a free consultation