What it is
The Railway CLI is your command-line interface for managing your Railway deployments, allowing you to deploy code, view logs, and interact with your services from your terminal.
Installation
Linux / macOS:
curl -fsSL https://railway.app/install.sh | bash
Windows (PowerShell):
irm https://railway.app/install.sh | iex
Verification:
railway --version
Core Concepts
- Project: A collection of services and variables that represent your application on Railway.
- Environment: A specific deployment of your project, often representing different stages like development, staging, or production. Each environment has its own set of deployed services and variables.
- Service: A deployable unit within a project (e.g., a web server, a database).
- Deployment: A specific version of a service that has been built and deployed to an environment.
- Variables: Environment variables that can be set at the project or environment level, used to configure your services.
Commands / Usage
Project & Environment Management
-
Initialize Railway in a directory:
railway initPrompts you to select an existing project and environment, or create a new one, and links the current directory to it.
-
Link the current directory to a Railway project:
railway linkSimilar to
railway init, but specifically for linking an existing directory to a project. -
List available projects:
railway projectsDisplays a list of projects you have access to.
-
List available environments for the current project:
railway environmentsShows the environments linked to the currently configured project.
-
Switch the current project:
railway project set <project-id-or-name>Sets the active project for subsequent CLI commands.
-
Switch the current environment:
railway env set <environment-id-or-name>Sets the active environment for subsequent CLI commands.
Deployment
-
Deploy from the current directory:
railway upBuilds and deploys your project to the current environment. This is the most common command for deploying code changes.
-
Deploy a specific service:
railway up --service my-web-serviceDeploys only the specified service, triggering a build and redeploy for it.
-
Force a redeploy of all services:
railway up --rerunForces a rebuild and redeploy of all services, even if no code changes are detected.
-
Deploy with specific build arguments:
railway up --build-arg MY_BUILD_VAR=some_valuePasses build-time arguments to the Docker build process.
-
Deploy and only build, not deploy:
railway up --build-onlyBuilds the project’s images but does not deploy them. Useful for debugging build issues.
-
Deploy to a specific environment (not the current one):
railway up --environment productionDeploys the project to the 'production' environment, regardless of the currently set environment.
Logs
-
View logs for all services in the current environment:
railway logsStreams logs from all deployed services in real-time.
-
View logs for a specific service:
railway logs --service my-apiStreams logs only from the 'my-api' service.
-
View historical logs:
railway logs --prevShows logs from the previous deployment.
-
View logs for a specific deployment ID:
railway logs --deployment <deployment-id>Fetches logs for a particular deployment.
-
Follow logs for a specific service:
railway logs --service my-worker --followContinuously streams logs for the 'my-worker' service.
Service Management
-
List services in the current project/environment:
railway servicesDisplays all services configured for the project.
-
View details of a specific service:
railway services show my-databaseProvides detailed information about the 'my-database' service.
-
Open a service’s deployment in the web UI:
railway services open my-frontendOpens the 'my-frontend' service’s deployment page in your browser.
Variables
-
List all variables for the current project/environment:
railway variablesShows all environment variables set at the project and environment levels.
-
Set a project-level variable:
railway variables set --project MY_PROJECT_VAR=project_valueSets or updates a variable that applies to all environments in the project.
-
Set an environment-level variable:
railway variables set --environment MY_ENV_VAR=env_valueSets or updates a variable specific to the current environment.
-
Unset a variable:
railway variables unset MY_VARRemoves a variable from the current environment.
-
Unset a project-level variable:
railway variables unset --project MY_PROJECT_VARRemoves a project-level variable.
Database Access
-
Connect to a database service:
railway connect my-postgres-dbEstablishes a direct connection to the specified database service, providing connection details and often launching a client.
-
Execute a command on a service:
railway run my-app -- python manage.py migrateRuns a specific command within the context of the 'my-app' service in the current environment.
Status and Health
-
Get the status of deployments:
railway statusShows the current status of all services and deployments in the active environment.
-
Check the health of services:
railway healthReports on the health status of your deployed services.
Other Commands
-
View your Railway account information:
railway accountDisplays information about your current Railway account.
-
View the Railway CLI configuration:
railway configShows the current configuration of the Railway CLI.
-
Generate a new SSH key for deployment:
railway generate ssh-keyCreates a new SSH key pair and adds the public key to your Railway account.
-
Show the latest deployment ID:
railway deploy --latestPrints the ID of the most recent deployment.
Common Patterns
-
Deploying and then watching logs:
railway up && railway logsDeploys your changes and then immediately tails the logs for all services.
-
Deploying a specific service and watching its logs:
railway up --service my-api && railway logs --service my-apiDeploys only the 'my-api' service and then tails its specific logs.
-
Running database migrations:
railway up --service my-backend && railway run my-backend -- python manage.py migrateFirst, deploy your backend service, then run database migrations within that service’s environment.
-
Connecting to a database and running a query (e.g., with
psql):railway connect my-postgres-db -- psql -c "SELECT * FROM users;"Connects to the 'my-postgres-db' service and executes a single SQL command.
-
Setting a production-only variable:
railway env set production && railway variables set --environment DATABASE_URL=postgres://user:pass@host:port/dbSwitches to the 'production' environment and then sets a sensitive environment variable.
-
Troubleshooting a failed deployment by checking logs:
railway status # Identify the failed service railway logs --service <failed-service-name>First, check the overall status to find the problem, then dive into the logs of the specific failing service.
Gotchas
- Current Directory Matters: Most commands (
railway up,railway logs,railway init) operate based on the current working directory. Ensure you are in the correct project directory or have linked it properly. - Environment Context: Commands like
railway upandrailway logsoperate on the currently active environment. Userailway env set <env-name>to switch environments before performing actions. - Variable Precedence: Project-level variables are overridden by environment-level variables with the same name.
railway connectvs. Direct Access:railway connectprovides a secure tunnel. For direct access outside of the CLI, you’ll need to configure your service to be publicly accessible or use other methods outlined in Railway’s documentation.- Build Context: The
railway upcommand uses the current directory as the build context by default. If yourDockerfileis not at the root or requires specific files, ensure your project structure andrailway.toml(if used) are configured correctly. - Service Names: When using flags like
--serviceor commands likerailway run, ensure you are using the exact service name as defined in your Railway project.