How to Install and Use tmux on Ubuntu: A Complete Guide 2024
Introduction
tmux (Terminal Multiplexer) is a powerful command-line tool that enhances your terminal experience on Ubuntu and other Unix-like systems. It allows you to create multiple terminal sessions within a single window, making it an invaluable tool for developers, system administrators, and power users.
What is tmux?
tmux is a terminal multiplexer that enables you to:
- Create multiple terminal sessions within a single window
- Keep programs running even after disconnecting from SSH
- Split your terminal window into multiple panes
- Switch between different terminal sessions seamlessly
- Share terminal sessions with other users
Benefits of Using tmux
When working on Ubuntu servers or local machines, tmux offers several compelling advantages:
-
Persistent Sessions: Your work continues running even if your SSH connection drops or you close your terminal accidentally. Simply reattach to your session, and everything will be exactly as you left it.
-
Improved Productivity: Work with multiple terminal windows simultaneously without cluttering your desktop. You can easily switch between different tasks while maintaining a clear overview of your work.
-
Enhanced Collaboration: Share your terminal sessions with team members, making it perfect for pair programming or troubleshooting together.
System Requirements
Before installing tmux on Ubuntu, ensure your system meets these minimal requirements:
- Ubuntu 18.04 LTS or later
- Basic terminal familiarity
- Sufficient system privileges (sudo access) for installation
- At least 50MB of free disk space
Installation Methods
There are two primary methods to install tmux on Ubuntu: using the apt package manager or building from source. Let's explore both options in detail.
Using apt Package Manager
The simplest and recommended way to install tmux is through Ubuntu's package manager, apt. Here's how to do it:
# Update package list
sudo apt update
# Install tmux
sudo apt install tmux -y
# Verify the installation
tmux -V
This method automatically handles dependencies and provides a stable version of tmux that's tested with your Ubuntu release.
Building from Source
For users who need the latest features or specific versions, building from source is an option:
# Install required dependencies
sudo apt install git automake build-essential libevent-dev ncurses-dev
# Clone the source code
git clone https://github.com/tmux/tmux.git
cd tmux
# Build and install
sh autogen.sh
./configure
make
sudo make install
Version Comparison
Let's compare the different installation methods:
-
APT Installation:
- Pros: Easy to install and upgrade, stable version, automatic dependency management
- Cons: May not be the latest version
- Current version in Ubuntu repositories: Typically 1-2 versions behind the latest
-
Source Installation:
- Pros: Access to the latest features, ability to customize compilation
- Cons: Requires manual dependency management, more complex installation process
- Latest version available: Always up-to-date with the official repository
After installation, you can verify your tmux version by running:
tmux -V
Basic Configuration
tmux's behavior can be customized through its configuration file and various settings. Let's explore how to set up and customize tmux to suit your needs.
Location of Config File
tmux looks for its configuration file in your home directory:
# Create your tmux configuration file
touch ~/.tmux.conf
# Open it with your favorite text editor
nano ~/.tmux.conf
Essential Configurations
Here are some commonly used configurations to enhance your tmux experience:
# Change the prefix key to Ctrl+a (more convenient than default Ctrl+b)
set -g prefix C-a
unbind C-b
bind C-a send-prefix
# Enable mouse support
set -g mouse on
# Start window numbering at 1 (instead of 0)
set -g base-index 1
# Set easier window split keys
bind-key v split-window -h
bind-key h split-window -v
# Set easier window movement keys
bind -n M-Left select-pane -L
bind -n M-Right select-pane -R
bind -n M-Up select-pane -U
bind -n M-Down select-pane -D
# Improve colors
set -g default-terminal "screen-256color"
# Set scrollback buffer size
set -g history-limit 10000
Customizing Key Bindings
You can customize key bindings to make tmux more intuitive and efficient for your workflow:
-
Basic Key Binding Syntax:
bind-key key command bind key command
-
Common Custom Bindings:
# Reload configuration file bind r source-file ~/.tmux.conf \; display "Config reloaded!" # Quick pane cycling unbind ^A bind ^A select-pane -t :.+ # More intuitive split commands bind | split-window -h bind - split-window -v
After making changes to your configuration file, you can either:
- Restart tmux for the changes to take effect
- Or reload the configuration while tmux is running by pressing
prefix
+r
(if you've added the reload binding above)
Getting Started with tmux
After installation and basic configuration, let's learn how to use tmux effectively. This section covers the essential commands and operations you'll need for daily use.
Starting Your First Session
Here are the basic commands to start and manage tmux sessions:
# Start a new session
tmux
# Start a new named session
tmux new -s mysession
# List all sessions
tmux ls
# Attach to an existing session
tmux attach -t mysession
# Detach from current session (inside tmux)
# Press prefix key (Ctrl+b or Ctrl+a) then d
Basic Commands and Shortcuts
All tmux commands start with a prefix key (default: Ctrl+b). Here are essential shortcuts:
# Session Management
prefix + d # Detach from session
prefix + s # List sessions
prefix + $ # Rename current session
# Window Management
prefix + c # Create new window
prefix + n # Move to next window
prefix + p # Move to previous window
prefix + , # Rename current window
prefix + w # List windows
# Pane Operations
prefix + % # Split pane vertically
prefix + " # Split pane horizontally
prefix + o # Switch to next pane
prefix + x # Close current pane
Session Management
Sessions are the highest level of organization in tmux. Here's how to manage them effectively:
-
Creating Sessions:
# Create a new session with a specific name tmux new -s development # Create a new session with a specific working directory tmux new -s project -c ~/projects/myproject
-
Managing Multiple Sessions:
# Switch between sessions tmux switch -t session_name # Kill a specific session tmux kill-session -t session_name # Kill all sessions except the current one tmux kill-session -a
-
Session Navigation:
- Use
prefix + (
to move to previous session - Use
prefix + )
to move to next session - Use
prefix + s
to show session list and select interactively
- Use
Advanced Features
tmux provides powerful features for advanced users that can significantly enhance productivity. Let's explore some of these advanced capabilities.
Window Management
Windows in tmux function similar to tabs in a modern terminal. Here are some advanced window operations:
# Advanced Window Commands
prefix + . # Move window to a different number
prefix + f # Find window by name
prefix + & # Kill window
prefix + 0-9 # Switch to window by number
# Swap window positions
swap-window -s 2 -t 1 # Swap windows 2 and 1
swap-window -t -1 # Move current window left
Pane Operations
Panes allow you to divide your window into multiple sections. Here are advanced pane management techniques:
-
Resizing Panes:
# Using prefix followed by: Alt + Arrow keys # Resize pane in direction of arrow # Or hold prefix and press: Ctrl + Arrow keys # Resize pane in larger increments
-
Advanced Pane Navigation:
prefix + { # Move current pane left prefix + } # Move current pane right prefix + z # Toggle pane zoom (maximize/restore) prefix + ! # Convert pane into a window
-
Synchronize Panes:
# Toggle synchronize-panes (send commands to all panes) :setw synchronize-panes
Copy Mode and Scrolling
Copy mode allows you to scroll, search, and copy text:
-
Entering and Navigating Copy Mode:
prefix + [ # Enter copy mode q # Exit copy mode Space # Start selection Enter # Copy selection # Navigation in copy mode: Arrow keys # Move cursor Page Up/Down # Scroll page up/down g # Go to top G # Go to bottom / # Search forward ? # Search backward n # Next search match N # Previous search match
-
Advanced Copy Operations:
# Enable vi mode for better copy operations set-window-option -g mode-keys vi # Custom key bindings for copy mode bind-key -T copy-mode-vi v send-keys -X begin-selection bind-key -T copy-mode-vi y send-keys -X copy-selection
-
Integration with System Clipboard:
# On Ubuntu, install xclip first: sudo apt install xclip # Add to .tmux.conf: bind-key -T copy-mode-vi y send-keys -X copy-pipe-and-cancel "xclip -sel clip -i"
These advanced features can be combined to create powerful workflows. For example, you can:
- Set up multiple panes for monitoring different services
- Use synchronized panes to execute commands on multiple servers simultaneously
- Create complex window layouts for different development tasks
- Set up automated backup of tmux sessions
Best Practices
Adopting good practices with tmux can significantly improve your workflow and productivity. Let's explore some recommended approaches and tips.
Common Workflows
-
Development Environment Setup:
# Create a new session for development tmux new -s dev # Common development layout # Split window for editing and terminal tmux split-window -v -p 30 # 70% editor, 30% terminal # Additional split for running tests/servers tmux split-window -h # Split terminal pane horizontally
-
Server Monitoring Setup:
# Create monitoring session tmux new -s monitoring # Split for different monitoring tasks tmux split-window -h # Split for system stats tmux split-window -v # Split for logs # Run monitoring commands # First pane: htop # Second pane: tail -f /var/log/syslog # Third pane: network monitoring
Productivity Tips
-
Session Organization:
- Use descriptive session names (e.g.,
client1
,backend
,docs
) - Keep related tasks in the same session but different windows
- Use consistent window naming conventions
- Regular session cleanup for better management
- Use descriptive session names (e.g.,
-
Performance Optimization:
# Add to .tmux.conf # Reduce escape-time delay set -sg escape-time 0 # Increase responsiveness set -g status-interval 1 # Limit session history to prevent memory issues set -g history-limit 50000
-
Automated Setup Scripts:
#!/bin/bash # Save as dev-setup.sh # Create new session in detached state tmux new-session -d -s development # Set up windows and panes tmux rename-window -t development:1 'editor' tmux send-keys -t development:1 'vim' C-m tmux new-window -t development:2 -n 'server' tmux send-keys -t development:2 'npm run dev' C-m # Attach to session tmux attach -t development
Integration with Other Tools
-
Version Control Integration:
- Create dedicated windows for git operations
- Use pane synchronization for managing multiple repositories
- Set up automated status checking
-
Terminal Multiplexer Best Practices:
# Add to .bashrc or .zshrc # Automatically attach to tmux session on SSH if [[ -z "$TMUX" ]] && [ "$SSH_CONNECTION" != "" ]; then tmux attach-session -t ssh_tmux || tmux new-session -s ssh_tmux fi
-
Remote Development:
- Use nested tmux sessions for local/remote work
- Set different status bar colors for local/remote sessions
- Configure different prefix keys for nested sessions
# For nested sessions (.tmux.conf)
bind-key -n C-a send-prefix # For inner session
set -g status-bg colour40 # Green for local
set -g status-bg colour160 # Red for remote
Frequently Asked Questions (FAQ)
Q1: How do I exit tmux?
# Method 1: Completely exit tmux
exit # Type in tmux window
# or
prefix + d # Detach from current session while keeping it running
# Method 2: Force close all sessions
tmux kill-server
Q2: Why isn't my mouse scrolling working?
In newer versions of tmux, you need to enable mouse support in your configuration file:
# Add to ~/.tmux.conf
set -g mouse on
Q3: How do I copy text in tmux?
-
Using mouse (if mouse support is enabled):
- Simply select the text
- Use system clipboard shortcuts
-
Using keyboard:
prefix + [ # Enter copy mode Space # Start selection Enter # Copy selection prefix + ] # Paste
Q4: How do I recover a lost tmux session?
# List all sessions
tmux ls
# Reattach to the last session
tmux attach
# Attach to a specific session
tmux attach -t session_name
Q5: How can I resize panes in tmux?
# Use prefix key + arrow keys to resize panes
prefix + Up # Expand current pane upward
prefix + Down # Expand current pane downward
prefix + Left # Expand current pane left
prefix + Right # Expand current pane right
Q6: How do I apply configuration changes?
# Method 1: Reload within tmux
prefix + :
source-file ~/.tmux.conf
# Method 2: Reload from terminal
tmux source-file ~/.tmux.conf
Q7: How do I synchronize input across panes?
# In tmux command mode
:setw synchronize-panes on
# Turn off synchronization
:setw synchronize-panes off
Q8: Why are my colors not displaying correctly?
Add these lines to your ~/.tmux.conf
:
# Enable 256 color support
set -g default-terminal "screen-256color"
# Enable true color support
set-option -sa terminal-overrides ",xterm*:Tc"
Q9: How do I save and restore tmux sessions?
You can use the tmux-resurrect
plugin:
# Install tmux plugin manager
git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm
# Add to .tmux.conf:
set -g @plugin 'tmux-plugins/tmux-resurrect'
# Save sessions
prefix + Ctrl-s
# Restore sessions
prefix + Ctrl-r
Q10: How do I change my status bar appearance?
# Add to ~/.tmux.conf
# Change status bar background
set -g status-bg black
# Change status bar foreground
set -g status-fg white
# Change current window styling
set-window-option -g window-status-current-style bg=red,fg=white,bold
Q11: How do I handle nested tmux sessions?
# Add to .tmux.conf to use different prefix for nested sessions
bind-key -n C-a send-prefix # For inner session
set -g status-bg blue # Different color for nested session
Q12: My function keys aren't working in tmux?
# Add to ~/.tmux.conf
set-option -g xterm-keys on
set-window-option -g xterm-keys on